Laravel 5 - 跳过迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35115541/
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-14 13:08:56  来源:igfitidea点击:

Laravel 5 - skip migrations

phplaravel

提问by Erhnam

I have migrated an existing Laravel 5 application and database. Only no migrations table was there yet, so I created this with the following command:

我已经迁移了现有的 Laravel 5 应用程序和数据库。目前还没有迁移表,所以我使用以下命令创建了它:

php artisan migrate:install

Inside the database migration folders three files exist (2015_12_08_134409_create_tables_script.php, 2015_12_08_134410_create_foreign.php, 2015_12_08_134411_create_index.php)

在数据库迁移文件夹中存在三个文件(2015_12_08_134409_create_tables_script.php、2015_12_08_134410_create_foreign.php、2015_12_08_134411_create_index.php)

When using 'php artisan migrate' the scripts inside the migrations folder are executed again. This is what I want to avoid. Is it possible to insert records in Laravel's migrations table, so these scripts will be skipped and new scripts will be picked up once 'php artisan migrate' is executed again?

使用“php artisan migrate”时,迁移文件夹中的脚本将再次执行。这是我想要避免的。是否可以在 Laravel 的迁移表中插入记录,因此一旦 'php artisan migrate' 再次执行,这些脚本将被跳过并拾取新脚本?

回答by Sergio Guillen Mantilla

Once you have the migration table created, insert these records:

创建迁移表后,插入以下记录:

insert into migrations(migration, batch) values('2015_12_08_134409_create_tables_script',1);
insert into migrations(migration, batch) values('2015_12_08_134410_create_foreign',1);
insert into migrations(migration, batch) values('2015_12_08_134411_create_index',1);

So artisan will understand those migrations as 'executed'

所以工匠会将这些迁移理解为“已执行”

回答by Calos

Another way is just create a folder under database/migrationsto put you want skip migrations, this method works for both files that have been migrated or not yet migrated.

另一种方法是在下面创建一个文件夹database/migrations来放置您想要跳过的迁移,此方法适用于已迁移或尚未迁移的文件。

# Execute commands in laravel project root folder
mkdir database/migrations/ignored
mv database/migrations/2018_08_14_000000_should_ignore_migration.php \
   database/migrations/ignored/

# Check migration has been skipped
php artisan migrate:status


2018-09-25 update:

2018-09-25 更新:

This Artisan CLI extension commandis support single command to migrate specific files on Laravel 5.4 or later:

这个Artisan CLI 扩展命令支持单个命令在 Laravel 5.4 或更高版本上迁移特定文件:

# Install extension
composer require caloskao/migrate-specific

# Migrate
php artisan migrate:specific /path/to/migration_file.php