Skip to content

Commit 0d7eb18

Browse files
committed
Prepare for new openshift/mysql-55-centos7 image
* 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
1 parent cf1fa89 commit 0d7eb18

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

app.rb

-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
def configure_database
99
if ENV['RACK_ENV']=="production"
1010
while !self.connect_to_database_prod
11-
puts "Connecting to production database (#{ENV['DATABASE_SERVICE_HOST']})...\n"
1211
sleep 0.1
1312
end
1413
else
1514
while !self.connect_to_database_test
16-
puts "Connecting to test database...\n"
1715
sleep 0.1
1816
end
1917
end

config/database.rb

+32-10
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,56 @@
22

33
def self.connect_to_database_prod
44
begin
5-
ActiveRecord::Base.establish_connection(
5+
config = {
66
:adapter => "mysql2",
77
:host => "#{ENV["DATABASE_SERVICE_HOST"]}",
88
:port => "#{ENV["DATABASE_SERVICE_PORT"]}",
9-
:database => "#{ENV["MYSQL_DATABASE"]}",
10-
:password => "#{ENV["MYSQL_ROOT_PASSWORD"]}"
11-
)
9+
:database => "#{ENV["MYSQL_DATABASE"]}"
10+
}
11+
if ENV.key?("MYSQL_ROOT_PASSWORD")
12+
config[:password] = "#{ENV["MYSQL_ROOT_PASSWORD"]}"
13+
else
14+
config[:username] = "#{ENV["MYSQL_USER"]}"
15+
config[:password] = "#{ENV["MYSQL_PASSWORD"]}"
16+
end
17+
18+
puts "Connecting to production database (#{config[:username]}@#{config[:host]}:#{config[:port]})..."
19+
ActiveRecord::Base.establish_connection(config)
1220

1321
ActiveRecord::Base.connection.active?
1422

15-
rescue Exception
23+
rescue Exception => e
24+
if not /Can't connect to MySQL server/ =~ e.message
25+
puts e.message
26+
end
1627
return false
1728
end
1829
end
1930

2031
def self.connect_to_database_test
2132
begin
22-
ActiveRecord::Base.establish_connection(
33+
config = {
2334
:adapter => "mysql2",
2435
:host => "#{ENV["DATABASE_TEST_SERVICE_HOST"]}",
2536
:port => "#{ENV["DATABASE_TEST_SERVICE_PORT"]}",
26-
:database => "#{ENV["MYSQL_DATABASE"]}",
27-
:password => "#{ENV["MYSQL_ROOT_PASSWORD"]}"
28-
)
37+
:database => "#{ENV["MYSQL_DATABASE"]}"
38+
}
39+
if ENV.key?("MYSQL_ROOT_PASSWORD")
40+
config[:password] = ENV["MYSQL_ROOT_PASSWORD"]
41+
else
42+
config[:username] = ENV["MYSQL_USER"]
43+
config[:password] = ENV["MYSQL_PASSWORD"]
44+
end
45+
46+
puts "Connecting to test database (#{config[:username]}@#{config[:host]}:#{config[:port]})..."
47+
ActiveRecord::Base.establish_connection(config)
2948

3049
ActiveRecord::Base.connection.active?
3150

32-
rescue Exception
51+
rescue Exception => e
52+
if not /Can't connect to MySQL server/ =~ e.message
53+
puts e.message
54+
end
3355
return false
3456
end
3557
end

config/database.yml

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1+
<% # Handle old behaviour of official mysql image %>
2+
<% user = ENV.key?("MYSQL_ROOT_PASSWORD") ? "root" : ENV["MYSQL_USER"] %>
3+
<% password = ENV.key?("MYSQL_ROOT_PASSWORD") ? ENV["MYSQL_ROOT_PASSWORD"] : ENV["MYSQL_PASSWORD"] %>
14
development:
25
adapter: mysql2
36
database: <%= ENV["MYSQL_DATABASE"] %>
4-
username: root
5-
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
7+
username: <%= user %>
8+
password: <%= password %>
69
host: <%= ENV["DATABASE_SERVICE_HOST"] %>
710
port: <%= ENV["DATABASE_SERVICE_PORT"] %>
811

912
test:
1013
adapter: mysql2
1114
database: <%= ENV["MYSQL_DATABASE"] %>
12-
username: root
13-
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
15+
username: <%= user %>
16+
password: <%= password %>
1417
host: <%= ENV["DATABASE_TEST_SERVICE_HOST"] %>
1518
port: <%= ENV["DATABASE_TEST_SERVICE_PORT"] %>
1619

1720
production:
1821
adapter: mysql2
1922
database: <%= ENV["MYSQL_DATABASE"] %>
20-
username: root
21-
password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
23+
username: <%= user %>
24+
password: <%= password %>
2225
host: <%= ENV["DATABASE_SERVICE_HOST"] %>
2326
port: <%= ENV["DATABASE_SERVICE_PORT"] %>

run.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ if [ -z "${MYSQL_DATABASE}" ]; then
66
export DATABASE_SERVICE_HOST=localhost
77
export DATABASE_SERVICE_PORT=3306
88
export MYSQL_DATABASE=test
9-
export MYSQL_ROOT_PASSWORD=root
9+
export MYSQL_USER=root
10+
export MYSQL_PASSWORD=root
1011
fi
1112

1213
bundle exec ruby app.rb

0 commit comments

Comments
 (0)