How to execute only specific migration script file in Laravel

Posted on

This is an article on how to execute only a specific migration script file as shown in the title of this article. The migration script file exist inside or it is stored in the Laravel web-based application project. Basically, executing the following command :

php artisan migrate

It will definitely migrating or execute the migration process for all of the migration script files available in the folder of ‘/database/migrations’. But how can we be able to trick the migration process for only migration one specific or certain migration file script ?. The solution is quite easy which is done by isolating the migration script file into a specific folder. In order to do that, create a specific folder named ‘selected’ for instance. It can be done by executing the following command :

user@hostname:/var/www/html/laravel/database/migrations$ mkdir selected
user@hostname:/var/www/html/laravel/database/migrations$ ls -al | grep selected
drwxrwxr-x 2 www-data user  4096 Nov 10 11:15 selected
user@hostname:/var/www/html/laravel/database/migrations$ 

So, just move the specific migration script file which is going to be executed into the already created folder named ‘selected’. The process is shown as follows :

But by defining the path of migration script file, the migration script file executed will be only the one which is located inside that specific folder.

user@hostname:/var/www/html/laravel/database/migrations$ mv 2017_11_03_060734_create_services_table.php selected
user@hostname:/var/www/html/laravel/database/migrations$ cd selected
user@hostname:/var/www/html/laravel/database/migrations/selected$ ls -al
total 24
drwxrwxr-x 2 www-data user 4096 Nov 8 09:24 .
drwxrwxr-x 4 www-data user 4096 Nov 8 09:19 ..
-rwxrwxr-x 1 www-data user  790 Nov 8 09:20 2017_11_03_060734_create_services_table.php
user@hostname:/var/www/html/laravel/database/migrations/selected$

After moving the specific migration script file into a specific location, just execute the command for migrating it by executing the following command to do it :

php artisan migrate:refresh --path=/database/migrations/selected

The following is the execution of editing the file and also migrating the specific migration script file as shown below :

user@hostname:/var/www/html/laravel/database/migrations/selected$ vim 2017_11_03_060734_create_services_table.php
user@hostname:/var/www/html/laravel/database/migrations/selected$ cd ..
user@hostname:/var/www/html/laravel/database/migrations$ cd ..
user@hostname:/var/www/html/laravel/database$ cd ..
user@hostname:/var/www/html/laravel$ php artisan migrate:refresh --path=/database/migrations/selected
Migration not found: 2017_11_05_151111_test
Migration not found: 2017_11_05_120721_user_role
Migration not found: 2017_11_05_120717_role
Migration not found: 2017_11_03_095254_create_units_table
Rolling back: 2017_11_03_060734_create_services_table
Rolled back: 2017_11_03_060734_create_services_table
Migration not found: 2017_11_02_154132_create_comments_table
Migration not found: 2017_11_02_151921_create_categories_table
Migration not found: 2017_11_02_150552_create_tickets_table
Migration not found: 2014_10_12_100000_create_password_resets_table
Migration not found: 2014_10_12_000000_create_users_table
Migrating: 2017_11_03_060734_create_services_table
Migrated: 2017_11_03_060734_create_services_table
user@hostname:/var/www/html/laravel$

As shown above, it can be seen that only the file named ‘2017_11_03_060734_create_services_table’ which is located in the certain folder named ‘selected’.  The cause for the error given ‘Migration not found’ is because the migration script file are not located inside the ‘selected’ folder although the status of the file exist in the migrations table. Although it is not located in the folder named ‘selected’, the list of migration script file is located in a table named ‘migrations’ as shown below :

mysql> select * from migrations;
+----+------------------------------------------------+-------+
| id | migration                                      | batch |
+----+------------------------------------------------+-------+
| 22 | 2017_11_03_060734_create_services_table        |     4 |
| 32 | 2014_10_12_000000_create_users_table           |     5 |
| 33 | 2014_10_12_100000_create_password_resets_table |     5 |
| 35 | 2017_11_02_151921_create_categories_table      |     5 |
| 37 | 2017_11_03_095254_create_units_table           |     5 |
| 38 | 2017_11_05_120717_role                         |     5 |
| 39 | 2017_11_05_120721_user_role                    |     5 |
| 40 | 2017_11_05_151111_test                         |     5 |
| 42 | 2017_11_02_150552_create_tickets_table         |     6 |
| 43 | 2017_11_02_154132_create_comments_table        |     7 |
+----+------------------------------------------------+-------+
10 rows in set (0,01 sec)

To make sure the table has been executed and modified just check in the MySQL Database Server as shown below :

user@hostname:/var/www/html/laravel$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8326
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use support_ticket;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * services;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'services' at line 1
mysql> desc services;
+-------------+------------------+------+----+--------+---------------+
| Field       | Type             | Null | Key| Default| Extra         |
+-------------+------------------+------+----+--------+---------------+
| id          | int(10) unsigned | NO   | PRI| NULL   |auto_increment |
| name        | varchar(255)     | NO   |    | NULL   |               |
| service_id  | varchar(255)     | NO   |    | NULL   |               |
| status      | varchar(255)     | NO   |    | NULL   |               |
| time        | varchar(255)     | NO   |    | NULL   |               |
| description | varchar(255)     | NO   |    | NULL   |               |
| created_at  | timestamp        | YES  |    | NULL   |               |
| updated_at  | timestamp        | YES  |    | NULL   |               |
+-------------+------------------+------+-----+-------+---------------+
8 rows in set (0,00 sec)

mysql>

As shown in the above output, the table named ‘services’ already created. Below is the actual content of the file :

<?php 
use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 
class CreateServicesTable extends Migration { 
/** * Run the migrations. * * @return void */ 
public function up() { 
       Schema::create('services', function (Blueprint $table) {            
            $table->increments('id');
            $table->string('name');
            $table->string('service_id');
            $table->string('status');
            $table->string('duration');
            $table->string('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('services');
    }
}

3 thoughts on “How to execute only specific migration script file in Laravel

Leave a Reply