What are Database Migrations?

Database migrations ensure the structure of the SQL database always stays in-sync between all different servers the software is installed on. There may be both production and staging servers, plus a team of developers working on the same project. Database migrations ensure every installation of the software has the exact same database structure.

You can think of migrations as a book with pages in sequential order where every page is a separate database migration, which in reality is just a PHP class. This allows you to not only execute SQL statements against the database to modify its structure, but additional PHP code to do anything necessary to upgrade the system. Aside from an installation method within the PHP class, migrations generally always also include a rollback() / down() method as well.

Migrations are orderd by the timestamp of their creation. When a system is upgraded, it will automatically check for any new migration classes that have not already been installed. For any found, the install() / up() method within the migration's PHP class will be executed to modify the database structure as necessary.