|
2 | 2 |
|
3 | 3 | def self.connect_to_database_prod
|
4 | 4 | begin
|
5 |
| - ActiveRecord::Base.establish_connection( |
| 5 | + config = { |
6 | 6 | :adapter => "mysql2",
|
7 | 7 | :host => "#{ENV["DATABASE_SERVICE_HOST"]}",
|
8 | 8 | :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) |
12 | 20 |
|
13 | 21 | ActiveRecord::Base.connection.active?
|
14 | 22 |
|
15 |
| - rescue Exception |
| 23 | + rescue Exception => e |
| 24 | + if not /Can't connect to MySQL server/ =~ e.message |
| 25 | + puts e.message |
| 26 | + end |
16 | 27 | return false
|
17 | 28 | end
|
18 | 29 | end
|
19 | 30 |
|
20 | 31 | def self.connect_to_database_test
|
21 | 32 | begin
|
22 |
| - ActiveRecord::Base.establish_connection( |
| 33 | + config = { |
23 | 34 | :adapter => "mysql2",
|
24 | 35 | :host => "#{ENV["DATABASE_TEST_SERVICE_HOST"]}",
|
25 | 36 | :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) |
29 | 48 |
|
30 | 49 | ActiveRecord::Base.connection.active?
|
31 | 50 |
|
32 |
| - rescue Exception |
| 51 | + rescue Exception => e |
| 52 | + if not /Can't connect to MySQL server/ =~ e.message |
| 53 | + puts e.message |
| 54 | + end |
33 | 55 | return false
|
34 | 56 | end
|
35 | 57 | end
|
0 commit comments