Using CodeIgniter Migrations
About
Migrations give you the ability keep your database across all environments the same without comprimising the data inside the database. Think it as a version control for your database schema. You should be doing this at all times especially when there are multiple developers so all developers don’t need to worry about having the latest database table.
Step by step
- You first need to create your migrations folder,
application/migrations/
. - Let us start our first migration file.
application/migrations/001_create.php
You will see that you are extending a migration class that is creating the sessions table. You can see that for each Array() in fields array it imitates a column. Everytime a migration is being upstreamed, CodeIgniter will call upon up()
and on downstream it will call down()
.
- Once your first migration is complete, you need to edit
application/config/migration.php
with the proper configuration variables set.
$config['migration_enabled'] = TRUE;
$config['migration_version'] = 1;
As you see here, you need to set migration_version
everytime you create a new migration file.
- Last step! Almost there! Now we actually have to call the actual migration. In your model or controller, you need to add the following code.
You should note that you should only call this code block that requires the specific schema of the database.