Working with database structure is often a cumbersome process. Initially everything is fine. You have all kinds of options and can design whatever database structure you want and your project needs. You can even change your structure many times over during the initial development without any real implications. However, this changes as soon as you get more developers on board or whenever you start working with real data. As soon as collaboration and live data exists, you need to make sure that structure and data is properly managed throughout any change process you may need. You cannot just change the database in your local environment without properly recording the change and making sure everyone else on the team, and later the production server, is onboard. Otherwise you will have severe database errors. Thankfully Laravel provides relief during this process.
What are migrations?
Migrations in Laravel are a set of scripts which hold information about the database structure. The system is fairly straightforward: Every time you change the database, you create a script to do so – and to revert the change. If you get used to changing the database via migration files right away, you will be much less likely to experience database structure problems later.
Migration files should be distributed along with the rest of the code via version control. Then everyone has the updated versions available and can migrate their individual database accordingly. The reversal commands make it possible to downgrade to an earlier database version anytime, if you should encounter problems after an upgrade.
Writing migrations
Creating your own migration files is pretty simple. Laravel has a structure ready for this, and many different commands available to support most imaginable structure configurations. Below is an example:
Schema::create('users', function (Blueprint $table) { $table->increments('id'); });
The example creates a table “users” with an increment “id” field. There are plenty of migration commands available to set up other fields, and you should study the Laravel documentation for this. Working with migrations or a similar process is crucial to keeping database structure changes under control without sudden surprises and problems along the way. Just like implementing version control for code or getting used to always creating automated tests for your code, migrations are an essential part of good development practices.
No comments yet (leave a comment)