Ruby-on-rails 如何再次运行迁移而不删除所有较新的迁移?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16981071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:08:03  来源:igfitidea点击:

How do I run a migration again, without deleting all the newer migrations?

ruby-on-railsrubydeviserake

提问by Kush

I had just installed devise so the table didn't have any data on it except one user (me).

我刚刚安装了设计,所以除了一个用户(我)之外,表格上没有任何数据。

I was re-doing the database all over again so I dropped it all. I did rails g scaffoldto generate 6 new models and controllers and did rake db:migrate

我正在重新做数据库,所以我把它全部删除了。我确实rails g scaffold生成了 6 个新模型和控制器rake db:migrate

In my /db/migratedirectory I have the devise file with the filename 20130603211907_devise_create_users.rb

在我的/db/migrate目录中,我有文件名的设计文件20130603211907_devise_create_users.rb

Here is the issue: If I do rake db:migrate:down VERSION=20130603211907it will delete all the new migrations.

这是问题:如果我这样做rake db:migrate:down VERSION=20130603211907,它将删除所有新迁移。

How do I run a migration again, without deleting all the newer migrations?

如何再次运行迁移而不删除所有较新的迁移?

采纳答案by davegson

If you are developing locally and it wouldn't hurt to remove all data from your models you could simply dropyour db and then create& migratefrom scratch:

如果在本地发展,它不会伤害到从模型中删除所有数据,你可以简单地drop你的数据库,然后createmigrate从头开始:

Purge or recreate a Ruby on Rails database

清除或重新创建 Ruby on Rails 数据库

回答by Sachin R

It will run the down and then the up step (This command can drop your table!):

它将运行向下然后向上的步骤(此命令可以删除您的表!):

rake db:migrate:redo VERSION=xxxxxxx

rake db:migrate:redo VERSION=xxxxxxx

To prevent your table from being deleted you could do this in conjunction with commenting out the downsteptemporarily.

为了防止您的表被删除,您可以结合临时注释掉该down步骤来执行此操作。

回答by Kush

Thanks for the help everyone. This is what worked for me:

谢谢大家的帮助。这对我有用:

WARNING: these commands will delete all data in your database!

警告:这些命令将删除数据库中的所有数据!

rake db:drop
rake db:create
rake db:migrate

回答by woltob

You can call rake db:migrate:redo VERSION=20130603211907which will rerun the specified version.

您可以调用rake db:migrate:redo VERSION=20130603211907which 将重新运行指定的版本。

Also, if you have migrations that should only run when going up the migration chain (e.g. data transformation, copying, etc.), you can specify a

此外,如果您的迁移应该只在迁移链上运行(例如数据转换、复制等),您可以指定一个

def up 
  do_something
end

and def down (going down), def change (both ways) method respectively.

和def down(下降),def改变(双向)方法分别。

To temporarily disable migrations, just, for example, rename the method def upto def up_and it will be ignored when processing the migration chain.

要暂时禁用迁移,只需将方法重命名def updef up_,在处理迁移链时将被忽略。

回答by bunty

rake db:migrate:up VERSION=20090408054532

this will migrate all file upto VERSION=20090408054532

这会将所有文件迁移到 VERSION=20090408054532

checkout Run a single migration file

checkout运行单个迁移文件