Checking your database migration status in PHP Laravel 5

Posted on

Checking your database migration status script file in PHP Laravel 5

Programming Language : PHP

Framework : Laravel 5

Operating System : Ubuntu 16.04 (Xenial Xerus). It is working for operating sytem which has command line interface

If you want specifically generate table by using php artisan utility provided in Laravel, we can use the following command :

php artisan migrate:status

The above command must be executed in the root folder of Laravel project. The following is the example in real-life case :

username@hostname:/var/www/html/laravel_project# php artisan migrate:status
+------+----------------------------------------------------+
| Ran? | Migration                                          |
+------+----------------------------------------------------+
| Y    | 2014_10_12_000000_create_users_table               |
| Y    | 2014_10_12_100000_create_password_resets_table     |
| Y    | 2016_07_07_113807_create_projects_and_tasks_tables |
+------+----------------------------------------------------+
username@hostname:/var/www/html/laravel_project#

As you can see, there are the migration ran’s status of each migration script file which is generated before. We also can check it whether or not the migration script file itself exists in the specific folder which contains the migration script file in :

database/migrations
username@hostname:/var/www/html/laravel_project/database/migrations# ls
2014_10_12_000000_create_users_table.php  2014_10_12_100000_create_password_resets_table.php  2016_07_07_113807_create_projects_and_tasks_tables.php
  1. Let’s start from creating a migration script

We already knew, the migration script file itself can be generated by executing the following command :

php artisan make:migrate migration_script_file

We can see completely in the other article which can be found in here.  But in this context, we try to create another one as follows :

username@hostname:/var/www/html/laravel_project#
username@hostname:/var/www/html/laravel_project# php artisan migrate
Migrated: 2016_07_07_130638_create_file_upload_table
username@hostname:/var/www/html/laravel_project#
  1. Check the migration status

After  creating the migration script file, we can check the status of the migration script file itself whether or not has ben ran or executed by typing the following command :

php artisan migrate:status

Example in the real-life example :

username@hostname:/var/www/html/laravel_project# php artisan migrate:status
+------+---------------------------------------------------+
| Ran? | Migration                                         |
+------+---------------------------------------------------+
| Y    | 2014_10_12_000000_create_users_table              |
| Y    | 2014_10_12_100000_create_password_resets_table    |
| Y    | 2016_07_07_125815_create_project_and_tasks_tables |
| N    | 2016_07_07_130638_create_file_uploads_table       |
+------+---------------------------------------------------+
  1. Run the migration script file by typing the following command :
php artisan migrate

Example in real-life situation :

username@hostname:/var/www/html/laravel_project# php artisan migrate
Migrated: 2016_07_07_130638_create_file_uploads_table
username@hostname:/var/www/html/laravel_project#
  1. Since the migration has already took place by executing command in the third step which is executing all of the migration script status that haven’t been executed. We have to check the status again by typing the following command :
php artisan migrate:status

Real-life example is shown below :

username@hostname:/var/www/html/laravel_project# php artisan migrate:status
+------+---------------------------------------------------+
| Ran? | Migration                                         |
+------+---------------------------------------------------+
| Y    | 2014_10_12_000000_create_users_table              |
| Y    | 2014_10_12_100000_create_password_resets_table    |
| Y    | 2016_07_07_125815_create_project_and_tasks_tables |
| Y    | 2016_07_07_130638_create_file_uploads_table       |
+------+---------------------------------------------------+
username@hostname:/var/www/html/laravel_project#

As we can see in the above, the status has been changed in the field of Ran ? for the fourth record from ‘N’ to ‘Y’. It is to imply that after executing command ‘php artisan migrate’ which is executed successfully, the status of the migration script file 2016_07_07_130638_create_file_uploads_table has changed.

Check whether the migration script file whether it has already been created or not :

username@hostname:/var/www/html/laravel_project# mysql -uusername -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 54
Server version: 5.7.12-0ubuntu1.1 (Ubuntu)
Copyright (c) 2000, 2016, 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 laravel_project;
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> show tables;
+---------------------------+
| Tables_in_laravel_project |
+---------------------------+
| file                      |
| file_uploads              |
| migrations                |
| password_resets           |
| projects                  |
| tasks                     |
| users                     |
+---------------------------+
7 rows in set (0,00 sec)
mysql>

Successfully created the table of file_uploads which can be seen in the above output command in MySQL console.  It is align with the information given in the status of migration file script that the script has been successfully ran or executed.

One thought on “Checking your database migration status in PHP Laravel 5

Leave a Reply