Migrate, rollback or refresh for specific migration files in the Laravel framework.

Migrate, rollback or refresh for specific migration files in the Laravel framework.

README

GitHub version
Latest Version on Packagist
GitHub issues
GitHub license
Total Downloads

MigrateSpecific is a Laravel framework Artisan CLI extension command that helps you easily perform database migrations of specific migration files.

Requirement

laravel/framework v5.7 or later.

Installation

Run command in your Laravel project root directory:

composer require caloskao/migrate-specific

Register command at app/Console/Kernel.php :

protected $commands = [
    CalosKaoMigrateSpecific::class
];

Now, run php artisan , you can see migrate:specific in the migrate section:

 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, reset or rollback for specific migration files.
  migrate:status       Show the status of each migration

Usage

You can run php artisan help migrate:specific to check command usage:

Description:
  Migrate, rollback or refresh for specific migration files.

Usage:
  migrate:specific [options] [--] [<files?*>]

Arguments:
  files?*                        File or directory path, support multiple file. (Sperate by space) [default: "database/migrations"]

Options:
      --database                 The database connection to use
  -f, --skip-foreign-key-checks  Set FOREIGN_KEY_CHECKS=0 before migrate.
  -k, --keep-batch               Keep batch number. (Only works in refresh mode)
  -m, --mode[=MODE]              Set migrate execution mode, supported mode have: default, refresh [default: "default"]
  -y, --assume-yes               Automatically assumes "yes" to run commands in non-interactive mode. This option is automatically enabled if you use the 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

Basic usage

Migrate single file or directory:

php artisan migrate:specific <path>

Migrate mutiple files:

php artisan migrate:specific <file 1> <file 2> <file 3> ...

Output is like below:

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

Migrate mode

Use option -m or --mode to run migrate:refresh or migrate:rollback for specific migrations.

# Rollback mode
php artisan migrate:specific -m rollback <files>

# Refresh mode
php artisan migrate:specific -m refresh <files>

Keep batch in refresh mode

Use option -k or --keep-batch to keep migration batch.

Before migrate:

+——+——————————————————-+——-+
| Ran? | Migration | Batch |
+——+——————————————————-+——-+
| Yes | 2019_02_14_011711_create_password_resets_table | 1 |
| Yes | 2019_02_14_011711_create_users_table | 2 |
+——+——————————————————-+——-+

php artisan migrate:specific -m refresh -k database/migrations/2019_02_14_011711_create_password_resets_table.php

Run php artisan migrate:status after migrate, you can see the migration 2019_02_14_011711_create_password_resets_table batch number will not be changed.

Skip foreign key checks

If your pattern has foreign key constraints, sometimes you might get SQL error 1451:

Rolling back: 2019_02_14_011711_create_users_table
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table `users`)

Use option -f of --skip-foreign-key-checks to execute database statement SET FOREIGN_KEY_CHECKS=0 before migrate.

Note: A good practice is to rollback related foreign key migration at the same time, otherwise you may still get other errors, such as SQL Error 1091.

Rolling back: 2019_02_14_011711_create_users_table
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP FOREIGN KEY `fk_projects_users_1`; check that it exists (SQL: alter table `reports` drop foreign key `fk_projects_users_1`)

Using MigrateSpecific in non-interactive mode

Sometimes we need to perform a database migration many times, or we need to deploy it into an automated process. At this time, we can use the option -y to directly perform database migration without confirmation.

php artisan migrate:specific -y <files>

Note:

  • If you call the option -n or -q, MigrateSpecific will be automatically enable option -y.
  • If you are not using MigrateSpecific in the above situations, we recommend that you do not perform database migration in non-interactive mode to avoid accidental loss of data.

License

The Migrate Specific extension is open-sourced software licensed under the MIT license.

Source

Add a Comment

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *