在 Laravel 中回滚一项特定的迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30287896/
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 one specific migration in Laravel
提问by cyber8200
I want
我想要
to rollback only :
仅回滚:
Rolled back: 2015_05_15_195423_alter_table_web_directories
Rolled back: 2015_05_15_195423_alter_table_web_directories
I run
我跑
php artisan migrate:rollback
, 3 of my migration are rolling back.
php artisan migrate:rollback
,我的3次迁移都在回滚。
Rolled back: 2015_05_15_195423_alter_table_web_directories
Rolled back: 2015_05_13_135240_create_web_directories_table
Rolled back: 2015_05_13_134411_create_contacts_table
I delete
我删除
both of my web_directories
and my contacts
table unintentionally. I never want that to happen, and if I can rollback only that specific one, this disaster will never happen.
我web_directories
和我的contacts
桌子都无意中。我从不希望这种情况发生,如果我只能回滚那个特定的,那么这场灾难将永远不会发生。
采纳答案by Martin Bean
If you look in your migrations
table, then you'll see each migration has a batch number. So when you roll back, it rolls back each migration that was part of the last batch.
如果您查看migrations
表格,您会看到每个迁移都有一个批号。因此,当您回滚时,它会回滚作为上一批一部分的每个迁移。
If you only want to roll back the very last migration, then just increment the batch number by one. Then next time you run the rollback
command, it'll only roll back that one migration as it's in a “batch” of its own.
如果您只想回滚最后一次迁移,则只需将批号加 1。然后,下次您运行该rollback
命令时,它只会回滚该迁移,因为它属于自己的“批处理”。
回答by Yauheni Prakopchyk
回答by PAUL KIARIE
Every time you rollback you get the last batch of migration. use the command
每次回滚时,您都会获得最后一批迁移。使用命令
php artisan migrate:rollback --step=1
回答by Nehal Hasnayeen
If you can't do what is told by @Martin Bean, then you can try another trick.
如果您不能按照@Martin Bean 所说的去做,那么您可以尝试另一个技巧。
Create a new migration and on that file in up() method insert what's in down() method of the migration you want to rollback and in down() method insert what's in up() method.
创建一个新的迁移,并在 up() 方法中的该文件上插入要回滚的迁移的 down() 方法中的内容,并在 down() 方法中插入 up() 方法中的内容。
e.g if your original migration is like this
例如,如果您的原始迁移是这样的
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id')->unsigned();
$table->string('name');
});
}
public function down()
{
Schema::drop('users');
}
then in new migration file do this
然后在新的迁移文件中执行此操作
public function up()
{
Schema::drop('users');
}
public function down()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id')->unsigned();
$table->string('name');
});
}
and then run the migrate, it will delete the table. and if you again want that back just rollback it.
然后运行迁移,它将删除表。如果你再次想要回滚它。
回答by Jignesh Joisar
better to used refresh migrate
最好使用刷新迁移
You may rollback & re-migrate a limited number of migrations by providing the step option to the refresh command. For example, the following command will rollback & re-migrate the last two migrations:
您可以通过为 refresh 命令提供 step 选项来回滚和重新迁移有限数量的迁移。例如,以下命令将回滚并重新迁移最后两次迁移:
php artisan migrate:refresh --step=2
otherwise used rollback migrate
否则使用回滚迁移
You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last three migrations:
您可以通过为 rollback 命令提供 step 选项来回滚有限数量的迁移。例如,以下命令将回滚最后三个迁移:
php artisan migrate:rollback --step=3
for more detail about migration see
有关迁移的更多详细信息,请参阅
回答by Deepak Thomas
Best way is to create a new migration and make required changes in that.
最好的方法是创建一个新的迁移并在其中进行所需的更改。
Worst case workaround (ifyou have access to DB plus you are okay with a RESET of that table's data):
最坏情况的解决方法(如果您有权访问 DB 并且您可以对该表的数据进行重置):
- Go to DB and delete/rename the migration entry for
your-specific-migration
- Dropthe table created by
your-specific-migration
- Run
php artisan migrate --path=/database/migrations/your-specific-migration.php
- 转到数据库并删除/重命名迁移条目
your-specific-migration
- 删除创建的表
your-specific-migration
- 跑
php artisan migrate --path=/database/migrations/your-specific-migration.php
This will force laravel to run that specific migration as no entry about it exists in Laravel's migration history
这将强制 Laravel 运行特定的迁移,因为 Laravel 的迁移历史中不存在关于它的条目
UPDATE: The Laravel way (Thanks, @thiago-valente)
更新:Laravel 方式(谢谢,@thiago-valente)
Run:
跑:
php artisan migrate:rollback --path=/database/migrations/your-specific-migration.php
php artisan migrate:rollback --path=/database/migrations/your-specific-migration.php
and then:
进而:
php artisan migrate
php artisan migrate
This will re-run that particular migration
这将重新运行该特定迁移
回答by Andre F.
It might be a little late to answer this question but here's a very good, clean and efficient way to do it I feel. I'll try to be as thorough as possible.
回答这个问题可能有点晚了,但我觉得这是一种非常好的、干净和有效的方法。我会尽量做到彻底。
Before creating your migrations create different directories like so:
在创建迁移之前创建不同的目录,如下所示:
database
|
migrations
|
batch_1
batch_2
batch_3
Then, when creating your migrations run the following command (using your tables as an example):
然后,在创建迁移时运行以下命令(以您的表为例):
php artisan make:migration alter_table_web_directories --path=database/migrations/batch_1
or
或者
php artisan make:migration alter_table_web_directories --path=database/migrations/batch_2
or
或者
php artisan make:migration alter_table_web_directories --path=database/migrations/batch_3
The commands above will make the migration file within the given directory path. Then you can simply run the following command to migrate your files via their assigned directories.
上面的命令将在给定的目录路径中生成迁移文件。然后,您只需运行以下命令即可通过指定的目录迁移文件。
php artisan migrate alter_table_web_directories --path=database/migrations/batch_1
*Note: You can change batch_1 to batch_2 or batch_3 or whatever folder name you're storing the migration files in. As long as it remains within the database/migrations directory or some specified directory.
*注意:您可以将batch_1 更改为batch_2 或batch_3 或您存储迁移文件的任何文件夹名称。只要它保留在database/migrations 目录或某个指定目录中。
Next if you need to rollback your specific migrations you can rollback by batch as shown below:
接下来,如果您需要回滚特定的迁移,您可以按如下所示批量回滚:
php artisan migrate:rollback --step=1
or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_1
or
或者
php artisan migrate:rollback --step=2
or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_2
or
或者
php artisan migrate:rollback --step=3
or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_3
Using these techniques will allow you more flexibility and control over your database(s) and any modifications made to your schema.
使用这些技术将使您能够更灵活地控制数据库以及对架构所做的任何修改。
回答by Parth kharecha
Rollback one step. Natively.
退一步。本机。
php artisan migrate:rollback --step=1
Rollback two step. Natively.
回滚两步。本机。
php artisan migrate:rollback --step=2
回答by Raghu Aryan
If you want to rollback last migration.
如果要回滚上次迁移。
php artisan migrate:rollback
If you want to rollback specific migration then go to migration table and set highest value of that record in batch. Then.
如果要回滚特定迁移,请转到迁移表并批量设置该记录的最大值。然后。
php artisan migrate:rollback
Currently i'm working on laravel 5.8 if not working any other version of laravel please inform to me.
目前我正在使用 laravel 5.8 如果不能使用任何其他版本的 laravel 请通知我。
回答by Shah-Kodes
Use command "php artisan migrate:rollback --step=1" to rollback migration to 1 step back.
使用命令“php artisan migrate:rollback --step=1”将迁移回滚到1步。
For more info check the link :- https://laravel.com/docs/master/migrations#running-migrations
有关更多信息,请查看链接:- https://laravel.com/docs/master/migrations#running-migrations