Migrations in CodeIgniter 4
Content Index
We are going to work with the migrations in CodeIgniter 4, we already have our Hello World in CodeIgniter 4, now let's move on to the migrations; 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 moviesThis 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 migrateRollback 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:rollbackAnd 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.
Defining enum columns in migrations
We are going to know how we can use the enums to define our types in the database in CodeIgniter 4; which are a simple structure with some flexibility that we can use instead of foreign relationships that require a larger structure.
The enums are a very simple structure that allows us to define a set of fixed values for a column, we define it as if it were an array, therefore, the value of the record that we want to insert has to be included in that pool of data that we define. as an array.
Let's define a migration like the following:
class ProductsControl extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'INT',
'unsigned' => TRUE,
'auto_increment' => TRUE
],
'count' => [
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE
],
'type' => [
'type' => 'ENUM',
'constraint' => ['exit', 'entry'],
'default' => 'entry',
],
]);
$this->forge->addKey('id', TRUE);
$this->forge->createTable('products_control');
}
public function down()
{
$this->forge->dropTable('products_control');
}
}Explanation of the previous code
It's a NORMAL migration in CodeIgniter 4, the important thing in this post, is the type call:
'type' => [
'type' => 'ENUM',
'constraint' => ['exit', 'entry'],
'default' => 'entry',
],In which we define the constraint, such as an array, in which we define the data pool, in this case, we are indicating that type can only contain one of the following values: 'exit', 'entry'; we define a default value, and little else.
As you can see, we have any structures or columns you want to define, and the enum, which is nothing more than a common C4 definition with an array defining values that you can use 'exit' and 'entry'
The rest of your columns can have the structure that you want, that your application needs, and for this example it is not important.
Now, it is necessary to be able to interact with these tables in the database; for this, we have the Models in CodeIgniter 4.
I agree to receive announcements of interest about this Blog.
We are going to know and work with the migrations to generate our tables in the database using CodeIgniter 4.