php laravel 4 artisan——如何回滚到特定的迁移状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17697247/
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
laravel 4 artisan -- how to rollback to a specific migration state?
提问by do.
Say i got a.php
, b.php
, c.php
and d.php
migration classes files. How to rollback to a specific migration state, the state defined within b.php
for example, with artisan command ?
说我有a.php
,b.php
,c.php
和d.php
移民类文件。如何回滚到特定的迁移状态,b.php
例如使用 artisan 命令定义的状态?
回答by Andreyco
I am afraid, you cannot do this directly.
恐怕,你不能直接这样做。
You can: 1, Rollback The Last Migration Operation (all migrations ran in the last batch)
您可以: 1、回滚上次迁移操作(所有迁移都在最后一批中运行)
php artisan migrate:rollback
2, Rollback all migrations
2、回滚所有迁移
php artisan migrate:reset
3, Rollback all migrations and run them all again
3,回滚所有迁移并再次运行它们
php artisan migrate:refresh
php artisan migrate:refresh --seed
In your situation, modify b.php and it's up()
method, then execute artisan migrate:refresh
command.
在你的情况下,修改 b.php 及其up()
方法,然后执行artisan migrate:refresh
命令。
回答by Paul Cristea
There's a way to hack it by manually editing the database. In the migrations
table change the batch
column by giving the last migration a different batch number. Be aware that they are in increasing order, so edit accordingly. This tracks which migrations were applied separately.
有一种方法可以通过手动编辑数据库来破解它。在migrations
表中,batch
通过为上次迁移提供不同的批次号来更改列。请注意,它们的顺序是递增的,因此请相应地进行编辑。这会跟踪单独应用了哪些迁移。
Then run artisan:rollback
and it will undo the last "batch".
然后运行artisan:rollback
,它将撤消最后一个“批处理”。
So if you want to separate them all, then start from the top and give each 1,2,3,4,5 and so on... You can see that it is easily scriptable, and you can make an artisan command if you wish to separate all your migrations.
所以如果你想把它们全部分开,那么从顶部开始,给每个 1,2,3,4,5 等等......你可以看到它很容易编写脚本,如果你可以制作一个工匠命令希望分离您的所有迁移。
回答by junjet trasmonte
In my experience. I never do migrate:rollback. I would usually create another migration that does all the changes i need to "undo/rollback" the previous migrations.
在我的经验中。我从不迁移:回滚。我通常会创建另一个迁移来完成我需要“撤消/回滚”以前的迁移的所有更改。
This way you can be flexible if you want to rollback 2-x steps back, you can just create a new migration to effect the changes you want and then run the new migration by php artisan migrate.
通过这种方式,如果您想回滚 2-x 步,您可以很灵活,您只需创建一个新的迁移来实现您想要的更改,然后通过 php artisan migrate 运行新的迁移。
回答by Thiago Mata
In fact, there is not this feature (yet). surprisingly
事实上,没有这个功能(还)。出奇
The best idea, is create a new file backtob.php and make its up call the down of your other migrate files. To avoid copy and paste, you can do something like this:
最好的主意是创建一个新文件 backtob.php 并使其调用其他迁移文件的 down。为避免复制和粘贴,您可以执行以下操作:
class BacktoB {
public function up () {
// the database is in the after D state //
$migrateD = new D();
$migrateD->down();
// the database is in the after C state //
$migrateC = new C();
$migrateC->down();
// the database is in the before C state //
// before C = B //
}
public function down () {
// the database is in the B state //
$migrateC = new C();
$migrateC->up();
// the database is in the after C state //
$migrateD = new D();
$migrateD->up();
// the database is in the after D state //
}
}
As you can see, you can create the up and down calling the up and down of those migrations what you want to revert.
如您所见,您可以创建 up 和 down 调用您想要恢复的那些迁移的 up 和 down。
It is not the ideal, but it is what we can do.
这不是理想的,但这是我们可以做的。
回答by Yauheni Prakopchyk
回答by Laurence
If you reallywanted to - you could write a custom function that queries the migrations table, looks for the file you are after, and works out how many times to roll back - then does a loop of 'migrate:rollback' until you reach the required migration...
如果您真的想要 - 您可以编写一个自定义函数来查询迁移表,查找您要查找的文件,并计算回滚的次数 - 然后执行“迁移:回滚”循环,直到您到达需要迁移...
回答by mper
There is an easy yet dirty way:
有一个简单但肮脏的方法:
If you have migrations a.php
, b.php
and c.php
and want to rollback c
and b
, you can simply modify a.php
in such a way that there will be a syntax error... drop a semi-colon or something.
如果您有 migrations a.php
,b.php
并且c.php
想要回滚c
and b
,您可以简单地a.php
以这样一种方式进行修改,即会出现语法错误......删除分号或其他东西。
So, when you run php artisan migrate:rollback
it will rollback both c
and b
and stop with an error in a
. From then on, the rollback of c
and b
will be considered the last migration operation.
所以,当你运行php artisan migrate:rollback
它会回滚都c
和b
同一个错误和停止a
。从此,回滚c
和b
将被视为最后的迁移操作。
Don't forget to fix whatever error you made on purpose in a.php
.
不要忘记修复您在a.php
.
回答by HeadAndTail
Use the php artisan migrate:rollback command.
使用 php artisan migrate:rollback 命令。
php artisan migrate:rollback
To see what rollback will do, use the --pretend option.
要查看回滚会做什么,请使用 --pretend 选项。
php artisan migrate:rollback --pretend
You can also specify a database connection other than the default one.
您还可以指定默认连接以外的数据库连接。
php artisan migrate:rollback --pretend --database=other-one
回答by qatz
Since Laravel just provide php artisan migrate:rollback to rollback your migration script, the best way to rollback your selected migration script is to create a new migration script and put the script in your down method (on your selected migration script) to the newly created migration script. Hope this help.
由于 Laravel 仅提供 php artisan migrate:rollback 来回滚您的迁移脚本,因此回滚您选择的迁移脚本的最佳方法是创建一个新的迁移脚本并将该脚本放在您的 down 方法中(在您选择的迁移脚本上)到新创建的迁移脚本。希望这有帮助。