diff --git a/src/Pluf/Migration.php b/src/Pluf/Migration.php index 06dcbe7..fd778db 100644 --- a/src/Pluf/Migration.php +++ b/src/Pluf/Migration.php @@ -29,6 +29,7 @@ * * Simple example usage: * + *
  * $m = new Pluf_Migration('MyApp');
  * $m->migrate();
  *
@@ -43,6 +44,7 @@
  *
  * $m = new Pluf_Migration();
  * $m->migrate(3); // migrate (upgrade or downgrade) to version 3
+ * 
* */ class Pluf_Migration @@ -97,7 +99,48 @@ class Pluf_Migration } } - + /** + * Backup the application. + * + * @param string Path to the backup folder + * @param string Backup name (null) + */ + public function backup($path, $name=null) + { + foreach ($this->apps as $app) { + $func = $app.'_Migrations_Backup_run'; + Pluf::loadFunction($func); + if ($this->display) { + echo($func."\n"); + } + if (!$this->dry_run) { + $ret = $func($path, $name); + } + } + return true; + } + + /** + * Restore the application. + * + * @param string Path to the backup folder + * @param string Backup name + */ + public function restore($path, $name) + { + foreach ($this->apps as $app) { + $func = $app.'_Migrations_Backup_restore'; + Pluf::loadFunction($func); + if ($this->display) { + echo($func."\n"); + } + if (!$this->dry_run) { + $ret = $func($path, $name); + } + } + return true; + } + /** * Run the migration. * diff --git a/src/migrate.php b/src/migrate.php index 375fa28..1b06c0f 100644 --- a/src/migrate.php +++ b/src/migrate.php @@ -37,8 +37,9 @@ $debug = false; // Yes a dirty global variable. $search_path = null; $cg = new Console_Getopt(); -$shortoptions = 'aixuc:v:d'; -$longoptions = array('app=', 'version=', 'conf=', 'search-path=', 'include-path='); +$shortoptions = 'aixubrc:v:d'; +$longoptions = array('app=', 'version=', 'conf=', 'search-path=', + 'include-path='); $args = $cg->readPHPArgv(); @@ -56,6 +57,8 @@ function usage() .' Upgrade all: migrate.php --conf=path/to/config.php -a'."\n" .' All to version 3: migrate.php --conf=path/to/config.php -a -v3'."\n" .' Upgrade MyApp: migrate.php --conf=path/to/config.php --app=MyApp'."\n" + .' Backup MyApp: migrate.php --conf=path/to/config.php --app=MyApp -b /path/to/backup/folder [backupname]'."\n" + .' Restore MyApp: migrate.php --conf=path/to/config.php --app=MyApp -r /path/to/backup/folder backupname'."\n" .''."\n" .'Options:'."\n" .' c, --conf: Path to the configuration file.'."\n" @@ -68,6 +71,8 @@ function usage() .' d: Display debug information.'."\n" .' i: Install the application(s).'."\n" .' x: Uninstall the application(s).'."\n" + .' b: Backup the application(s).'."\n" + .' r: Restore the application(s).'."\n" .''."\n" .'Note: The command line parser of PEAR is not very robust'."\n" .' if you have an unexpected error about an offset not'."\n" @@ -100,15 +105,24 @@ $what = array( 'dry_run' => false, 'un-install' => false, 'install' => false, + 'backup' => false, + 'restore' => false, ); $opts = $ret[0]; +$args = $ret[1]; if (sizeof($opts) > 0) { foreach ($opts as $o) { switch ($o[0]) { case 'a': $what['all'] = true; break; + case 'b': + $what['backup'] = true; + break; + case 'r': + $what['restore'] = true; + break; case 'v': case '--version': $what['version'] = $o[1]; @@ -211,6 +225,13 @@ if ($what['install']) { } elseif ($what['un-install']) { debug('Uninstall '.$app_disp); $m->unInstall(); +} elseif ($what['backup']) { + debug('Backup '.$app_disp); + if (!isset($args[1])) $args[1] = null; + $m->backup($args[0], $args[1]); +} elseif ($what['restore']) { + debug('Restore '.$app_disp); + $m->restore($args[0], $args[1]); } else { debug('Migrate '.$app.' to version '.$what['version']); $m->migrate($what['version']);