多个数据库连接:schema_migrations是看在错误的数据库 [英] Multiple database connections: schema_migrations is looked up in the wrong database

查看:623
本文介绍了多个数据库连接:schema_migrations是看在错误的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的一些在下面的方式进行迁移的一个辅助数据库连接:

I am trying to use a secondary database connection for some of my migrations in the following way:

# 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连接。

In my database.yml the staging_db connection is configured.

当我运行耙分贝:迁移,该表foo是在staging_db模式正确创建,桌子schema_migrations是在RAILS_ENV =发展连接创建。然而分贝:迁移报告以下错误(这失败以后的迁移):

When I run rake db:migrate, the table foo is created correctly in the staging_db schema, and the table schema_migrations is created in the RAILS_ENV=development connection. However db:migrate reports the following error (which fails subsequent migrations):

表staging_db.schema_migrations   不存在

Table 'staging_db.schema_migrations' doesn't exist

有没有办法告诉分期::移民去寻找schema_migrations表在当前RAILS_ENV连接?

Is there a way to tell Staging::Migration to look for the schema_migrations table in the current RAILS_ENV connection?

顺便说一句,我知道一个事实,即staging_db是那么不RAILS_ENV感知。这是对我很好,因为每个服务器都有通过一个单独的database.yml这是不是在我的回购配置的环境。

BTW, I am aware of the fact that staging_db is then not RAILS_ENV-aware. This is fine for me since every server has its environment configured through a separate database.yml which is not in my repo.

推荐答案

您应该尝试在staging_db你的第一个迁移之前这样做:

You should try do this before your first migration in the staging_db:

ActiveRecord::Base.connection.initialize_schema_migrations_table

这将在临时数据库创建模式迁移表。如果这不是你想要的,你将不得不处理一些其他的事情。该schema_migrations_table_name确定哪些表包含迁移版本:

This will create a schema migration table in the staging db. If this is not what you want you will have to manipulate some other things. The schema_migrations_table_name determines which table contains the migration versions:

def schema_migrations_table_name
  Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
end

因此​​,如果你定义了一个table_name_ preFIX会造成schema_migration_table看在分期分贝。

So if you have a table_name_prefix defined it will cause the schema_migration_table to look in the staging db.

这篇关于多个数据库连接:schema_migrations是看在错误的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆