Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/jobs/reports/base_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def report_timeout
end

def transaction_with_timeout
Db::EstablishConnection::ReadReplicaConnection.new.call do
ApplicationRecord.connected_to(role: :reading, shard: :read_replica) do
ActiveRecord::Base.transaction do
quoted_timeout = ActiveRecord::Base.connection.quote(report_timeout)
ActiveRecord::Base.connection.execute("SET LOCAL statement_timeout = #{quoted_timeout}")
Expand Down
5 changes: 5 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

connects_to shards: {
default: { writing: :primary, reading: :primary },
read_replica: { reading: :primary_replica },
}
end
45 changes: 0 additions & 45 deletions app/services/db/establish_connection/read_replica_connection.rb

This file was deleted.

15 changes: 15 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,23 @@ class Application < Rails::Application
)
IdentityConfig.build_store(configuration)

console do
if ENV['ALLOW_CONSOLE_DB_WRITE_ACCESS'] != 'true' &&
IdentityConfig.store.database_readonly_username.present? &&
IdentityConfig.store.database_readonly_password.present?
warn <<-EOS.squish
WARNING: Loading database a configuration with the readonly database user.
If you wish to make changes to records in the database set
ALLOW_CONSOLE_DB_WRITE_ACCESS to "true" in the environment
EOS

ActiveRecord::Base.establish_connection :primary_replica
end
end

config.load_defaults '6.1'
config.active_record.belongs_to_required_by_default = false
config.active_record.legacy_connection_handling = false
config.assets.unknown_asset_fallback = true

if IdentityConfig.store.ruby_workers_enabled
Expand Down
44 changes: 32 additions & 12 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<% require 'production_database_configuration' %>

postgresql: &postgresql
adapter: postgresql
encoding: utf8
Expand All @@ -24,22 +22,44 @@ defaults: &defaults
statement_timeout: <%= IdentityConfig.store.database_statement_timeout %>

development:
<<: *defaults
primary:
<<: *defaults
primary_replica:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: should we call it read_replica instead? That threw me off a little bit when you call establish_connection.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, and that matches the name in devops repo where we call it read-replica, updated in 27926b2

<<: *defaults
replica: true

test:
_test: &test
<<: *defaults
pool: 10
checkout_timeout: 10
database: <%= ENV['POSTGRES_DB'] || "upaya_test#{ENV['TEST_ENV_NUMBER']}" %>
user: <%= ENV['POSTGRES_USER'] %>
password: <%= ENV['POSTGRES_PASSWORD'] %>

test:
primary:
<<: *test
primary_replica:
<<: *test
replica: true

production:
<<: *defaults
database: <%= IdentityConfig.store.database_name %>
username: <%= ProductionDatabaseConfiguration.username %>
host: <%= ProductionDatabaseConfiguration.host %>
password: <%= ProductionDatabaseConfiguration.password %>
pool: <%= ProductionDatabaseConfiguration.pool %>
sslmode: 'verify-full'
sslrootcert: '/usr/local/share/aws/rds-combined-ca-bundle.pem'
primary:
<<: *defaults
database: <%= IdentityConfig.store.database_name %>
username: <%= IdentityConfig.store.database_username %>
host: <%= IdentityConfig.store.database_host %>
password: <%= IdentityConfig.store.database_password %>
pool: <%= IdentityConfig.store.database_pool_idp %>
sslmode: 'verify-full'
sslrootcert: '/usr/local/share/aws/rds-combined-ca-bundle.pem'
primary_replica:
<<: *defaults
database: <%= IdentityConfig.store.database_name %>
username: <%= IdentityConfig.store.database_readonly_username %>
host: <%= IdentityConfig.store.database_read_replica_host %>
password: <%= IdentityConfig.store.database_readonly_password %>
pool: <%= IdentityConfig.store.database_pool_idp %>
sslmode: 'verify-full'
sslrootcert: '/usr/local/share/aws/rds-combined-ca-bundle.pem'
replica: true

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turns out when a connection is set to replica: true Rails will block writes also, which is nice

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sweet that's awesome

56 changes: 0 additions & 56 deletions lib/production_database_configuration.rb

This file was deleted.

178 changes: 0 additions & 178 deletions spec/lib/production_database_configuration_spec.rb

This file was deleted.