gpt4 book ai didi

ruby-on-rails - 多个数据库连接 : schema_migrations is looked up in the wrong database

转载 作者:行者123 更新时间:2023-12-04 07:32:53 27 4
gpt4 key购买 nike

我正在尝试通过以下方式为我的某些迁移使用辅助数据库连接:

# app/models/staging/migration.rb
class Staging::Migration < ActiveRecord::Migration
def self.connection
ActiveRecord::Base.establish_connection(:staging_db).connection
end
end

# db/migrate/<timestamp>_create_foo.rb
class CreateFoo < Staging::Migration
....
end

在我的 database.yml 中配置了 staging_db 连接。

当我运行 rake db:migrate 时,在 staging_db 模式中正确创建了表 foo,在 RAILS_ENV=development 连接中创建了表 schema_migrations。但是 db:migrate 报告以下错误(后续迁移失败):

Table 'staging_db.schema_migrations' doesn't exist



有没有办法告诉 Staging::Migration 在当前 RAILS_ENV 连接中查找 schema_migrations 表?

顺便说一句,我知道 staging_db 不是 RAILS_ENV-aware 的事实。这对我来说很好,因为每个服务器都通过一个单独的 database.yml 配置了它的环境,这不在我的仓库中。

最佳答案

您应该在第一次迁移 staging_db 之前尝试执行此操作:

ActiveRecord::Base.connection.initialize_schema_migrations_table

这将在暂存数据库中创建一个架构迁移表。如果这不是您想要的,您将不得不操纵其他一些东西。 schema_migrations_table_name 确定哪个表包含迁移版本:
def schema_migrations_table_name
Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
end

因此,如果您定义了 table_name_prefix,它将导致 schema_migration_table 在暂存数据库中查找。

关于ruby-on-rails - 多个数据库连接 : schema_migrations is looked up in the wrong database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6631265/

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