Skip to content

Commit

Permalink
Prepare for new openshift/mysql-55-centos7 image
Browse files Browse the repository at this point in the history
* Don't connect to database as root user
* Change user to username to avoid warning
* Print out exception when we can't connect
* Better debugging output (loggin user, host and port) and move it into
  the connection routine so we're really sure we log the correct values.
* Support for old behaviour
  • Loading branch information
mnagy committed Mar 16, 2015
1 parent cf1fa89 commit 0d7eb18
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
2 changes: 0 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 32 additions & 10 deletions config/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 9 additions & 6 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -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"] %>
3 changes: 2 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0d7eb18

Please sign in to comment.