Search code examples
ruby-on-railspostgresqlrails-activerecord

database_tasks: false not working in rails 7.1


I have 2 read replicas configured in the database.yml and I want to avoid any write operation in the Ruby on Rails application.

When setting database_tasks: false the following exception was raised:

/.rvm/gems/ruby-3.2.2/gems/activerecord-7.1.1/lib/active_record/railtie.rb:151:in `block (3 levels) in <class:Railtie>': 
undefined method `name' for nil:NilClass (NoMethodError)
in db_config.name

Is database_task deprecated or has it some bug after version upgrade?

database.yml

service_read_replica_defaults: &service_read_replica_defaults
  adapter: postgresql
  username: root
  password: <%= nil %>
  host: localhost
  port: <%= 5432 %>
  database: <%= "service_#{Rails.env}") %>
  database_tasks: false

patient_read_replica_defaults: &patient_read_replica_defaults
  adapter: mysql2
  username: root
  password: <%= nil %>
  host: <%= localhost %>
  port: 3306
  database: <%= "patient_#{Rails.env}") %>
  database_tasks: false

production:
  service_read_replica:
    <<: *service_read_replica_defaults
  patient_read_replica:
    <<: *patient_read_replica_defaults

development:
  service_read_replica:
    <<: *service_read_replica_defaults
  patient_read_replica:
    <<: *patient_read_replica_defaults

test:
  service_read_replica:
    <<: *service_read_replica_defaults
  patient_read_replica:
    <<: *patient_read_replica_defaults

There are two external databases (postgresql and mysql) want to connect with read only access. And I want to use database_task: false to restrict write actions on external databases.


Solution

  • This line is causing it to return false and hence db_config is returned as nil. As the database.yml file contains only replicas and has database: false in each config, in the end no config is loaded and returned as nil. Hence, the error. The solution we are looking for is that if there is any configuration that we are missing to add in our project or is this scenario missed by rails itself?

    Just FYI, the error is occurring while running the command bundle exec rails s