Skip to content

Commit f688e6f

Browse files
committed
Merge pull request #13 from joegreen88/master
Added option: install-provisional-version
2 parents 5c5bf66 + 86bfe17 commit f688e6f

File tree

2 files changed

+66
-21
lines changed

2 files changed

+66
-21
lines changed

src/Smrtr/MysqlVersionControl/TeardownCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function configure()
2727
{
2828
$this
2929
->setName($this->env)
30-
->setDescription('Install the '.$this->env.' versions')
30+
->setDescription('Tear down the '.$this->env.' database tables')
3131
->addArgument(
3232
'mysqlbin',
3333
InputArgument::OPTIONAL,

src/Smrtr/MysqlVersionControl/UpCommand.php

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
*/
1717
class UpCommand extends Command
1818
{
19+
/**
20+
* @var string
21+
*/
22+
const DEFAULT_PROVISIONAL_VERSION_NAME = 'new';
23+
24+
/**
25+
* @var string
26+
*/
1927
protected $env;
2028

2129
public function __construct($env)
@@ -46,6 +54,19 @@ protected function configure()
4654
InputOption::VALUE_REQUIRED,
4755
'Optional custom path to database versions directory'
4856
)
57+
->addOption(
58+
'install-provisional-version',
59+
null,
60+
InputOption::VALUE_NONE,
61+
'Install a provisional version which may still be in development and is not final.'
62+
)
63+
->addOption(
64+
'provisional-version',
65+
null,
66+
InputOption::VALUE_REQUIRED,
67+
'The name of the provisional version',
68+
static::DEFAULT_PROVISIONAL_VERSION_NAME
69+
)
4970
;
5071
}
5172

@@ -83,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
83104
$output->writeln('Installing version control...');
84105

85106
$result = $buildConf->prepare(
86-
"CREATE TABLE `db_config`
107+
"CREATE TABLE `db_config`
87108
(
88109
`key` VARCHAR(50) COLLATE 'utf8_general_ci' NOT NULL,
89110
`value` TEXT,
@@ -138,17 +159,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
138159
}
139160
$output->writeln('Available version: '.$availableVersion);
140161

141-
if ($currentVersion >= $availableVersion) {
142-
$output->writeln('<info>Database version is already up to date.</info>');
143-
return 0;
144-
}
145-
146-
$noun = ($availableVersion - $currentVersion > 1) ? 'updates' : 'update';
147-
$output->writeln(
148-
"Installing database $noun (Current version: $currentVersion, Available version: $availableVersion)..."
149-
);
150-
151-
// go from current to latest version, building stack of SQL files
152162
$filesToLookFor = [];
153163
if (!$input->getOption('no-schema')) {
154164
$filesToLookFor[] = 'schema.sql'; // structural changes, alters, creates, drops
@@ -160,20 +170,55 @@ protected function execute(InputInterface $input, OutputInterface $output)
160170
$filesToLookFor[] = 'runme.php'; // custom php hook
161171

162172
$stack = array();
163-
for ($i = $currentVersion + 1; $i <= $availableVersion; $i++) {
173+
if ($currentVersion < $availableVersion) {
174+
for ($i = $currentVersion + 1; $i <= $availableVersion; $i++) {
164175

165-
$path = $versionsPath.DIRECTORY_SEPARATOR.$i;
166-
if (!is_dir($path) || !is_readable($path)) {
167-
continue;
176+
$path = $versionsPath.DIRECTORY_SEPARATOR.$i;
177+
if (!is_dir($path) || !is_readable($path)) {
178+
continue;
179+
}
180+
181+
foreach ($filesToLookFor as $file) {
182+
if (is_readable($path.DIRECTORY_SEPARATOR.$file) && is_file($path.DIRECTORY_SEPARATOR.$file)) {
183+
$stack[$i][$file] = $path.DIRECTORY_SEPARATOR.$file;
184+
}
185+
}
168186
}
187+
}
188+
189+
// Look for a provisional version?
190+
$provisionalVersion = null;
191+
if ($input->getOption('install-provisional-version')) {
192+
193+
$provisionalVersion = $input->getOption('provisional-version');
194+
$output->writeln('Provisional version: '.$provisionalVersion);
195+
196+
$path = $versionsPath.DIRECTORY_SEPARATOR.$provisionalVersion;
197+
if (is_readable($path) && is_dir($path)) {
169198

170-
foreach ($filesToLookFor as $file) {
171-
if (is_readable($path.DIRECTORY_SEPARATOR.$file)) {
172-
$stack[$i][$file] = $path.DIRECTORY_SEPARATOR.$file;
199+
foreach ($filesToLookFor as $file) {
200+
if (is_readable($path.DIRECTORY_SEPARATOR.$file) && is_file($path.DIRECTORY_SEPARATOR.$file)) {
201+
$stack[$provisionalVersion][$file] = $path.DIRECTORY_SEPARATOR.$file;
202+
}
173203
}
174204
}
175205
}
176206

207+
$updates = count($stack);
208+
if (!$updates) {
209+
$output->writeln('<info>Database version is already up to date.</info>');
210+
return 0;
211+
}
212+
213+
$noun = ($updates > 1) ? 'updates' : 'update';
214+
$report = "Current version: $currentVersion, Available version: $availableVersion";
215+
if (is_string($provisionalVersion) && array_key_exists($provisionalVersion, $stack)) {
216+
$report .= ", Provisional version: $provisionalVersion";
217+
}
218+
$output->writeln(
219+
"Installing database $noun ($report)..."
220+
);
221+
177222
$s = '\\' == DIRECTORY_SEPARATOR ? "%s" : "'%s'"; // Windows doesn't like quoted params
178223
$cmdMySQL = "$mysqlbin -h $s --user=$s --password=$s --database=$s < %s";
179224

@@ -232,7 +277,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
232277
}
233278
}
234279

235-
if ($result) {
280+
if ($result && is_int($version)) {
236281
$result = $buildConf->query(
237282
"REPLACE INTO `db_config` (`key`, `value`, `updated_at`) VALUES ('version', $version, now())"
238283
)->execute();

0 commit comments

Comments
 (0)