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

$config['uri_protocol'] = 'AUTO';

After that line, add:

$config['uri_protocol'] = isset($_SERVER['REQUEST_URI']) ? 'PATH_INFO' : 'CLI';

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:

php index.php matches

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:

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 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 index.php matches do:migration

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

php index.php matches undo:migration

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

php index.php matches 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.