Home > Mobile >  Configuration for multiple databases with rails
Configuration for multiple databases with rails

Time:05-15

A Rails 7.0.3 application has the following database.yml configuration, with the default block generated by the rails app initialisation process.

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  primary:
    database: user_development
    username: deploy_root
    password: password
    host: "localhost"
    migrations_paths: db/user_migrate
  primary_replica:
    database: user_development
    username: deploy_readonly
    password: password
    host: "localhost"
    replica: true
  forum:
    database: forum_development
    username: deploy_root
    password: password
    host: "localhost"
    migrations_paths: db/forum_migrate
  forum_replica:
    database: forum_development
    username: deploy_readonly
    password: password
    host: "localhost"
    replica: true
  office:
    database: office_development
    username: deploy_root
    password: password
    host: "localhost"
    migrations_paths: db/office_migrate

test:
  <<: *default
  primary:
    database: user_test
    username: deploy_root
  primary_replica:
    database: user_test
    username: deploy_readonly
  forum:
    database: forum_test
    username: deploy_root
  forum_replica:
    database: forum_test
    username: deploy_readonly
  office:
    database: office_test
    username: deploy_root

  <<: *default
  primary:
    host: 'primary.domain.ws'
    username: deploy_root
    password: [omiss]
    database: user_production
    pool: 50
    timeout: 5000
  primary_replica:
    host: 'replica.domain.ws'
    username: deploy_readonly
    password: [omiss]
    database: user_production
    pool: 50
    timeout: 5000
    replica: true
  forum:
    database: forum_production
    username: deploy_root
    password: [omiss]
    host: 'forum.domain.ws'
    migrations_paths: db/forum_migrate
  forum_replica:
    database: forum_production
    username: deploy_readonly
    password: [omiss]
    host: 'forumreplica.domain.ws'
    replica: true
  office:
    database: office_production
    username: deploy_root
    password: [omiss]
    host: 'office.domain.ws'
    migrations_paths: db/office_migrate

However, upon running bin/rails db:create the application complains

no implicit conversion of nil into String
Couldn't create '' database. Please check your configuration.

The syntax is that used in other rails 7 apps, albeit not in partnet/replica contexts and seems to respect the rails guides guidelines.

What is wrong with the above that a string get interpreted as nil?

update If the development block is altered to

development:
  <<: *default
  database: office_development
  username: deploy_root
  password: password
  host: "localhost"
  migrations_paths: db/office_migrate
  primary:
    [...]

the default database is created, but not the main and replicas. thus there is an issue with the YAML structure, although it lints successfully.

CodePudding user response:

The call to shared attributes <<: *default needs to be removed. This will allow processing of development databases.

The attributes

  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

being added to each block.

An additional hang-up was with the test databases, the replicas:

  primary_replica:
    database: user_test
    username: deploy_readonly
  forum_replica:
    database: forum_test
    username: deploy_readonly

needed removing for the process to complete. I surmise that there is the assumption that objects being read from the main copy is the exact same as it being read from the replica and thus this is excessive

  • Related