2017-09-14 54 views
11

我有一个应用程序,它使用基于子域的不同数据库。所以基本上,模式将是相同的,但数据会因数据库而异。但是,当我发布一些新功能并需要对模式进行一些更改时,我需要运行一个可在shards.yml中配置的所有数据库上运行的命令。如何做rails db:一次在rails上迁移多个不是主从关系的分片?

的database.yml

default: &default 
    adapter: postgresql 
    encoding: unicode 
    pool: 15 
    host: localhost 
    port: 5432 
    username: postgres 
    password: 

development: 
    <<: *default 
    database: app_default 
production: 
    <<: *default 
    database: app_default 
    username: <%= ENV['BACKEND_DATABASE_USERNAME'] %> 
    password: <%= ENV['BACKEND_DATABASE_PASSWORD'] %> 

shards.yml

shared: &shared 
    adapter: postgresql 
    encoding: unicode 
    pool: 15 
    host: localhost 
    username: postgres 
    password: 
    port: 5432 

octopus: 
    environments: 
    - development 
    - test 
    - production 
    development: 
    default: 
     <<: *shared 
     database: app 
    first: 
     <<: *shared 
     database: first 
    second: 
     <<: *shared 
     database: second 
    .... 
    test: 
    test: 
     host: postgres 
     adapter: postgresql 
     database: app_test 
    production: 
    default: 
     <<: *shared 
     database: app 
    first: 
     <<: *shared 
     database: first 
    second: 
     <<: *shared 
     database: second 
    .... 

我使用八达通设置基于子域的碎片,工作正常。我有的问题是:

  1. 我不能做rails db:reset。收到错误ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: cannot drop the currently open database
  2. 我不能这样做rails db:migrate将迁移的所有数据库

回答

4

您必须添加using到您的迁移

class CreateComments < ActiveRecord::Migration 
    using :first, :second 

    def change 
    create_table :comments do |t| 
     t.belongs_to :page, index: true 
     t.text :body 

     t.timestamps 
    end 
    end 
end 

上述迁移将在两个firstsecond

运行
+0

感谢您花时间回答问题。不幸的是,这个解决方案仍然不能解决'rails db:reset'错误。虽然这可能适用于指定的碎片,但我正在寻找适用于所有碎片的迁移。 –