首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在rails上执行rails db:在不是主从关系的多个碎片上迁移?

如何在rails上执行rails db:在不是主从关系的多个碎片上迁移?
EN

Stack Overflow用户
提问于 2017-09-14 13:12:25
回答 1查看 1.2K关注 0票数 11

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

database.yml

代码语言:javascript
复制
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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-09-20 09:57:24

您必须在迁移中添加using

代码语言:javascript
复制
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上。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46220136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档