Introduction
Database migrations provide a systematic way to manage database changes in an application's development lifecycle. CodeIgniter offers a built-in migration feature that simplifies the process of creating, applying, and rolling back database schema changes. This tutorial will guide you through the steps of using database migrations in CodeIgniter, enabling you to keep your database structure up-to-date and version-controlled.
Example: Creating a Migration
Let's start with an example of creating a migration to add a new table called "products" to the database.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Create_products_table extends CI_Migration {
public function up()
{
$this->dbforge->add_field([
'id' => [
'type' => 'INT',
'unsigned' => TRUE,
'auto_increment' => TRUE,
],
'name' => [
'type' => 'VARCHAR',
'constraint' => '100',
]
]);
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('products');
}
public function down()
{
$this->dbforge->drop_table('products');
}
}
?>
In the example above, we create a new migration class named "Migration_Create_products_table" that extends the CI_Migration
class. In the up()
method, we define the table structure using the add_field()
method of the dbforge
library. We specify the columns and their attributes, such as data type, constraints, and auto-incrementing properties. We then use the add_key()
method to set the primary key, and finally, the create_table()
method to create the "products" table. In the down()
method, we use the drop_table()
method to revert the changes and remove the "products" table.
Steps to Use Database Migrations in CodeIgniter
- Enable Migrations: Open the
config/migration.php
file and set$config['migration_enabled']
toTRUE
. - Create a Migration: Run the command
php index.php migrate create migration_name
in the terminal, replacing "migration_name" with a descriptive name for your migration. - Edit the Migration: Open the generated migration file in the
application/migrations
folder and define theup()
anddown()
methods to make the necessary database changes and rollback steps, respectively. - Run the Migrations: In the terminal, run the command
php index.php migrate
to apply all pending migrations and update the database structure. - Rollback Migrations: To revert the last migration, use the command
php index.php migrate rollback
. To rollback all migrations, usephp index.php migrate rollback all
.
Common Mistakes
- Not enabling migrations in the configuration file (
$config['migration_enabled']
). - Forgetting to define the
up()
anddown()
methods in the migration class. - Not following proper naming conventions for migration files.
Frequently Asked Questions (FAQs)
-
Q: Can I modify an existing migration?
A: It is generally not recommended to modify an existing migration file once it has been applied. Instead, create a new migration to make the necessary changes.
-
Q: How can I reorder migrations?
A: CodeIgniter follows a timestamp-based naming convention for migration files. To reorder migrations, you can modify the timestamps in the migration filenames accordingly.
-
Q: Can I run migrations automatically during deployment?
A: Yes, you can run migrations automatically during deployment by executing the
php index.php migrate
command as part of your deployment script or process.
Summary
Database migrations in CodeIgniter provide a convenient way to manage database schema changes throughout the development lifecycle. By enabling migrations, creating migration files, and defining the necessary changes in the up()
and down()
methods, you can easily apply and roll back database modifications. Avoid common mistakes, such as not enabling migrations or forgetting to define the methods in the migration class. Refer to the FAQs section for answers to common questions. Start using database migrations in CodeIgniter to maintain an organized and version-controlled database structure in your applications.