Table of Contents
Inside this article we will see How to rollback migrations in cakephp 4. Article contains classified information about rollback migrations step by step 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.
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 CRUD Using Ajax Requests Tutorial
- CakePHP 4 Custom Primary Key For Table Using Migration
- CakePHP 4 How To Add Column in Table Using Migration
- CakePHP 4 How To Check All Routes of Application
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 3 different migrations for 3 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
Above is the order of Rollback.
$ bin/cake migrations rollback
We can see CreateDevices rollback now, It’s status is now “down”.
$ bin/cake migrations rollback
Process of rollback always reverts the single status of migration in order by migration ID.
Rollback Upto Specific Migration Version
If we want to rollback all migrations upto a specific migration version (Migration Id), we can do. Specified migration id will be excluded.
According to above example, this is order as per migration id for rollback.
CreateDevices (20220220015142) > CreateEmployeesTable (20220219025842) > CreateProducts (20220217021429)
Rollback upto specific migration version
$ bin/cake migrations rollback -t 20220217021429
We can see 20220217021429 is the Migration ID of CreateProducts migration. Once we execute above command it will rollback all migration upto that version in order of descending. Specified migration ID will be excluded from rollback list.
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 How To Rollback Migrations in CakePHP 4 Tutorial in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.