Skip to content

Commit

Permalink
Merge pull request #13 from joegreen88/master
Browse files Browse the repository at this point in the history
Added option: install-provisional-version
  • Loading branch information
joegreen88 committed Jan 16, 2016
2 parents 5c5bf66 + 86bfe17 commit f688e6f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Smrtr/MysqlVersionControl/TeardownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
85 changes: 65 additions & 20 deletions src/Smrtr/MysqlVersionControl/UpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
class UpCommand extends Command
{
/**
* @var string
*/
const DEFAULT_PROVISIONAL_VERSION_NAME = 'new';

/**
* @var string
*/
protected $env;

public function __construct($env)
Expand Down Expand Up @@ -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
)
;
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -138,17 +159,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
$output->writeln('Available version: '.$availableVersion);

if ($currentVersion >= $availableVersion) {
$output->writeln('<info>Database version is already up to date.</info>');
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
Expand All @@ -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('<info>Database version is already up to date.</info>');
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";

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f688e6f

Please sign in to comment.