Skip to content

Commit 4370cd1

Browse files
committed
Add the UPDATE (the system choose if UP or DOWN the database
1 parent 6296c71 commit 4370cd1

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

scripts/migrate

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ $application->add(new \ByJG\DbMigration\Console\DownCommand());
2020
$application->add(new \ByJG\DbMigration\Console\CreateCommand());
2121
$application->add(new \ByJG\DbMigration\Console\DatabaseVersionCommand());
2222
$application->add(new \ByJG\DbMigration\Console\InstallCommand());
23+
$application->add(new \ByJG\DbMigration\Console\UpdateCommand());
2324
$application->run();

src/Console/UpdateCommand.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jg
5+
* Date: 17/06/16
6+
* Time: 21:52
7+
*/
8+
9+
namespace ByJG\DbMigration\Console;
10+
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
use Symfony\Component\Console\Question\ConfirmationQuestion;
14+
15+
class UpdateCommand extends ConsoleCommand
16+
{
17+
protected function configure()
18+
{
19+
parent::configure();
20+
$this
21+
->setName('update')
22+
->setDescription('Migrate Up or Down the database version based on the current database version and the ' .
23+
'migration scripts available'
24+
);
25+
26+
}
27+
28+
protected function execute(InputInterface $input, OutputInterface $output)
29+
{
30+
$versionInfo = $this->migration->getCurrentVersion();
31+
if (strpos($versionInfo['status'], 'partial') !== false) {
32+
$helper = $this->getHelper('question');
33+
$question = new ConfirmationQuestion(
34+
'The database was not fully updated and maybe be unstable. Did you really want migrate the version? (y/N) ',
35+
false
36+
);
37+
38+
if (!$helper->ask($input, $output, $question)) {
39+
$output->writeln('Aborted.');
40+
return;
41+
}
42+
}
43+
44+
parent::execute($input, $output);
45+
$this->migration->update($this->upTo, true);
46+
}
47+
}

src/Migration.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public function getMigrationSql($version, $increment)
126126
return $file;
127127
}
128128
}
129+
return null;
129130
}
130131

131132
/**
@@ -239,6 +240,23 @@ public function up($upVersion = null, $force = false)
239240
$this->migrate($upVersion, 1, $force);
240241
}
241242

243+
/**
244+
* Run all scripts to up or down the database version from current up to latest version or the specified version.
245+
*
246+
* @param int $upVersion
247+
* @param bool $force
248+
*/
249+
public function update($upVersion = null, $force = false)
250+
{
251+
$versionInfo = $this->getCurrentVersion();
252+
$version = intval($versionInfo['version']);
253+
$increment = 1;
254+
if ($upVersion !== null && $upVersion < $version) {
255+
$increment = -1;
256+
}
257+
$this->migrate($upVersion, $increment, $force);
258+
}
259+
242260
/**
243261
* Run all scripts to down the database version from current version up to the specified version.
244262
*

0 commit comments

Comments
 (0)