How to Run or Execute only one specific Migration Script File in Laravel

Posted on

Introduction

This article is focusing on how to execute only one specific migration in script in Laravel-based project or application. In the previous article in this link with the title of ‘How to execute only specific migration script file in Laravel’. In that article, the solution is just to create a specific folder. Furthermore, put all of the selected script in that specific folder. After that, just run the migration script with the additional parameter pointing to that specific folder. In this article, that is not how it is going to run or to execute. It will only use one specific migration script for the execution.

Run or Execute only one specific migration script file in Laravel

Just follow the sequences as follow in order to achieve it :

  1. The existance of the Laravel-based project or application is a must. Visit this link to read the article with the title of ‘How to Create a Laravel Project using Composer in Microsoft Windows’ as a reference.

  2. Next, just execute the command line interface.

  3. After that, just access the root location of the Laravel-based project or application. In this case, it exist in ‘C:\programming\php\laravel\crud’.
  4. Soon after, execute a command for generating a specific migration script file. Just read the article in this link with the title of ‘How to Create Migration Script in Laravel’ to know how to perform it.

  5. Finally, execute the command for executing only one specific migration script file using the following command pattern :

    php artisan migrate --path=migration_script_file_with_its_relative_path

    Before executing it, check the table in the database. The example is using a table with the name of company. Below is the list of existing tables :

    mysql> show tables;
    +------------------------------------+
    | Tables_in_db_app_laravel           |
    +------------------------------------+
    | failed_jobs                        |
    | migrations                         |
    | password_resets                    |
    | users                              |
    +------------------------------------+
    4 rows in set (0.11 sec)
    
    mysql>
    
  6. Using the above command pattern, the following is the execution for only one specific migration file script :

    C:\programming\php\laravel\crud> php artisan migrate --path=database/migrations/2021_08_07_092232_create_companies_table.php
    Migrating: 2021_08_07_092232_create_companies_table
    Migrated:  2021_08_07_092232_create_companies_table (652.69ms)
    C:\programming\php\laravel\crud>
    

    After the execution, there is one additional table appear with the name of ‘companies as follows :

    mysql> show tables;
    +------------------------------------+
    | Tables_in_db_app_laravel           |
    +------------------------------------+
    | companies                          |
    | failed_jobs                        |
    | migrations                         |
    | password_resets                    |
    | users                              |
    +------------------------------------+
    5 rows in set (0.11 sec)
    
    mysql>
    
  7. Actually, it has the same purpose with executing migration script files in the article exist in this link. That article name has the title of ‘How to Execute Migration Script File in Laravel’. But that article’s main purpose is to execute all available migration scripts exist in the directory of ‘database\migrations’. The directory itself exist inside the root folder of the Laravel-based project or application.

    So, in that article, it will execute all the migration script files in database\migrations. But in this article, since there is an additional parameter for specifying the specific migration script, the execution will only affect that file in the value definition of the parameter ‘–path’. In this case, it is a migration script file with the name of ‘2021_08_07_092232_create_companies_table.php’.

Leave a Reply