當我們將 migration file 遷移到資料庫後,日後想要直接修改原先的 migration file 而不是新增一個去做結構變更時,可以透過 migrate:refresh
重新遷移,但是如果前一次遷移包含多個檔案時,這些資料表都會受到影響;若想要修改的資料表不屬於上一個批次(batch),透過 --step
選項去做遷移,影響範圍就會更大。
常見的做法是建立一個臨時目錄,將 migration file 放進去後執行遷移,但是這個做法僅適用於第一次遷移;另一種方式是修改資料表 migrations 的批次號碼,讓指定的 migration file 成為上一個批次。
這些都是臨時性做法,若是有很多批要修改就會很麻煩,所以我就寫了一個小工具,之後用一行指令就可以解決這個問題。
Package Name: MigrateSpecific
Version: 1.2.1
Site: https://github.com/caloskao/migrate-specific
安裝
在 Laravel 專案目錄下執行:
1
|
composer require caloskao/migrate-specific
|
修改 app/Console/Kernel.php
,註冊套件:
1
2
3
|
protected $commands = [
\CalosKao\MigrateSpecific::class
];
|
執行 php artisan
,如果有看到 migrate:specific
代表註冊成功:
1
2
3
4
5
6
7
8
|
migrate
migrate:fresh Drop all tables and re-run all migrations
migrate:install Create the migration repository
migrate:refresh Reset and re-run all migrations
migrate:reset Rollback all database migrations
migrate:rollback Rollback the last database migration
migrate:specific Migrate, refresh or reset for specific database migration files.
migrate:status Show the status of each migration
|
執行
執行 php artisan help migrate:specific
可以查看詳細用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
Description:
Easily execute database migration of specific files in the Laravel framework.
Usage:
migrate:specific [options] [--] <files>...
Arguments:
files File path, support multiple file (Sperate by space).
Options:
-m, --mode[=MODE] Set migrate exection mode, supported mode have: default, refresh, rollback, new-batch [default: "default"]
-y, --assume-yes Automatic yes to prompts; assume "yes" as answer to all prompts and run non-interactively. The process will be automatic assume yes as answer when you used option "-n" or "-q".
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--env[=ENV] The environment the command should run under
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
|
基本操作
遷移指定檔案
1
|
php artisan migrate:specific database/migrations/table.php
|
也可以同時遷移多個
1
|
php artisan migrate:specific database/migrations/2014_10_12_000000_create_users_table.php /home/caloskao/2018*
|
輸出大概長這樣
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
MigrateSpecific v1.2.1
Copyright (C) 2018 by CalosKao
If you have any problem or bug about the use, please come to Github to open the question.
https://github.com/caloskao/migrate-specific
The following migration files will be migrated:
2014_10_12_000000_create_users_table.php
2018_07_31_174401_create_jobs_table.php
2018_07_31_185911_create_failed_jobs_table.php
Is this correct? (yes/no) [no]:
> yes
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2018_07_31_174401_create_jobs_table
Migrated: 2018_07_31_174401_create_jobs_table
Migrating: 2018_07_31_185911_create_failed_jobs_table
Migrated: 2018_07_31_185911_create_failed_jobs_table
|
遷移模式
可以透過參數 -m 指定要怎麼進行遷移,目前可以支援 migrate:refresh
和 migrate:reset
兩種,執行上跟 Laravel 預設的行為相同,差別指在於可以指定檔案。
Refresh 模式:
1
|
php artisan migrate:specific -m refresh /path/to/migration.php
|
Reset 模式:
1
|
php artisan migrate:specific -m reset /path/to/migration.php
|
非互動式遷移
有時我們需要執行多次遷移,或者需要把指令部署到自動化流程中。 這時候我們可以使用選項 -y
直接執行遷移而無需確認。(使用選項 -n
或選項 -q
也會觸發選項 -y
)
1
|
php artisan migrate:specific -y /path/to/migration.php
|