Skip to content

Instantly share code, notes, and snippets.

@cakebaker
Created April 2, 2009 07:15
Show Gist options
  • Save cakebaker/89069 to your computer and use it in GitHub Desktop.
Save cakebaker/89069 to your computer and use it in GitHub Desktop.
CakePHP shell script to run NoseRub's migrations
<?php
/**
* A simple shell script to run NoseRub's migrations.
*
* Requires the Migration model from NoseRub (http://noserub.com) and a
* folder app/config/sql/migrations for the migrations.
*
* Copyright (c) by Daniel Hofstetter (http://cakebaker.42dh.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*/
class MigrateShell extends Shell {
public function main() {
$this->Migration = ClassRegistry::init('Migration');
$databaseStatus = $this->Migration->getDatabaseStatus();
if ($databaseStatus == 1) {
$currentMigration = $this->Migration->getCurrentMigration();
$mostRecentMigration = $this->Migration->getMostRecentMigration();
$this->out('Actual version: #'. $currentMigration);
$this->out('Updating to #'. $mostRecentMigration);
if ($currentMigration < $mostRecentMigration) {
$migrations = $this->Migration->getOpenMigrations($currentMigration);
$this->Migration->migrate($migrations, $currentMigration, $mostRecentMigration);
$this->out('Applying migrations:');
foreach ($migrations['sql'] as $idx => $migration) {
$this->out($migration['name']);
if (isset($migrations['php'][$idx]['name'])) {
$this->out($migrations['php'][$idx]['name']);
}
}
} else {
$this->out('Database up-to-date!');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment