CakePHP Migrations is an add-on that we will be using to make database maintenance easier.

Contents

Testing that migrations are working

First, make sure you have PEAR installed.

Navigate to flickribbon/cake/console and run:

./cake migrate help
  • If you get a help message, migrations are working!

Get CakePHP Migrations running

  1. To run cake migrations, first navigate to flickribbon/cake/console. Now run the following command:
    • Windows & Linux:
      > php cake.php migrate
    • Mac OSX:
      $ ./cake migrate
    Note: If you get an error about the class Spyc being missing, make a copy of it in flickribbon/app/vendors/shell and make it's name all lowercase (it is originally located in flickribbon/app/vendors/). This can be accomplished with the following command (while in the vendors folder):
    cp Spyc.php shell/spyc.php
  2. To generate a basic YAML file (inside flickribbon/app/config/migrations) run the following command:
    • Windows & Linux:
      > php cake.php migrate generate my_migration_name_here
    • Mac OSX:
      $ ./cake migrate generate my_migration_name_here
  3. When prompted if you would like to edit the file in Textmate,
    • For Windows & Linux say N
    • For Mac OSX you can say Y

Editing the YAML file

Now this will generate a YAML file to create your database tables in the flickribbon/app/config/migrations/ directory. You will have to use a text editor to edit the YAML file and you will add tables and fields for example. Here's what the file will look like

UP:
  create_table:
    test:
      name:
      description: text
      count: integer
      is_active: boolean
DOWN:
  drop_table: test

Before making a new migration file, make sure to check out the example YAML migration files

Whenever you want your database to have all the migrations applied to them, execute the following command:

  • Windows & Linux:
    > php cake.php migrate
  • Mac OSX:
    $ ./cake migrate

This will run all the YAML and generate all the tables and it's fields onto your local database.

This will allow us to create new tables for our program using YAML files. We can then commit those YAML files to our Subversion repository so everyone on the team can perform ./cake migrate and have the same version of the database!

Migrations and fixtures after a Subversion update

Here is what you should run after each time you update from subversion:

./cake migrate

And if you want to load in a bunch of test data from the fixtures, run

./cake fixtures

Misc

To reset your database and load all the data in from fixtures, run

./cake migrate reset
./cake migrate
./cake fixtures


To generate fixture for all the data currently in your tables, run

./cake fixtures g fromdb -force

More