Ruby-on-rails 如何删除 Rails 3 中的迁移文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3872586/
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
How to delete migration files in Rails 3
提问by alvincrespo
I would like to remove/delete a migration file. How would I go about doing that? I know there are similar questions on here but as an update, is there a better way than doing script/destroy?
我想移除/删除迁移文件。我该怎么做?我知道这里有类似的问题,但作为更新,有没有比执行脚本/销毁更好的方法?
Also, should I do a db:resetor db:dropif I remove/delete a migration?
另外,我应该执行db:reset还是db:drop删除/删除迁移?
回答by Fábio Batista
I usually:
我通常:
- Perform a
rake db:migrate VERSION=XXXon all environments, to the version before the one I want to delete. - Delete the migration file manually.
- If there are pending migrations (i.e., the migration I removed was not the last one), I just perform a new
rake db:migrateagain.
rake db:migrate VERSION=XXX对所有环境执行 a到我要删除的版本之前的版本。- 手动删除迁移文件。
- 如果有待处理的迁移(即,我删除的迁移不是最后一个),我只需
rake db:migrate再次执行新的迁移。
If your application is already on production or staging, it's safer to just write another migration that destroys your table or columns.
如果您的应用程序已经在生产或暂存中,那么编写另一个会破坏您的表或列的迁移会更安全。
Another great reference for migrations is: http://guides.rubyonrails.org/migrations.html
迁移的另一个重要参考是:http: //guides.rubyonrails.org/migrations.html
回答by Gediminas
Another way to delete the migration:
删除迁移的另一种方法:
$ rails d migration SameMigrationNameAsUsedToGenerate
Use it before rake db:migrateis executed because changes in database will stay forever :) - or remove changes manually
在rake db:migrate执行之前使用它,因为数据库中的更改将永远保留:) - 或手动删除更改
回答by fOx
Run below commands from app's home directory:
从应用程序的主目录运行以下命令:
rake db:migrate:down VERSION="20140311142212"(here version is the timestamp prepended by rails when migration was created. This action will revert DB changes due to this migration)Run "rails destroy migration migration_name"(migration_name is the one use chose while creating migration. Remove "timestamp_" from your migration file name to get it)
rake db:migrate:down VERSION="20140311142212"(这里的 version 是创建迁移时由 rails 加上的时间戳。此操作将还原由于此迁移而导致的数据库更改)Run "rails destroy migration migration_name"(migration_name 是创建迁移时选择的一种用途。从迁移文件名中删除“ timestamp_”以获得它)
回答by Vicky
You can also run a down migration like so:
您还可以像这样运行向下迁移:
rake db:migrate:down VERSION=versionnumber
Refer to the Ruby on Rails guideon migrations for more info.
有关更多信息,请参阅有关迁移的Ruby on Rails 指南。
回答by Agnes
We can use,
我们可以用,
$ rails d migration table_name
Which will delete the migration.
这将删除迁移。
回答by frenesim
Sometimes I found myself deleting the migration file and then deleting the corresponding entry on the table schema_migrations from the database. Not pretty but it works.
有时我发现自己删除了迁移文件,然后从数据库中删除了表 schema_migrations 上的相应条目。不漂亮,但它有效。
回答by random_user_0891
This also works in Rails 5.
这也适用于 Rails 5。
If the migration was the most recent one you can remove the database column(s) that the migration added by doing:
如果迁移是最新的,您可以通过执行以下操作删除迁移添加的数据库列:
rake db:rollback
then remove the migration file itself by running:
然后通过运行删除迁移文件本身:
rails d migration WhateverYourMigrationWasNamed.rb
回答by stackPusher
None of these answers quite fit the problem i had as the migration i wanted to delete was missing:
I had created and run a migration in some other branch, which was then discarded. The problem is when a migration is run, rails adds the version into a schema_migrationstable in the database. So even if it isn't listed in your db structure or schema, rails looks for it.
You can reveal these orphaned migrations by running:
这些答案都不太适合我遇到的问题,因为我想删除的迁移丢失了:我已经在其他分支中创建并运行了迁移,然后将其丢弃。问题是在运行迁移时,rails 会将版本添加到schema_migrations数据库中的表中。因此,即使它未在您的数据库结构或模式中列出,rails 也会查找它。您可以通过运行来显示这些孤立的迁移:
rails db:migrate:status
rails db:migrate:status
Note the versions of the missing migrations and head into the db console:
注意缺少迁移的版本并进入数据库控制台:
rails dbconsole
rails dbconsole
Now remove the versions from the migration table manually:
现在手动从迁移表中删除版本:
delete from schema_migrations where version='<version>';
delete from schema_migrations where version='<version>';
You should now be good.
你现在应该很好。
回答by stackPusher
Look at 4.1 Rolling Back
看4.1回滚
http://guides.rubyonrails.org/migrations.html
http://guides.rubyonrails.org/migrations.html
$ rake db:rollback
$耙分贝:回滚
回答by Miguel Alatorre
I just had this same problem:
我刚刚遇到了同样的问题:
- rails d migration fuu-this deleted the migration with the last timestamp
- rails d migration fuu-this deleted the other migration
- use git status to check that is not on the untracked files anymore
- rails g migration fuu
- rails d migration fuu- 这删除了具有最后时间戳的迁移
- rails d migration fuu- 这删除了另一个迁移
- 使用 git status 检查它不再位于未跟踪的文件中
- rails g 迁移 fuu
That fixed it for me
那为我修好了

