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:

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

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.

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:

 

 

Leave a Reply