We all have been in that situation when had to manage all those database schema manually, and it was really time-consuming. You might have already searched for ways to avoid it, but after trying it myself, I have found the easiest way to manage the database schema manually. Here is the solution and that is “Laravel Database Migrations”.
What is Migration?
Migrations provide mechanisms for creating and modifying database tables to the Laravel Developers. It is just a version control for your database. It allows your team to easily modify and share the applications database schema. You don’t have to worry about the specific SQL syntax for the database engine when you are using it.
Migration class contains two methods,i.e., up and down. The up method is used to add new tables, columns, or indexes to your database.The down method is used to revert the changes.If you need to update database, all you need to do is, just create a new migration. If you aren’t happy with the changes then you can easily revert it and can be back on track.
If you don’t use migrations, then I don’t think there is any version control for your database structure. Let me share some of the top benefits of using it.
Benefits of using Migration
- The biggest advantage of migration that I think is its ability to create a new migration to update schema and roll back it if not needed the changes.
- The other advantage of using migrations is that you don’t need to import .SQL file manually onto production server. Instead of that, you can just run the Artisan command to migrate your database.
- Migrations give you a powerful way of tracking what changes you need to apply to your database. Each developer can recreate the schema; apply changes in development and/or production.
To remove the complexities for you further, I have listed various commands used for migrations that are really useful:
- php artisan migrate:install
This command is used to create a special table in database and keep the track of migrations which have already been executed.
- php artisan migrate:make create_users_table
This command is used to create new migration. The migration file created by this command will handle the user table. The created migration files are placed in “application/migrations” directory. Each migration file name contains date and time as a prefix to the file. So the file name would be something like “2016_12_19_071925_create_users_table.php”.
- php artisan migrate
The file created won’t do anything if it is just placed in “application/migrations” directory. This command is used to execute all outstanding migrations.
- php artisan migrate application
This command is used to run all the migrations in the application folder.
- php artisan migrate:rollback
If you realise that you made a design error and you want to roll back the last migration then use this command.
- php artisan migrate:reset
You can’t roll back to a point before a specific migration. For this you have to run the “rollback” command repeatedly until you reach that migration. However you can reset all the migrations that you have ever run by using this command.
Alright, this is all you can get from a Laravel developer about the Migrations. I hope it will be helpful you to avoid all the hours of pain in managing your database schema. If you have any further doubts or want to share anything related to blog, you can feel free to share it in the comments section. If you want to hire Laravel developers for you next project, feel free to leave your details in the subscription form.