【问题标题】:How to do rails db:migrate on multiple shards that are not master slave relationship at once on rails?如何在rails上一次在多个不是主从关系的分片上执行rails db:迁移?
【发布时间】:2018-02-23 11:58:21
【问题描述】:

我有一个应用程序,它根据子域使用不同的数据库。所以本质上,架构是相同的,但每个数据库的数据会有所不同。但是当我发布一些新功能并且需要一些架构更改时,我需要运行一个可以在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 会迁移到所有数据库

【问题讨论】:

    标签: ruby-on-rails sharding octopus


    【解决方案1】:

    您必须将 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 上运行

    【讨论】:

    • 感谢您抽出宝贵时间回答问题。不幸的是,此解决方案仍然无法解决 rails db:reset 错误。虽然这可能只适用于指定的分片,但我一直在寻找要应用于所有分片的迁移。
    猜你喜欢
    • 2012-11-18
    • 2021-10-29
    • 2010-10-06
    • 1970-01-01
    • 2021-11-22
    • 2017-04-14
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多