简体   繁体   中英

How can I use rake to create databases when multiple databases exist within the database.yml config file?

How can I format the rake db:create:all RAILS_ENV=development command in a way that will capture all databases in my config file?

# config/database.yml
db1:
  development:
    adapter: mysql2
    host: 127.0.0.1
    database: db1
    username: user
    password: pass
  test:
    .
    .
db2:
  development:
    adapter: mysql2
    host: 127.0.0.1
    database: db2
    username: user
    password: pass
  test:
    .
    .
db3:
  development:
    adapter: mysql2
    host: 127.0.0.1
    database: db3
    username: user
    password: pass
  test:
    .
    .

When using rake db:create:all I am expecting for all of the databases (db1, db2, db3) to be created, but only db1 is created.

Multiple database support will be a new feature in Rails 6, which is not released yet. See https://weblog.rubyonrails.org/2019/1/18/Rails-6-0-Action-Mailbox-Action-Text-Multiple-DBs-Parallel-Testing/

The YAML structure in database.yml would look a little different from yours - see https://github.com/rails/rails/pull/33877#issuecomment-422150252

For rails6, you can provide more than one connections and specify databases for each connection, eg:- database.yml

  adapter: postgresql
  encoding: unicode
  username: username
  password: password
  pool: 5
  host: localhost

development:
  primary:
    <<: *default
    database: database1
    adapter: postgresql
  secondary:
    <<: *default
    database: database2
    adapter: postgresql

You can do rails db:create for creating both database1 and database2

You can do rails db:create:primary for creating only database1(since it comes under primary connection).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM