Migrations and Oil

When quickly creating projects and getting the bare bones together, the FuelPHP Oil tool can handle some of the more repetitive tasks for you, allowing you to concentrate on other aspects of the project. This ranges from setting up the database tables (as demonstrated before) to creating models and controllers. The FuelPHP Oil tool can also be used to scaffold an administration system that is ready to be used to administer contents with a full user-authentication system. It'll provide the starting point; although the results won't always be pretty, it will be functional.

This allows you to test ideas quickly and iterate towards the perfect code. We'll run through the individual features of Oil to create models, controllers, and database migrations. This will give us more time to tweak and create a more polished project/journal.

One of the most useful features of Oil is the ability to migrate the database structure. These can be used to ensure the consistency between environments. When deploying code, it's possible to run migrations on the database as well.

Migrations can be used to rename a table, add and remove fields or even tables. Oil has the notion of magic migrations. These start with a keyword's prefix, making it easy to see what they will be doing:

$ php oil generate migration create_users name:text
$ php oil generate migration rename_table_users_to_people
$ php oil generate migration add_profile_to_people profile:text
$ php oil generate migration delete_profile_from_people profile:text
$ php oil generate migration drop_people

Tip

You need to be aware of the keywords when creating a migration. Keywords include create_, rename_, add_, delete_, rename_, and drop_.

Migrations are stored in fuel/app/migrations and a list of performed migrations is stored in fuel/app/config/<environment>/migrations.php.

Additionally, the migration's database table records the current state of the project. Running migrations is as a simple as typing the following command:

$ oil refine migrate

Tip

You can simply type r and g to run the refine and generate Oil actions.

If you want to migrate up or down, run the following commands:

$ oil r migrate:up
$ oil r migrate:down

These commands can be worked into a deployment script, so that migrations can automatically be run when the new code is added to different environments. This is particularly useful for avoiding errors on the production environment.

Before progressing to generating models and their database tables, let's drop the posts table we created previously:

$ php oil g migration drop_posts
$ php oil r migrate
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.203.85