Table of Contents
Inside this article we will see CakePHP 4 Rollback Migrations to Specific Migration Version. Article contains classified information about rollback migrations by passing a migration version using CakePHP CLI command.
Migrations are those files which creates table schema for application. When we create migrations in CakePHP application it stores inside /config/Migrations folder.
Let’s assume few migrations in application for products, employees, devices, students, books.
When we run either migrate for migrations or rollback, status command keep tracks of all activity. This is because it uses phinxlog behind the scene.
Application generates a default table phinxlog in database when run migrations in application. This table stores the migrations which we have migrated in database.
Learn More –
- CakePHP 4 How To Rename Table Using Migration
- CakePHP 4 Installation Database Error – Missing Datasource
- CakePHP 4 Remove Column From Table Using Migration
- How To Rollback Migrations in CakePHP 4 Tutorial
Let’s get started.
What is a Rollback Process?
Rollback process means revert the current changes of application. In terms of database, latest migrations what migrated and created tables will be rollback and drop that tables.
In this CakePHP 4, we have bake console command available to do rollback. Rollback process always do – Revert one step back. It uses migration ID while doing so.
CakePHP 4 Installation
To create a CakePHP project, run this command into your shell or terminal. Make sure composer should be installed in your system.
composer create-project --prefer-dist cakephp/app:~4.0 mycakephp
Above command will creates a project with the name called mycakephp.
Open app_local.php file from /config folder. Search for Datasources. Go to default array of it.
You can add your connection details here to connect with your database. It will be like this –
//... 'Datasources' => [ 'default' => [ 'host' => 'localhost', /* * CakePHP will use the default DB port based on the driver selected * MySQL on MAMP uses port 8889, MAMP users will want to uncomment * the following line and set the port accordingly */ //'port' => 'non_standard_port_number', 'username' => 'root', 'password' => 'sample@123', 'database' => 'mydatabase', /* * If not using the default 'public' schema with the PostgreSQL driver * set it here. */ //'schema' => 'myapp', /* * You can use a DSN string to set the entire configuration */ 'url' => env('DATABASE_URL', null), ], //... //...
You can pass host, username, password and database.
Successfully, you are now connected with the database.
Process of Rollback
Here, in this image we can see we have 5 different migrations for 5 different table schema.
Migrations for these tables and they successfully migrated into database. We can see the migration status is in “up”.
According to Migration ID – CreateDevices > CreateEmployeesTable > CreateProducts > CreateStudents > CreateBooks
Above is the order of Rollback.
$ bin/cake migrations rollback
It will revert migration in it’s order from latest one by one.
If suppose we will do rollback upto this migration id : 20220220015142
Rollback migrations upto migration version 20220220015142. Given version will be excluded from rollback list. The greater value of migration ID from this given value will be rollback.
$ bin/cake migrations rollback -t 20220220015142
If suppose we will do rollback upto this migration id : 20220217021429
Rollback migrations upto migration version 20220217021429. Given version will be excluded from rollback list. The greater value of migration ID from this given value will be rollback.
$ bin/cake migrations rollback -t 20220217021429
For more about datatypes and other information of migration you can click here for official document.
We hope this article helped you to learn about CakePHP 4 Rollback Migrations To Specific Migration Version in a very detailed way.
Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.