Skip to content

Commit c92efc8

Browse files
committed
fix: Update database connection handling and configuration for appliance environment
1 parent f204456 commit c92efc8

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

config/database.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ test:
1919
username: <%= ENV.fetch("DATABASE_USER", "ontoportal") %>
2020
password: <%= ENV.fetch("DATABASE_PASSWORD", "ontoportal") %>
2121

22-
production:
22+
appliance:
2323
<<: *default
24-
host: <%= ENV["DATABASE_HOST"] %>
24+
host: <%= ENV.fetch("DATABASE_HOST", "localhost") %>
2525
port: <%= ENV.fetch("DATABASE_PORT", 5432) %>
26-
database: <%= ENV["DATABASE_NAME"] %>
27-
username: <%= ENV["DATABASE_USER"] %>
28-
password: <%= ENV["DATABASE_PASSWORD"] %>
26+
database: <%= ENV.fetch("DATABASE_NAME", "ontologies_api_appliance") %>
27+
username: <%= ENV.fetch("DATABASE_USER", "ontoportal") %>
28+
password: <%= ENV.fetch("DATABASE_PASSWORD", "ontoportal") %>

config/relationalDatabase.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55
module RelationalDatabase
66
class << self
77
def connect!
8-
ActiveRecord::Base.establish_connection(load_config[environment])
9-
puts "(API) >> Connected to PostgreSQL database: #{ActiveRecord::Base.connection_db_config.database}"
8+
env = environment
9+
config = load_config
10+
11+
ActiveRecord::Base.configurations = config
12+
13+
begin
14+
ActiveRecord::Base.establish_connection(env.to_sym)
15+
puts "(API) >> Connected to PostgreSQL database: #{ActiveRecord::Base.connection_db_config.database} (env: #{env})"
16+
rescue => e
17+
puts "(API) >> ERROR: Failed to connect to database for env '#{env}': #{e.message}"
18+
puts "(API) >> Available configs: #{ActiveRecord::Base.configurations.configs_for.map(&:env_name).inspect}"
19+
raise e
20+
end
1021
end
1122

1223
def create!
@@ -34,10 +45,12 @@ def config_file
3445
private
3546

3647
def load_config
37-
@load_config ||= YAML.safe_load(
38-
ERB.new(File.read(config_file)).result,
39-
aliases: true
40-
)
48+
@load_config ||= begin
49+
YAML.safe_load(
50+
ERB.new(File.read(config_file)).result,
51+
aliases: true
52+
)
53+
end
4154
end
4255

4356
def environment

config/unicorn.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
# Unix forking works, we need to make sure we aren't using any of the parent's
7676
# sockets, e.g. db connection
7777

78-
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
78+
defined?(ActiveRecord::Base) and RelationalDatabase.connect!
7979
# Redis and Memcached would go here but their connections are established
8080
# on demand, so the master never opens a socket
8181
end

0 commit comments

Comments
 (0)