Last active
August 29, 2015 13:56
-
-
Save andrewwheal/9089542 to your computer and use it in GitHub Desktop.
FuelPHP v1 Migration Examples/Suggestions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Fuel\Migrations; | |
class Db_Stuff | |
{ | |
public function up() | |
{ | |
// Try/catch so that we can provide a nicer error message and gracefully fall out of the migration | |
try { | |
// If your migration has a dependency on another package/module's migrations then you can make sure that package/module has been migrated like this | |
// In this example this migration relies on a module within the propshop package so we need to make sure that the appropriate module path is available | |
\Config::set('module_paths.99999', PKGPATH.'/propshop/modules/'); | |
// This example depends on migration number 5 of the shipping module so we will run that module up to that point | |
$results = \Migrate::up(5, 'shipping', 'module'); | |
// If things were successful then we will get an array of the migrations that were run | |
if (! is_array($results)) { | |
// Report that one of the dependencies failed and return false | |
\Cli::error('Could not migrate dependencies: ' . __FILE__); | |
return false; | |
} | |
// Always wrap all DB calls in a transaction | |
// Data definition language (DDL) statements are not compatible with transactions | |
// However, transactions should always be used for consistency, people just need to be aware of their limits | |
\DB::start_transaction(); | |
\DBUtil::create_table(...); | |
\DB::insert(...); | |
\DB::commit_transaction(); | |
} catch (\Exception $e) { | |
\DB::rollback_transaction(); | |
\Cli::error(sprintf('Up Migration Failed - %s - %s', $e->getMessage(), __FILE__)); | |
return false; | |
} | |
\Cli::write('Migrated Up Successfully: ' . __FILE__, 'green'); | |
} | |
public function down() | |
{ | |
try { | |
\DB::start_transaction(); | |
// The down of a migration should undo any changes made by the up | |
// It is neccessary to remove foreign keys/constraints, remove added data, | |
// manipulate data in reverse, remove added fields, drop created tables | |
// This is not an exaustive list | |
\DB::delete(...); | |
\DBUtil::drop_table(...); | |
\DB::commit_transaction(); | |
} catch (\Exception $e) { | |
\DB::rollback_transaction(); | |
\Cli::error(sprintf('Down Migration Failed - %s - %s', $e->getMessage(), __FILE__)); | |
return false; | |
} | |
\Cli::write('Migrated Down Successfully: ' . __FILE__, 'green'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Fuel\Migrations; | |
class Do_Something | |
{ | |
public function up() | |
{ | |
// Try/catch so that we can provide a nicer error message and gracefully fall out of the migration | |
try { | |
// You don't always have to do DB stuff in migrations, examples of other things that might | |
// be done in a migration are, creating folder structure, moving/copying files, running a task | |
} catch (\Exception $e) { | |
\Cli::error(sprintf('Up Migration Failed - %s - %s', $e->getMessage(), __FILE__)); | |
return false; | |
} | |
\Cli::write('Migrated Up Successfully: ' . __FILE__, 'green'); | |
} | |
public function down() | |
{ | |
try { | |
// Sometimes migrations can't be undone, e.g. one that runs a task | |
// However, every effort should be made to get things back to the state things were in | |
// before the up was run, and at the very least allow for the up to be re-run successfully | |
} catch (\Exception $e) { | |
\Cli::error(sprintf('Down Migration Failed - %s - %s', $e->getMessage(), __FILE__)); | |
return false; | |
} | |
\Cli::write('Migrated Down Successfully: ' . __FILE__, 'green'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment