How to run migrations by schema name in Laravel

Migrations are like version control for your database, allowing your team to define and share the application’s database schema definition. Typically, migrations will use Schema facade to create and modify database tables and columns Laravel Documentation

In general, one would run all outstanding migrations by running the following command in the app root folder:

php artisan migrate

To view all which migrations have run so far one would do:

php artisan migrate:status

 +------+-------------------------------------------------------+-------+

| Ran? | Migration                                             | Batch |

+------+-------------------------------------------------------+-------+

| Yes  | 2014_10_12_000000_create_users_table                  | 1     |

| Yes  | 2014_10_12_100000_create_password_resets_table        | 1     |

| Yes  | 2019_08_19_000000_create_failed_jobs_table            | 1     |

| Yes  | 2019_12_14_000001_create_personal_access_tokens_table | 2     |

| Yes  | 2021_03_04_052554_create_referrals_table              | 1     |

+------+-------------------------------------------------------+-------+

To run only a migration for specific Schema you may specify path to the schema folder. For example, I have got a 2019_12_14_000001_create_personal_access_tokens_table.php which was placed inside this folder when I installed Sanctum. I just want to run this migration while not trying to run other migrations. The reason behind this is this that I am using an existing database with tables with same name and I don’t want to lose any data in it by running migrations.

To run migration only for personal_access_tokens table I would create a folder named “tokens” inside “database/migrations” folder and copy my 2019_12_14_000001_create_personal_access_tokens_table schema file to it. Thereafter I would specify path to this folder by using –path param while running migration.

php artisan migrate --path=/database/migrations/tokens

Also, it is worth mentioning that since I was using an existing database there was an existing table named migrations in it hence I needed to give new name to the table which laravel would use to store migrations information. I named it laravel_migration and made the following change in my config/database.php file:

'migrations' => 'laravel_migrations',

 

 

Leave a Reply