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

Created at: July 27, 2015; Last update: July 27, 2015

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.

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:

After that line, add:

Save the file…

Put Matches.php inside the controllers directory. Put config/matches.php inside your app config folder. Also, put matches_templates folder inside the views folder. From terminal or command prompt go to the application’s index.php and type:

If everything went well, you should be greeted by Matches.

Using Matches to create a migration

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

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:

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():

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

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

And that’s it… Hope you’ve enjoyed this at least as much as I did.

You can find Matches on my Github repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.