如何做rails db:在rails上同时迁移多个非主从关系的分片?

时间:2017-09-14 13:12:25

标签: ruby-on-rails sharding octopus

我有一个应用程序,它使用基于子域的不同数据库。基本上,模式将是相同的,但每个数据库的数据会有所不同。但是当我发布一些新功能并且需要进行一些架构更改时,我需要运行一个可以在shards.yml中配置的所有数据库上运行的命令。

的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
    ....

我正在使用Octopus根据子域设置分片,这很好用。我遇到的问题是:

  1. 我做不到rails db:reset。得到错误ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: cannot drop the currently open database
  2. 我无法执行将在所有数据库上迁移的rails db:migrate

1 个答案:

答案 0 :(得分:5)

您必须在迁移中添加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

上运行