diff --git a/app.rb b/app.rb index e47039f6fb..0156f78ce6 100644 --- a/app.rb +++ b/app.rb @@ -8,12 +8,10 @@ def configure_database if ENV['RACK_ENV']=="production" while !self.connect_to_database_prod - puts "Connecting to production database (#{ENV['DATABASE_SERVICE_HOST']})...\n" sleep 0.1 end else while !self.connect_to_database_test - puts "Connecting to test database...\n" sleep 0.1 end end diff --git a/config/database.rb b/config/database.rb index 793d0db248..bc1356d49e 100644 --- a/config/database.rb +++ b/config/database.rb @@ -2,34 +2,56 @@ def self.connect_to_database_prod begin - ActiveRecord::Base.establish_connection( + config = { :adapter => "mysql2", :host => "#{ENV["DATABASE_SERVICE_HOST"]}", :port => "#{ENV["DATABASE_SERVICE_PORT"]}", - :database => "#{ENV["MYSQL_DATABASE"]}", - :password => "#{ENV["MYSQL_ROOT_PASSWORD"]}" - ) + :database => "#{ENV["MYSQL_DATABASE"]}" + } + if ENV.key?("MYSQL_ROOT_PASSWORD") + config[:password] = "#{ENV["MYSQL_ROOT_PASSWORD"]}" + else + config[:username] = "#{ENV["MYSQL_USER"]}" + config[:password] = "#{ENV["MYSQL_PASSWORD"]}" + end + + puts "Connecting to production database (#{config[:username]}@#{config[:host]}:#{config[:port]})..." + ActiveRecord::Base.establish_connection(config) ActiveRecord::Base.connection.active? - rescue Exception + rescue Exception => e + if not /Can't connect to MySQL server/ =~ e.message + puts e.message + end return false end end def self.connect_to_database_test begin - ActiveRecord::Base.establish_connection( + config = { :adapter => "mysql2", :host => "#{ENV["DATABASE_TEST_SERVICE_HOST"]}", :port => "#{ENV["DATABASE_TEST_SERVICE_PORT"]}", - :database => "#{ENV["MYSQL_DATABASE"]}", - :password => "#{ENV["MYSQL_ROOT_PASSWORD"]}" - ) + :database => "#{ENV["MYSQL_DATABASE"]}" + } + if ENV.key?("MYSQL_ROOT_PASSWORD") + config[:password] = ENV["MYSQL_ROOT_PASSWORD"] + else + config[:username] = ENV["MYSQL_USER"] + config[:password] = ENV["MYSQL_PASSWORD"] + end + + puts "Connecting to test database (#{config[:username]}@#{config[:host]}:#{config[:port]})..." + ActiveRecord::Base.establish_connection(config) ActiveRecord::Base.connection.active? - rescue Exception + rescue Exception => e + if not /Can't connect to MySQL server/ =~ e.message + puts e.message + end return false end end diff --git a/config/database.yml b/config/database.yml index b2e5fe4d63..a096530beb 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,23 +1,26 @@ +<% # Handle old behaviour of official mysql image %> +<% user = ENV.key?("MYSQL_ROOT_PASSWORD") ? "root" : ENV["MYSQL_USER"] %> +<% password = ENV.key?("MYSQL_ROOT_PASSWORD") ? ENV["MYSQL_ROOT_PASSWORD"] : ENV["MYSQL_PASSWORD"] %> development: adapter: mysql2 database: <%= ENV["MYSQL_DATABASE"] %> - username: root - password: <%= ENV['MYSQL_ROOT_PASSWORD'] %> + username: <%= user %> + password: <%= password %> host: <%= ENV["DATABASE_SERVICE_HOST"] %> port: <%= ENV["DATABASE_SERVICE_PORT"] %> test: adapter: mysql2 database: <%= ENV["MYSQL_DATABASE"] %> - username: root - password: <%= ENV['MYSQL_ROOT_PASSWORD'] %> + username: <%= user %> + password: <%= password %> host: <%= ENV["DATABASE_TEST_SERVICE_HOST"] %> port: <%= ENV["DATABASE_TEST_SERVICE_PORT"] %> production: adapter: mysql2 database: <%= ENV["MYSQL_DATABASE"] %> - username: root - password: <%= ENV['MYSQL_ROOT_PASSWORD'] %> + username: <%= user %> + password: <%= password %> host: <%= ENV["DATABASE_SERVICE_HOST"] %> port: <%= ENV["DATABASE_SERVICE_PORT"] %> diff --git a/run.sh b/run.sh index 8d5d200784..83b4fdeff6 100755 --- a/run.sh +++ b/run.sh @@ -6,7 +6,8 @@ if [ -z "${MYSQL_DATABASE}" ]; then export DATABASE_SERVICE_HOST=localhost export DATABASE_SERVICE_PORT=3306 export MYSQL_DATABASE=test - export MYSQL_ROOT_PASSWORD=root + export MYSQL_USER=root + export MYSQL_PASSWORD=root fi bundle exec ruby app.rb