Running and Managing Migrations
This guide covers the commands you use after writing or generating migration files.
Applying Migrations
Once you have generated or written your migration file, apply the changes to your database:
# Run all the migrations
bin/cake migrations migrate
# Migrate to a specific version using the --target option
bin/cake migrations migrate -t 20150103081132
# Run migrations from a custom source directory
bin/cake migrations migrate -s Alternate
# Run migrations against a different connection
bin/cake migrations migrate -c my_custom_connection
# Run migrations for a plugin
bin/cake migrations migrate -p MyAwesomePluginReverting Migrations
The rollback command undoes previously executed migrations:
# Roll back to the previous migration
bin/cake migrations rollback
# Roll back to a specific version
bin/cake migrations rollback -t 20150103081132You can also use the --source, --connection, and --plugin options just like for the migrate command.
Viewing Migration Status
The status command prints a list of all migrations, along with their current status:
bin/cake migrations statusYou can also output the results as JSON:
bin/cake migrations status --format jsonYou can also use the --source, --connection, and --plugin options just like for the migrate command.
Cleaning Up Missing Migrations
Sometimes migration files may be deleted from the filesystem but still exist in the migrations tracking table. These migrations will be marked as MISSING in the status output. You can remove these entries using the --cleanup option:
bin/cake migrations status --cleanupThis will remove all migration entries from the tracking table that no longer have corresponding migration files in the filesystem.
Marking a Migration as Migrated
It can sometimes be useful to mark a set of migrations as migrated without actually running them. In order to do this, use the mark_migrated command.
You can mark all migrations as migrated:
bin/cake migrations mark_migratedYou can also mark all migrations up to a specific version using the --target option:
bin/cake migrations mark_migrated --target=20151016204000If you do not want the targeted migration to be marked as migrated during the process, use the --exclude flag:
bin/cake migrations mark_migrated --target=20151016204000 --excludeIf you wish to mark only the targeted migration as migrated, use the --only flag:
bin/cake migrations mark_migrated --target=20151016204000 --onlyYou can also use the --source, --connection, and --plugin options just like for the migrate command.
NOTE
When you bake a snapshot with cake bake migration_snapshot, the created migration will automatically be marked as migrated. To prevent this behavior, for example for unit test migrations, use the --generate-only flag.
This command also accepts the migration version number as a positional argument:
bin/cake migrations mark_migrated 20150420082532If you wish to mark all migrations as migrated, you can use the all special value:
bin/cake migrations mark_migrated allSeeding Your Database
Seed classes are a good way to populate your database with default or starter data. They are also useful for generating data for development environments.
By default, seeds are looked for in the config/Seeds/ directory of your application. See Database Seeding for how to build and use seed classes.