CodeIgniter Q & A


How to create and run migrations in CodeIgniter?

Creating and running migrations in CodeIgniter is a structured way to manage database schema changes and keep your database synchronized across different environments, such as development, testing, and production. Migrations allow you to version-control your database schema and make it easier to collaborate with other developers. Here’s a step-by-step guide on how to create and run migrations in CodeIgniter:


  1. Enable Migrations:

   Start by enabling migrations in your CodeIgniter application. Open the `application/config/migration.php` file and set `$config[‘migration_enabled’]` to `TRUE`.


  1. Create a Migration File:

   To create a new migration, you can use the `make:migration` command provided by CodeIgniter’s command-line tool. Open your terminal, navigate to your project’s root directory, and run the following command:


   php spark make:migration YourMigrationName


   Replace `YourMigrationName` with a meaningful name for your migration. This command will create a new migration file in the `application/Database/Migrations` directory.


  1. Define the Migration:

   Open the newly created migration file in the `application/Database/Migrations` directory. In the `up()` method, define the changes you want to make to your database schema, such as creating tables, adding columns, or modifying indexes.


  1. Run the Migration:

   To apply the migration and update your database schema, run the following command in your terminal:


   php spark migrate



  1. Rollback the Migration (Optional):

   If you need to undo the last migration, you can use the `php spark migrate:rollback` command. This will run the `down()` method defined in the last migration, reverting the changes made.


  1. Version Control:

   It’s a good practice to keep your migration files under version control (e.g., Git) so that you can track changes to your database schema over time and collaborate with other developers effectively.


By following these steps, you can create and run migrations in CodeIgniter to manage your database schema changes in an organized and controlled manner. Migrations help maintain consistency across different environments and streamline the process of making database updates while preserving data integrity.

Previously at
Flag Argentina
time icon
Experienced Full Stack Systems Analyst, Proficient in CodeIgniter with extensive 5+ years experience. Strong in SQL, Git, Agile.