CakePHP: how to use Migrations?



With Migrations, CakePHP offers the option of importing or undoing changes to the database structure. The typical migration process is as follows:

Generating migrations from an existing database

If you are dealing with a pre-existing database and want to start using migrations, or to version control the initial schema of your application’s database, you can run the migration_snapshot command:

bin/cake bake migration_snapshot Initial

Generating a diff between two database states

You can generate a migrations file that will group all the differences between two database states using the migration_diff bake template. To do so, you can use the following command:

bin/cake bake migration_diff NameOfTheMigrations

Applying Migrations

Once you have generated or written your migration file, you need to execute the following command to apply the changes to your database:

bin/cake migrations migrate

Migrations Status

The Status command prints a list of all migrations, along with their current status. You can use this command to determine which migrations have been run:

bin/cake migrations status

Photo by Aneta Voborilova on Unsplash


Leave a Reply

Your email address will not be published. Required fields are marked *