diff --git a/.env.example b/.env.example index 55b998f19..34af630e5 100644 --- a/.env.example +++ b/.env.example @@ -54,6 +54,13 @@ POSTGRES_PASSWORD= RAILS_ENV=development # Changes the Postgres query timeout limit. The default is 14 seconds. Modify only when required. # POSTGRES_STATEMENT_TIMEOUT=14s + +# read replica configuration for production +# POSTGRES_HOST_RR=postgres_rr +# create a new user with readonly permissions +# POSTGRES_USERNAME_RR=postgres_readonly +# POSTGRES_PASSWORD_RR= + RAILS_MAX_THREADS=5 # The email from which all outgoing emails are sent diff --git a/app/models/application_record.rb b/app/models/application_record.rb index c6877c883..adebaf03b 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -2,6 +2,8 @@ class ApplicationRecord < ActiveRecord::Base include Events::Types self.abstract_class = true + connects_to database: { writing: :primary, reading: :primary_replica } + before_validation :validates_column_content_length # the models that exposed in email templates through liquid diff --git a/config/database.yml b/config/database.yml index 5cd278f8d..ebdd769d2 100644 --- a/config/database.yml +++ b/config/database.yml @@ -22,7 +22,15 @@ test: password: "<%= ENV.fetch('POSTGRES_PASSWORD', '') %>" production: - <<: *default - database: "<%= ENV.fetch('POSTGRES_DATABASE', 'chatwoot_production') %>" - username: "<%= ENV.fetch('POSTGRES_USERNAME', 'chatwoot_prod') %>" - password: "<%= ENV.fetch('POSTGRES_PASSWORD', 'chatwoot_prod') %>" + primary: + <<: *default + database: "<%= ENV.fetch('POSTGRES_DATABASE', 'chatwoot_production') %>" + username: "<%= ENV.fetch('POSTGRES_USERNAME', 'chatwoot_prod') %>" + password: "<%= ENV.fetch('POSTGRES_PASSWORD', 'chatwoot_prod') %>" + primary_replica: + <<: *default + host: "<%= ENV.fetch('POSTGRES_HOST_RR', 'localhost') %>" + database: "<%= ENV.fetch('POSTGRES_DATABASE', 'chatwoot_production') %>" + username: "<%= ENV.fetch('POSTGRES_USERNAME_RR', 'chatwoot_prod_readonly') %>" + password: "<%= ENV.fetch('POSTGRES_PASSWORD_RR', 'chatwoot_prod') %>" + replica: true