'Creating and migrating a devise-driven User model in a main/replica context

The following main/replica database structure

development:
  primary:
    <<: *default
    database: users_development
    username: deploy_root
    password: password
    host: "localhost"
    migrations_paths: db/user_migrate
  primary_replica:
    <<: *default
    database: users_development
    username: deploy_readonly
    password: password
    host: "localhost"
    replica: true

has defined as its main AR defined as:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to database: { writing: :primary, reading: :primary_replica }
end

However, when The following commands are run following the suggested syntax by the rails guides

rails generate devise User nick avatar --database users
bin/rails db:migrate

the only response is a prompt. Two problems arise:

  1. the migration is created but not in the proper directory migrations_paths: db/user_migrate

  2. Logging into the database users_development=# \dt returns, consistently with the prompt reply Did not find any relations. In other words the table was not created (which is confirmed by the schema.rb file being unaltered

is specifying migrations_paths with a sub directory a mistake for the primary database connection?
Or should rails generate devise User nick avatar --database users invoke primary in lieu of users?



Solution 1:[1]

The primary database is assumed to have its migrations in the migrate directory, not a sub-directory.

development:
  primary:
    <<: *default
    database: users_development
    username: deploy_root
    password: password
    host: "localhost"
  primary_replica:
    <<: *default
    database: users_development
    username: deploy_readonly
    password: password
    host: "localhost"
    replica: true

Running a rails generate with --database name_of_primary_database will proceed as in single database application.

Although, this observer would have enjoyed that all migrations be organised in a similar manner, migrations_paths is not allowed for a primary database.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jerome