diff --git a/src/Smrtr/MysqlVersionControl/TeardownCommand.php b/src/Smrtr/MysqlVersionControl/TeardownCommand.php index b01f933..5316ff2 100644 --- a/src/Smrtr/MysqlVersionControl/TeardownCommand.php +++ b/src/Smrtr/MysqlVersionControl/TeardownCommand.php @@ -27,7 +27,7 @@ protected function configure() { $this ->setName($this->env) - ->setDescription('Install the '.$this->env.' versions') + ->setDescription('Tear down the '.$this->env.' database tables') ->addArgument( 'mysqlbin', InputArgument::OPTIONAL, diff --git a/src/Smrtr/MysqlVersionControl/UpCommand.php b/src/Smrtr/MysqlVersionControl/UpCommand.php index 104fbb7..1ba8d45 100644 --- a/src/Smrtr/MysqlVersionControl/UpCommand.php +++ b/src/Smrtr/MysqlVersionControl/UpCommand.php @@ -16,6 +16,14 @@ */ class UpCommand extends Command { + /** + * @var string + */ + const DEFAULT_PROVISIONAL_VERSION_NAME = 'new'; + + /** + * @var string + */ protected $env; public function __construct($env) @@ -46,6 +54,19 @@ protected function configure() InputOption::VALUE_REQUIRED, 'Optional custom path to database versions directory' ) + ->addOption( + 'install-provisional-version', + null, + InputOption::VALUE_NONE, + 'Install a provisional version which may still be in development and is not final.' + ) + ->addOption( + 'provisional-version', + null, + InputOption::VALUE_REQUIRED, + 'The name of the provisional version', + static::DEFAULT_PROVISIONAL_VERSION_NAME + ) ; } @@ -83,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln('Installing version control...'); $result = $buildConf->prepare( -"CREATE TABLE `db_config` + "CREATE TABLE `db_config` ( `key` VARCHAR(50) COLLATE 'utf8_general_ci' NOT NULL, `value` TEXT, @@ -138,17 +159,6 @@ protected function execute(InputInterface $input, OutputInterface $output) } $output->writeln('Available version: '.$availableVersion); - if ($currentVersion >= $availableVersion) { - $output->writeln('Database version is already up to date.'); - return 0; - } - - $noun = ($availableVersion - $currentVersion > 1) ? 'updates' : 'update'; - $output->writeln( - "Installing database $noun (Current version: $currentVersion, Available version: $availableVersion)..." - ); - - // go from current to latest version, building stack of SQL files $filesToLookFor = []; if (!$input->getOption('no-schema')) { $filesToLookFor[] = 'schema.sql'; // structural changes, alters, creates, drops @@ -160,20 +170,55 @@ protected function execute(InputInterface $input, OutputInterface $output) $filesToLookFor[] = 'runme.php'; // custom php hook $stack = array(); - for ($i = $currentVersion + 1; $i <= $availableVersion; $i++) { + if ($currentVersion < $availableVersion) { + for ($i = $currentVersion + 1; $i <= $availableVersion; $i++) { - $path = $versionsPath.DIRECTORY_SEPARATOR.$i; - if (!is_dir($path) || !is_readable($path)) { - continue; + $path = $versionsPath.DIRECTORY_SEPARATOR.$i; + if (!is_dir($path) || !is_readable($path)) { + continue; + } + + foreach ($filesToLookFor as $file) { + if (is_readable($path.DIRECTORY_SEPARATOR.$file) && is_file($path.DIRECTORY_SEPARATOR.$file)) { + $stack[$i][$file] = $path.DIRECTORY_SEPARATOR.$file; + } + } } + } + + // Look for a provisional version? + $provisionalVersion = null; + if ($input->getOption('install-provisional-version')) { + + $provisionalVersion = $input->getOption('provisional-version'); + $output->writeln('Provisional version: '.$provisionalVersion); + + $path = $versionsPath.DIRECTORY_SEPARATOR.$provisionalVersion; + if (is_readable($path) && is_dir($path)) { - foreach ($filesToLookFor as $file) { - if (is_readable($path.DIRECTORY_SEPARATOR.$file)) { - $stack[$i][$file] = $path.DIRECTORY_SEPARATOR.$file; + foreach ($filesToLookFor as $file) { + if (is_readable($path.DIRECTORY_SEPARATOR.$file) && is_file($path.DIRECTORY_SEPARATOR.$file)) { + $stack[$provisionalVersion][$file] = $path.DIRECTORY_SEPARATOR.$file; + } } } } + $updates = count($stack); + if (!$updates) { + $output->writeln('Database version is already up to date.'); + return 0; + } + + $noun = ($updates > 1) ? 'updates' : 'update'; + $report = "Current version: $currentVersion, Available version: $availableVersion"; + if (is_string($provisionalVersion) && array_key_exists($provisionalVersion, $stack)) { + $report .= ", Provisional version: $provisionalVersion"; + } + $output->writeln( + "Installing database $noun ($report)..." + ); + $s = '\\' == DIRECTORY_SEPARATOR ? "%s" : "'%s'"; // Windows doesn't like quoted params $cmdMySQL = "$mysqlbin -h $s --user=$s --password=$s --database=$s < %s"; @@ -232,7 +277,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } } - if ($result) { + if ($result && is_int($version)) { $result = $buildConf->query( "REPLACE INTO `db_config` (`key`, `value`, `updated_at`) VALUES ('version', $version, now())" )->execute();