The migrations in CodeIgniter or how to have a “Git” for your database (part 3 of 3: Using Matches for faster development)

Hmm… I don’t know about you but the two tutorials before this one in the series about migration really scared me with the naming, the controllers, the format of the migration, etc. So why not speed things up? We can do this by using Matches, a CodeIgniter CLI script that helps me speed things up when talking about development in CodeIgniter. I sure hope you didn’t just realize that this is also about promoting my script (which, by the way, it’s open source…).

Now, let’s talk about Matches, and how can this script help us with the migrations. By using matches, you won’t have to concern yourself with naming the file with the timestamp, or about the controller that will create, undo or reset the migrations.

Intalling Matches

First of all, you need to verify if you have php cli installed. In order to do this, type into terminal/command prompt.

PHP

1

php-v

If you receive the php version number, then you are good to go.

Now go to your config.php file inside the application/config and make sure the following line looks like below:

Using Matches to create a migration

To create a migration, after you’ve been greeted by Matches, you only need to write:

PHP

1

php index.php matches create:migration create_users_table

This line above will tell matches to create a migration file named 20150727110200_create_users_table.php (where the timestamp is taken from the server time…).

Now, if you open the file you will see the basic template of the migration with an up() and a down() method. Make sure you change the “SET_YOUR_TABLE_HERE” string with the table name you want to create. Of course you could have done this directly from the matches by writing:

PHP

1

php index.php matches create:migration create_users_table table:users

After you’ve set up the fields you want to create/modify/remove you can do the migration by using the matches’ method do_migration():

PHP

1

php index.php matches do:migration

If you want to undo the last migration, you write:

PHP

1

php index.php matches undo:migration

Now, if you want to reset the migration, you use… you’ve guessed it… reset_migration()