Skip to content

Libraries Plain Migration

Jakub Jakubiec edited this page Feb 24, 2014 · 3 revisions
Library Extends Path
Plain_Migration CI_Migration /application/libraries/Plain_Migration.php

This library extends the default Codeigniter Migration. It allows us to customize a few methods to make better use of migrations.

Methods

__construct - Public

Called automatically which in turn calls the parent constructor.


version - Public

Migrate to specified version. Overwrites the default Migrations library behaviour, so we can create database backup before migrating.

Arguments

Variable Type Default Required Description
$target_version int N/A Yes Target schema version

_make_backup - Protected

Create database backup and save it to file.


_createBackupFile - Private

Create database backup file in backup folder.


_delete_backup - Protected

Remove database backup file

Arguments

Variable Type Default Required Description
$backupFile string N/A Yes Path to a database backup file.
______

checkForColumns - Protected

Checks to make sure all the columns sent exist in the table sent. If not it logs an error, shows a 500 error and stops the migration. Easy way to check if all the columns per table exist before you run the migration.

Arguments

Variable Type Default Required Description
$columns Mixed N/A Yes A string or array of columns to check for in the table.
$table String N/A Yes The table name to check for the columns in.

Example

// check a single column
parent::checkForColumns('column', 'table_name');

// check multiple columns
parent::checkForColumns(array('column', 'column1'), 'table_name');

checkForTables - Protected

Checks to make sure all the tables sent exist. If not it logs an error, shows a 500 error and stops the migration. Easy way to check for the tables to be used in the current migration for you run the migration.

Arguments

Variable Type Default Required Description
$tables Mixed N/A Yes A string or array of table to check if exist.

Example

// check a single table
parent::checkForTables('table_name');

// check multiple columns
parent::checkForTables(array('table_name', 'table_name_1'));

getColumns - Private

Gets the columns from a table. Codeigniter already does this but it caches them. When running multiple migrations in a row, the Codeigniter calls will fail.

Arguments

Variable Type Default Required Description
$table String N/A Yes The table name to extract the columns from.

Example

// Get all the columns from a table
$columns = self::getColumns('table_name');

tableExists - Private

Returns true or false if a table exists. Codeigniter already does this but it caches the tables. When running multiple migrations in a row, the Codeigniter calls will fail.

Arguments

Variable Type Default Required Description
$table String N/A Yes The table name to check for.

Example

// Check for table
if (self::tableExists('table')) {
    // Run shiz
}
Clone this wiki locally