CodeIgniter 4 is a PHP framework. It is widely popular for its simplicity, speed, and flexibility in building web applications. One of the standout features of CodeIgniter 4 is the built-in database migration system that streamlines the process of updating and managing database structures over time.
This migration feature allows developers to make quick changes to table structures, add or remove columns without affecting any data, and maintain a version-controlled history of all database modifications made.
This article explores how to recreate tables or update table structure without losing its data in CodeIgniter 4 using migration techniques. Database migration plays a vital role in managing schema changes, and this article guides readers through the necessary steps to set up CodeIgniter 4, create migration files, and modify table structure efficiently using the migration system.
Whether you are a newcomer or a seasoned developer, grasping the migration feature in CodeIgniter 4 is crucial to effectively manage database modifications and ensure a seamless development process.
So, let’s take a deep dive into exploring how one can update table structures using migration in CodeIgniter 4!
Table of Content
- Database Configuration
- Create Table using Migration
- Update Table Structure Using migrate:refresh
- Update Table Structure without losing data
- Conclusion
1. Database configuration
- Open
.env
file to update the database configuration and save it.
database.default.hostname = 127.0.0.1 database.default.database = testdb database.default.username = root database.default.password = database.default.DBDriver = MySQLi
2. Create Table using Migration
- Create a table
employees
using migration.
php spark migrate:create create_employees_table
- Now, navigate to
app/Database/Migrations/
folder from the project root. - Find a PHP file that ends with
CreateEmployeesTable
and open it. - Define the
employees
table structure in theup()
method. - Using the
down()
method deleteemployees
table that calls when undoing migration.
<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class CreateEmployeesTable extends Migration { public function up(){ $this->forge->addField([ 'id' => [ 'type' => 'INT', 'constraint' => 5, 'unsigned' => true, 'auto_increment' => true, ], 'emp_name' => [ 'type' => 'VARCHAR', 'constraint' => '100', ], 'email' => [ 'type' => 'VARCHAR', 'constraint' => '100', ], 'city' => [ 'type' => 'VARCHAR', 'constraint' => '100', ], ]); $this->forge->addKey('id', true); $this->forge->createTable('employees'); } public function down(){ $this->forge->dropTable('employees'); } }
- Run the migration –
php spark migrate
I added some records to the table.
3. Update Table Structure Using migrate:refresh
Using migrate:refresh
you can recreate tables.
Steps –
- Again open
CreateEmployeesTable
migration PHP file inapp/Database/Migrations/
folder. - Modify table structure in the
up()
method –
public function up(){ $this->forge->addField([ 'id' => [ 'type' => 'INT', 'constraint' => 5, 'unsigned' => true, 'auto_increment' => true, ], 'fullname' => [ 'type' => 'VARCHAR', 'constraint' => '191', ], 'email' => [ 'type' => 'VARCHAR', 'constraint' => '100', ], 'city' => [ 'type' => 'VARCHAR', 'constraint' => '100', ], 'age' => [ 'type' => 'INT', 'constraint' => '3', ], ]); $this->forge->addKey('id', true); $this->forge->createTable('employees'); }
- Here, I did the following –
- Change column name from
emp_name
tofullname
and change constraint value from100
to191
. - Added a new column
age
.
- Change column name from
- Refresh the migration –
php spark migrate:refresh
NOTE – Above command will recreate the whole database and delete its data.
4. Update Table Structure without losing data
To do this create a new migration –
php spark make:migration update_and_addfield_to_employees_table
- Open a PHP file that ends with
UpdateAndAddfieldToEmployeesTable.php
inapp/Database/Migrations/
folder. - Define table modification in
up()
method –- Rename column name from
emp_name
tofullname
. - Add a new column
age
.
- Rename column name from
- Reset table structure using
down()
method –- Rename column name from
fullname
toemp_name
. - Delete
age
column.
- Rename column name from
NOTE – If you have created more than 1 column while table altering then in the
down()
method mention the column names between[]
separated by comma indropColumn()
.
<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class UpdateAndAddfieldToEmployeesTable extends Migration { public function up(){ ## Rename column name from emp_name to fullname $alterfields = [ 'emp_name' => [ 'name' => 'fullname', 'type' => 'VARCHAR', 'constraint' => '100', ], ]; $this->forge->modifyColumn('employees', $alterfields); ## Add age column $addfields = [ 'age' => [ 'type' => 'INT', 'constraint' => '3', ], ]; $this->forge->addColumn('employees', $addfields); } public function down(){ ## Delete 'age' column $this->forge->dropColumn('employees', ['age']); ## Rename column name from fullname to emp_name $fields = [ 'fullname' => [ 'name' => 'emp_name', 'type' => 'VARCHAR', 'constraint' => '100', ], ]; $this->forge->modifyColumn('employees', $fields); } }
- Run the migration –
php spark migrate
After execution employees
table structure is changed and data is not deleted.
5. Conclusion
Use migrate:refresh
only when you want to recreate all tables using migration otherwise, create a new migration file for updating the existing table.
You can also alter more than 1 table using a single migration file.
If you found this tutorial helpful then don't forget to share.