CodeIgniter 4 - Database Migrations

Posted by Ridwan Fadilah on Jul 24, 2020 - 08:53 pm

Database Migrations

Learn about CodeIgniter 4 Database Migration. How to create and run database migration.

Migrations are a convenient way for you to alter your database in a structured and organized manner.
Source:codeigniter.com

Database migration is helpful, especially for teamwork. When you tell other developers that they need to go and run the database, they can easily do that. It's better than you create or edit fragments SQL by hand manually.

There are so many database migration services tools such as AWS Database Migration Service, Azure, etc. However, why you use other services if you could run it yourself with the CodeIgniter 4 framework?

Like Laravel and other PHP frameworks, CodeIgniter also has its built-in database migration services.

On this Page

How to Run CodeIgniter 4 Database Migrations?

Database Migrations

You can run CodeIgniter database migration using the command-line tool. You can use this following command to migrates all database group with all available migrations:



> php spark migrate

Important to note: Before you run it using CLI, be sure you have installed the composer. Haven't to install a composer? See this Installation Guide - How to Install Composer on Windows.

You can use this command with several options:

  • '-g' - to chose database group, otherwise default database group will be used.
  • '-n' - to choose namespace, otherwise (App) namespace will be used.
  • '-all' - to migrate all namespaces to the latest migration

Example:



> php spark migrate -g test -n Blog

When using the -all option, it will scan through all namespaces attempting to find any migrations that have not been run. Source:codeigniter.com

Now, in this tutorial, I'll show you how to set up and run the database migration in CodeIgniter 4.

Create a Migration

Firstly, we'll create the migration. The migration file will place in the app/Database/Migrations directory.

Migration Directory

To create a migration, open your console (CLI) and type this command then hit enter:



> php spark migrate:create peoples

The 'peoples' are the table name, and that's the table would be migrated later.

If the process success, the console will display a message like this:

Migration Created

and you'll get a migration file inside the Migrations/ directory:

Migration Files

At this point, we just created the migration. That's haven't to run.

Create a Database Scheme

After you create a database migration, you need to create a scheme. How to create a database schema in CodeIgniter 4? If you see the migration file content, you'll find two methods.

Migration Methods

The first is the 'up' method to create a database scheme, and the second is the 'down' method to remove the database scheme.

Here's the example to create the database scheme:



<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class Peoples extends Migration
{
	public function up()
	{
		$this->forge->addField([
			'id'                => [
				'type'           => 'INT',
				'constraint' => 11,
				'unsigned'  => true,
				'auto_increment' => true,
			],
			'name'         => [
				'type'           => 'VARCHAR',
				'constraint'     => '255',
			],
			'address'     => [
				'type'             => 'VARCHAR',
				'constraint'   => '255',
			],
			'created_at'  => [
				'type'	      => 'DATETIME',
				'null'	      => true,
			],
			'updated_at' 	=> [
				'type'	     => 'DATETIME',
				'null'	     => true,
			]
		]);
		$this->forge->addKey('id', true);
		$this->forge->createTable('peoples');
	}

	public function down()
	{
		$this->forge->dropTable('peoples');
	}
}


Running The Migrations

If the scheme has created, you can run and start the migration process. Use the console to run the migration. Don't forget to run your database server.

Now, to run the migration, use this following command:



> php spark migrate

Please notice this command will execute all of the migration in your directory.

Hit enter and wait for a few seconds. If it finishes, the console will display a message like this:

Migration Done

To make sure the process is successful, try to open your database. Here's my result:

SQL Database Structure - (after migration)

and here's the database table structure:

SQL Database Table Structure - (after migration)

As you can see, we also have a migration table on the database. It's used to track all of the changes that you have done.

All done, what's next? Next, we'll learn how to fill the table that has migrated. We'll learn something is usually called Seed and Faker. Both are the same used to fill the database.

So, what's different? You'll learn about that in the next article.

Okay, that's the tutorial about CodeIgniter 4 Database Migrations. If you want to learn more about CodeIgniter, see the related article below.

You can find the full source code of these examples on our GitHub. You can also find another tutorial on our YouTube Channel.

Thanks for reading.


Related Articles