gpt4 book ai didi

ruby-on-rails - 如何做 rails db :migrate on multiple shards that are not master slave relationship at once on rails?

转载 作者:行者123 更新时间:2023-12-04 02:18:23 25 4
gpt4 key购买 nike

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

数据库.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 基于子域设置分片,效果很好。我遇到的问题是:
  • 我做不到 rails db:reset .收到错误 ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: cannot drop the currently open database
  • 我做不到 rails db:migrate将迁移到所有数据库
  • 最佳答案

    您必须添加 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

    上述迁移将在 first 上运行和 second

    关于ruby-on-rails - 如何做 rails db :migrate on multiple shards that are not master slave relationship at once on rails?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46220136/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com