Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output ruckusing migration #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the [Wiki](https://github.com/ruckus/ruckusing-migrations/wiki) for the comp

* Portability: the migration files, which describe the tables, columns, indexes, etc to be created are themselves written in pure PHP5 which is then translated to the appropriate SQL at run-time. This allows one to transparently support any RDBMS with a single set of migration files (assuming there is an adapter for it, see below).

* Extensibility: the framework is written with extensibility in mind and it is very modular. Support for new RDMBSs should be as easy as creating the appropriate adapter and implementing a single interface.
* Extensibility: the framework is written with extensibility in mind, and it is very modular. Support for new RDMBSs should be as easy as creating the appropriate adapter and implementing a single interface.

* "rake" like support for basic tasks. The framework has a concept of "tasks" (in fact the primary focus of the framework, migrations, is just a plain task) which are just basic PHP5 classes which implement an interface. Tasks can be freely written and as long as they adhere to a specific naming convention and implement a specific interface, the framework will automatically register them and allow them to be executed.

Expand All @@ -25,6 +25,6 @@ See the [Wiki](https://github.com/ruckus/ruckusing-migrations/wiki) for the comp

# Limitations

* PHP5 is a hard requirement. The framework employes extensive use of object-oriented features of PHP5. There are no plans to make the framework backwards compatible.
* PHP5 is not a hard requirement. Seems to be working in project which uses PHP7. The framework employs extensive use of object-oriented features of PHP5. There are no plans to make the framework backwards compatible.

* As of August 2007, only the MySQL RDBMS is supported.
* As of August 2007, only the MySQL RDBMS is supported.
98 changes: 56 additions & 42 deletions lib/tasks/class.Ruckusing_DB_Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class Ruckusing_DB_Migrate implements Ruckusing_iTask {
private $adapter = null;
private $migrator_util = null;
private $task_args = array();
private $regexp = '/^(\d+)\_/';
private $debug = false;

function __construct($adapter) {
Expand All @@ -39,8 +38,12 @@ public function execute($args) {
die("This database does not support migrations.");
}
$this->task_args = $args;
echo "Started: " . date('Y-m-d g:ia T') . "\n\n";
echo "[db:migrate]: \n";

if (getenv('OUTPUT_RUCKUSING_MIGRATION')) {
echo "Started: " . date('Y-m-d g:ia T') . "\n\n";
echo "[db:migrate]: \n";
}

try {
// Check that the schema_version table exists, and if not, automatically create it
$this->verify_environment();
Expand Down Expand Up @@ -89,10 +92,13 @@ public function execute($args) {
echo "\tMigration directory does not exist: " . RUCKUSING_MIGRATION_DIR;
}catch(Ruckusing_Exception $ex) {
die("\n\n" . $ex->getMessage() . "\n\n");
}
echo "\n\nFinished: " . date('Y-m-d g:ia T') . "\n\n";
}

}

if (getenv('OUTPUT_RUCKUSING_MIGRATION')) {
echo "\n\nFinished: " . date('Y-m-d g:ia T') . "\n\n";
}
}

private function migrate_from_offset($offset, $current_version, $direction) {
$templates = $this->adapter->getTemplates($this->task_args);

Expand Down Expand Up @@ -124,8 +130,8 @@ private function migrate_from_offset($offset, $current_version, $direction) {
}

$migrationsCount = count($migrations);
$targetVersion;
$targetVersion = null;

// check to see if we have enough migrations to run - the user
// might have asked to run more than we have available
if(isset($migrations[$current_index+$offset]))
Expand All @@ -150,19 +156,22 @@ private function migrate_from_offset($offset, $current_version, $direction) {
}
else
{
echo "\nCurrent version equals desired version. Doesnt need to execute any migration.";
echo "\nCurrent version equals desired version. No need to execute any migrations.";
}
}

private function prepare_to_migrate($destination, $direction) {
try {
echo "\tMigrating " . strtoupper($direction);
if(!is_null($destination)) {
echo " to: {$destination}\n";
} else {
echo ":\n";
}

if (getenv('OUTPUT_RUCKUSING_MIGRATION')) {
echo "\tMigrating " . strtoupper($direction);

if (!is_null($destination)) {
echo " to: {$destination}\n";
} else {
echo ":\n";
}
}

$templates = $this->adapter->getTemplates($this->task_args);

if(array_key_exists('FLAVOUR', $this->task_args))
Expand All @@ -176,15 +185,22 @@ private function prepare_to_migrate($destination, $direction) {
if(count($migrations) == 0) {
return "\nNo relevant migrations to run. Exiting...\n";
}
$result = $this->run_migrations($migrations, $direction, $destination);

$this->run_migrations($migrations, $direction);
}catch(Exception $ex) {
throw $ex;
}
}

private function run_migrations($migrations, $target_method, $destination) {
/**
* @param mixed[] $migrations
* @param string $target_method
* @return mixed[]
* @throws Exception
*/
private function run_migrations($migrations, $target_method) {
$last_version = -1;

if($target_method === 'down')
{
$migrations = array_reverse($migrations);
Expand All @@ -203,50 +219,51 @@ private function run_migrations($migrations, $target_method, $destination) {
try {
//start transaction
$this->adapter->start_transaction();
$result = $obj->$target_method();
$obj->$target_method();
//successfully ran migration, update our version and commit
$this->migrator_util->resolve_current_version($file['version'], $target_method, $file['template']);
$this->migrator_util->resolve_current_version($file['version'], $target_method, $file['template']);
$this->adapter->commit_transaction();
}catch(Exception $e) {
$this->adapter->rollback_transaction();
//wrap the caught exception in our own
$ex = new Exception(sprintf("%s - %s", $file['class'], $e->getMessage()));
throw $ex;
throw new Exception(sprintf("%s - %s", $file['class'], $e->getMessage()));
}
$end = $this->end_timer();
$diff = $this->diff_timer($start, $end);
printf("========= %s ======== (%.2f)\n", $file['class'], $diff);

if (getenv('OUTPUT_RUCKUSING_MIGRATION')) {
$end = $this->end_timer();
$diff = $this->diff_timer($start, $end);
printf("========= %s ======== (%.2f)\n", $file['class'], $diff);
}

$last_version = $file['version'];
$exec = true;
} else {
trigger_error("ERROR: {$klass} does not have a '{$target_method}' method defined!");
}
}//is_file
}//foreach
}
}
}
//update the schema info
$result = array('last_version' => $last_version);
return $result;
}//run_migrations

return ['last_version' => $last_version];
}

private function start_timer() {
return microtime(true);
}

private function end_timer() {
return microtime(true);
}

private function diff_timer($s, $e) {
return $e - $s;
}

private function verify_environment() {
if(!$this->adapter->table_exists(RUCKUSING_TS_SCHEMA_TBL_NAME) ) {
echo "\n\tSchema version table does not exist. Auto-creating.";
$this->auto_create_schema_info_table();
}
}

private function auto_create_schema_info_table() {
try {
echo sprintf("\n\tCreating schema version table: %s", RUCKUSING_TS_SCHEMA_TBL_NAME . "\n\n");
Expand All @@ -256,7 +273,4 @@ private function auto_create_schema_info_table() {
die("\nError auto-creating 'schema_info' table: " . $e->getMessage() . "\n\n");
}
}

}//class

?>
}