Rails database replica setup

10+ years most with Ruby on Rails and JavaScript frontend and some Go. My first language is C for 3 years in university.
Search for a command to run...

10+ years most with Ruby on Rails and JavaScript frontend and some Go. My first language is C for 3 years in university.
No comments yet. Be the first to comment.
I recently updated from Slim template to ERB to use Herb (Thanks Marco). I had to update a test that was very strict on the format. I realized that Capybara::Node::Simple doesn’t provide a selector for regex. The page object doesn’t have a match? met...

It is as simple as: rustup completions zsh > ~/.zfunc/_rustup rustup completions zsh cargo > ~/.zfunc/_cargo With a TAB you get the auto-complete or the options available: ❯ cargo b b -- alias: build bench -- Execute all benchmarks of a local ...
Let’s not re-write a good article, go to: https://joyofrails.com/articles/mastering-custom-configuration-in-rails I mostly used config.x configuration that loads a yaml file with config_for that do the multi-environment by default: module YourApp c...
No need to re-write the wheel, let me redirect to this great article from Lily Reile. Obviously still up-to-date and really useful as the official Rails doc is mostly showing the value before and after. https://lilyreile.medium.com/rails-6-1-new-fram...
I came around to update Appsignal gem to the new major version of Appsignal released at the end of August 2024: https://rubygems.org/gems/appsignal/versions/4.0.0 The upgrade was smooth. The real change for my application was to remove the parameter ...
While reading the official documentation, I did not understand some parts of the set up. Let's me explain here.
1 main database
1 replica
Where to put the connects_to setting
Proper/clean configuration in database.yml
database.ymlNotes:
All environment needs primary and primary_replica otherwise Rails will raise an exception (at boot).
The *_replica needs replica: true
I did not put primary and primary_replica inside default because any change in a sub-object will erase all properties (I think)
In that example, only production has a proper replica with different settings
default: &default
adapter: trilogy
encoding: utf8mb4
pool: 5
database: <%= ENV.fetch('DB_NAME') { 'db_name' } %>
username: <%= ENV.fetch('DB_USER') { 'user' } %>
password: <%= ENV.fetch('DB_PASS') { 'password' } %>
host: <%= ENV.fetch('DB_HOST') { 'db.example.com' } %>
port: <%= ENV.fetch('DB_PORT') { '3306' } %>
development:
primary:
<<: *default
primary_replica:
<<: *default
replica: true
test:
primary:
<<: *default
primary_replica:
<<: *default
replica: true
staging:
primary:
<<: *default
primary_replica:
<<: *default
replica: true
production:
primary:
<<: *default
primary_replica:
<<: *default
username: <%= ENV.fetch('DB_USER_REPLICA') { 'user_replica' } %>
password: <%= ENV.fetch('DB_PASS_REPLICA') { 'password_replica' } %>
host: <%= ENV.fetch('DB_HOST_REPLICA') { 'db-replica.example.com' } %>
replica: true
connects_toI thought I could use connects_to directly on a model (eg: User) but no, it should be on an abstract_class or ActiveRecord::Base (what the exception says) but I didn't understand it. I thought I should put abstract_class on the User but no, it should the parent class ApplicationRecord or anything other parent class (eg: AdminApplicationRecord for admin user).
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
connects_to database: {
writing: :primary,
reading: :primary_replica
}
end
If it helped, please share it.
Have fun, bye.