简体   繁体   English

从heroku导入postgres后,rails会强制在本地运行数据库迁移

[英]after postgres import from heroku, rails locally imposes to run database migration

I'm trying to import my database from heroku back to local. 我正在尝试将我的数据库从heroku导入回本地。 Following the commands on heroku , I can import the entire database locally. 按照heroku上的命令,我可以在本地导入整个数据库。

However, Rails is answering I have a pending migration and that I should run bin/rails db:migrate RAILS_ENV=development . 但是,Rails回答了我有一个待处理的迁移,我应该运行bin/rails db:migrate RAILS_ENV=development That fails because the tables already exist. 失败是因为表已经存在。

On heroku: 在heroku上:

heroku pg:info --app my_app
 ›   Warning: heroku update available from 7.22.7 to 7.24.4.
=== DATABASE_URL
Plan:                  Hobby-dev
Status:                Available
Connections:           0/20
PG Version:            11.2

Localy: Localy:

psql (PostgreSQL) 11.3

ActiveRecord::Migrator.current_version returns => 20190608070435 and db/schema.rb contains: ActiveRecord::Migrator.current_version返回=> 20190608070435,并且db/schema.rb包含:

ActiveRecord::Schema.define(version: 2019_06_10_061746)

Replacing that version with the one given by ActiveRecord::Migrator.current_version also doesn't help. 用ActiveRecord :: Migrator.current_version给出的版本替换该版本也无济于事。

Don't know where this problem comes from. 不知道这个问题是从哪里来的。 After import from Heroku, I have the correct database locally, but rails sees a pending migration. 从Heroku导入后,我在本地具有正确的数据库,但是Rails看到了挂起的迁移。

Rails uses schema_migrations table to keep a track of migrations. Rails使用schema_migrations表来跟踪迁移。 This issue can happen when a corresponding migration record is missing and rails tries to run the migration. 当缺少相应的迁移记录并且Rails尝试运行迁移时,可能会发生此问题。 But it fails since you already have the table in your local DB. 但这失败了,因为您已经在本地数据库中拥有了该表。

If you are sure that you have the latest / up to date database locally, one thing you can do is to run the migration, but commenting out the code (that is in the migration). 如果您确定本地拥有最新的数据库,则可以做的一件事情是运行迁移,但是注释掉代码(即在迁移中)。 NOTE: make sure you only comment on the table creation code 注意:确保仅对表创建代码进行注释

Eg 例如

class <your migraton name> < ActiveRecord::Migration[4.2]
  def change
    # commented out code
  end
end

That way Rails will update the schema_migrations table the correct version and will not nag you again (Rails assumes you've created the table). 这样,Rails就会将schema_migrations表更新为正确的版本,并且不会再schema_migrations您(Rails假设您已经创建了该表)。

Then uncomment the migration file. 然后取消注释迁移文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM