File tree Expand file tree Collapse file tree 3 files changed +66
-0
lines changed
Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Original file line number Diff line number Diff 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 ();
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 *
You can’t perform that action at this time.
0 commit comments