Migrations in CodeIgniter 4

We are going to work with the migrations in CodeIgniter 4, for that we simply have to create a file or a migration through our spark, which is the command line that CodeIgniter 4 offers us.

Migrations are a very simple mechanism that we have to have a one-to-one relationship between the database tables with the definition that we have in our project; migrations are used by all sorts of modern frameworks like Django, which I recommend you read the previous post for another look at migrations in frameworks like CodeIgniter 4.

Create migrations

So, we open our terminal or CMD and we position ourselves in the root of our project and we have to execute the following command to create a migration:

php spark migrate:create [filename]

In our case we are going to create an example migration, called movies, to handle a list of movies as we do in the CodeIgniter 4 course:

php spark migrate:create movies

This generates a file to manage the migration in app/Database/Migrations which we have to indicate the definition for our columns of the table that we want to create.

As you can see, appended to the name, it has a number that is simply a date and in this way C4 guarantees unique names in migrations.

Its definition or the definition that each column will have to be carried out by means of an array like the following:

<?php namespace App\Database\Migrations;
 
use CodeIgniter\Database\Migration;
 
class Movies extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id'          => [
                    'type'           => 'INT',
                    'constraint'     => 5,
                    'unsigned'       => TRUE,
                    'auto_increment' => TRUE
            ],
            'category_id'          => [
                'type'           => 'INT',
                'constraint'     => 5,
                'unsigned'       => TRUE
            ],
            'title'       => [
                    'type'           => 'VARCHAR',
                    'constraint'     => '255',
            ],
            'description' => [
                    'type'           => 'TEXT',
                    'NULL'           => TRUE,
            ],
    ]);
    $this->forge->addKey('id', TRUE);
    $this->forge->createTable('movies');
    }
 
    //--------------------------------------------------------------------
 
    public function down()
    {
        $this->forge->dropTable('movies');
    }
}
 

Therefore, we have a very simple scheme to work with, it is not necessary to learn function names like in Laravel or anything like that, a simple definition based on names.

As you can see, we have to implement two functions, the one that is in charge of creating the structure, table or adding a new column or something similar, and another function that is in charge of reverting the previous changes, and this is, so that we can perform operations both to execute or create the migrations and to reverse them.

Run the migration

Then we run the following command to run the previous migration:

php spark migrate

Rollback migrations

You may have realized that you defined something wrong in the previous migration you ran, so revert the changes to the database and fix it:

php spark migrate:rollback

And here the important thing about developing the down method when defining the migration.

Refresh migrations

We can also "refresh" all the migrations, which would be a rollback of ALL the migrations to run them again.

php spark migrate:refresh 

Remember that migrations are a component that modern frameworks like CodeIgniter 4 use to create a table in the established database.

Considerations

Migrations are a mechanism that we can use to edit/modify an existing table or create new ones; for that look at the following names:

20121031100537_add_blog.php 2012-10-31-100538_alter_blog_track_views.php 2012_10_31_100539_alter_blog_add_translations.php 

  • We use a name such as alter_blog_** to add columns to the existing table called blog.
  • We use add_blog (although I personally like to just blog) to create a new component/table which in this case would be a table called blog.

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.