Skip to content

Commit

Permalink
add support for a test database service.
Browse files Browse the repository at this point in the history
  • Loading branch information
bparees committed Nov 27, 2014
1 parent cd96685 commit c20ae08
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
13 changes: 10 additions & 3 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
%x"rake db:migrate"
puts "Run app..."

while !self.connect_to_database
puts "Connecting to database...\n"
sleep 0.1
if ENV['RACK_ENV']=="production"
while !self.connect_to_database_prod
puts "Connecting to production database...\n"
sleep 0.1
end
else
while !self.connect_to_database_test
puts "Connecting to test database...\n"
sleep 0.1
end
end
puts "Connected to database"
end
Expand Down
19 changes: 18 additions & 1 deletion config/database.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'sinatra/activerecord'

def self.connect_to_database
def self.connect_to_database_prod
begin
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
Expand All @@ -16,3 +16,20 @@ def self.connect_to_database
return false
end
end

def self.connect_to_database_test
begin
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "#{ENV["DATABASE_TEST_SERVICE_HOST"]}",
:port => "#{ENV["DATABASE_TEST_SERVICE_PORT"]}",
:database => "#{ENV["MYSQL_DATABASE"]}",
:password => "#{ENV["MYSQL_ROOT_PASSWORD"]}"
)

ActiveRecord::Base.connection.active?

rescue Exception
return false
end
end
8 changes: 8 additions & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ development:
host: <%= ENV["DATABASE_SERVICE_HOST"] %>
port: <%= ENV["DATABASE_SERVICE_PORT"] %>

test:
adapter: mysql2
database: <%= ENV["MYSQL_DATABASE"] %>
username: root
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
host: <%= ENV["DATABASE_TEST_SERVICE_HOST"] %>
port: <%= ENV["DATABASE_TEST_SERVICE_PORT"] %>

production:
adapter: mysql2
database: <%= ENV["MYSQL_DATABASE"] %>
Expand Down

0 comments on commit c20ae08

Please sign in to comment.