CakePHP provides a command-line tool called
cake migrations that you can use to perform database migrations. Here are the steps to use this tool:
- Generate a migration file: Use the cake bake migration <MigrationName> command to generate a migration file. For example, cake bake migration AddFieldToTable will generate a file called AddFieldToTable.php in the config/Migrations directory.
- Edit the migration file: Open the migration file in a text editor and add the necessary code to modify the database schema. For example, you can use the addColumn() or createTable() methods to add a new column or table to the database.
- Run the migration: Use the cake migrations migrate command to run the migration. This will apply the changes to the database schema. For example, cake migrations migrate will apply all pending migrations.
- Rollback a migration: Use the cake migrations rollback command to undo the last migration. This is useful if you made a mistake or want to revert the changes. For example, cake migrations rollback will undo the last migration.
- List migrations: Use the cake migrations status command to list all migrations and their current status. For example, cake migrations status will list all migrations and their status (pending, migrated, or rolled back).
You can also use other options and arguments with the
cake migrations command to perform specific tasks, such as marking a migration as applied or creating a new migration. You can learn more about these options by running
cake migrations --help or by referring to the CakePHP documentation.