laravel 在 laravel5 中回滚特定的迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44099885/
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
Rollback a specific migration in laravel5
提问by Mansoor Ahmad
I need to rollback a specific table as I forgot to mention foreign key. and I dont want my data loss from all tables.
我需要回滚一个特定的表,因为我忘了提到外键。我不希望我的数据从所有表中丢失。
My framework is Laravel 5.4
我的框架是 Laravel 5.4
Thanks in Advance Everyone
提前谢谢大家
回答by Alexander Kim
I do it this way:
我这样做:
- Deleting a table manually;
- Going to "migrations" table and deleting corresponding row to your migration;
php artisan migrate
;- done
- 手动删除表;
- 转到“迁移”表并删除迁移的相应行;
php artisan migrate
;- 完毕
For example, if you modified migration called "Settings", then you delete this table from DB and migrations table, then rerun artisan command.
例如,如果您修改了名为“设置”的迁移,那么您从数据库和迁移表中删除此表,然后重新运行 artisan 命令。
回答by peterm
Migrations are meant to be applied and rolled back in a specific order. Therefore there is no "proper" way of reapplying an arbitrary migration.
迁移旨在按特定顺序应用和回滚。因此,没有重新应用任意迁移的“正确”方法。
In any case there are at least two options:
无论如何,至少有两个选择:
"Fail forward" - create a subsequent migration that creates a necessary FK and apply it. This is the only proper way if you are already in production.
If you're just in early stages of development and don't won't to bloat the migrations directory you can
- dump the tables so that you preserve the data
- rollback up to this particular migration
- fix and test the migration
- migrate
- load the data from dumps
“故障转移” - 创建后续迁移,创建必要的 FK 并应用它。如果您已经在生产中,这是唯一正确的方法。
如果您只是处于开发的早期阶段并且不会膨胀迁移目录,您可以
- 转储表,以便您保留数据
- 回滚到此特定迁移
- 修复和测试迁移
- 迁移
- 从转储加载数据
回答by Hajopa
You can do
你可以做
php artisan migrate:rollback --path=/database/migrations/your_file.php
php artisan migrate:rollback --path=/database/migrations/your_file.php
回答by Sandeesh
Unfortunately there isn't an option to rollback a migration per table basis. You can only rollback the latest migration or last few migrations using the step
parameter.
不幸的是,没有选项可以回滚每个表的迁移。您只能使用该step
参数回滚最近的迁移或最后几次迁移。
But there is a hacky way to do it in case you really need to. You can set the batch
value in the migrations table to a higher number than the most recent migration for only the migrations you want to rollback. With this when you call php artisan migrate:rollback
, only that particular migration files with batch
value altered would rollback.
但是,如果您真的需要,有一种很酷的方法可以做到这一点。您可以batch
仅针对要回滚的迁移将迁移表中的值设置为比最近迁移更高的数字。有了这个,当您调用 时php artisan migrate:rollback
,只有batch
更改了值的特定迁移文件才会回滚。