Skip to content

Commit 07ad488

Browse files
author
Alexandre Guidet
committed
Merge remote-tracking branch 'origin/master' into hotfix-3.6.1
2 parents 09dcca8 + 695b194 commit 07ad488

5 files changed

Lines changed: 58 additions & 30 deletions

File tree

Migrate/Command/AbstractEnvCommand.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Migrate\Migration;
1313
use Migrate\Utils\ArrayUtil;
14-
use SebastianBergmann\GlobalState\RuntimeException;
1514
use Symfony\Component\Config\FileLocator;
1615
use Symfony\Component\Console\Input\InputInterface;
1716
use Symfony\Component\Console\Output\OutputInterface;
@@ -208,21 +207,25 @@ public function removeFromChangelog(Migration $migration)
208207

209208
/**
210209
* @param Migration $migration
210+
* @param bool $changeLogOnly
211211
*/
212-
public function executeUpMigration(Migration $migration)
212+
public function executeUpMigration(Migration $migration, $changeLogOnly = false)
213213
{
214214
$this->getDb()->beginTransaction();
215-
$result = $this->getDb()->exec($migration->getSqlUp());
216-
217-
if ($result === false) {
218-
// error while executing the migration
219-
$errorInfo = "";
220-
$errorInfos = $this->getDb()->errorInfo();
221-
foreach ($errorInfos as $line) {
222-
$errorInfo .= "\n$line";
215+
216+
if ($changeLogOnly === false) {
217+
$result = $this->getDb()->exec($migration->getSqlUp());
218+
219+
if ($result === false) {
220+
// error while executing the migration
221+
$errorInfo = "";
222+
$errorInfos = $this->getDb()->errorInfo();
223+
foreach ($errorInfos as $line) {
224+
$errorInfo .= "\n$line";
225+
}
226+
$this->getDb()->rollBack();
227+
throw new \RuntimeException("migration error, some SQL may be wrong\n\nid: {$migration->getId()}\nfile: {$migration->getFile()}\n" . $errorInfo);
223228
}
224-
$this->getDb()->rollBack();
225-
throw new \RuntimeException("migration error, some SQL may be wrong\n\nid: {$migration->getId()}\nfile: {$migration->getFile()}\n" . $errorInfo);
226229
}
227230

228231
$this->saveToChangelog($migration);
@@ -231,21 +234,25 @@ public function executeUpMigration(Migration $migration)
231234

232235
/**
233236
* @param Migration $migration
237+
* @param bool $changeLogOnly
234238
*/
235-
public function executeDownMigration(Migration $migration)
239+
public function executeDownMigration(Migration $migration, $changeLogOnly = false)
236240
{
237241
$this->getDb()->beginTransaction();
238-
$result = $this->getDb()->exec($migration->getSqlDown());
239-
240-
if ($result === false) {
241-
// error while executing the migration
242-
$errorInfo = "";
243-
$errorInfos = $this->getDb()->errorInfo();
244-
foreach ($errorInfos as $line) {
245-
$errorInfo .= "\n$line";
242+
243+
if ($changeLogOnly === false) {
244+
$result = $this->getDb()->exec($migration->getSqlDown());
245+
246+
if ($result === false) {
247+
// error while executing the migration
248+
$errorInfo = "";
249+
$errorInfos = $this->getDb()->errorInfo();
250+
foreach ($errorInfos as $line) {
251+
$errorInfo .= "\n$line";
252+
}
253+
$this->getDb()->rollBack();
254+
throw new \RuntimeException("migration error, some SQL may be wrong\n\nid: {$migration->getId()}\nfile: {$migration->getFile()}\n" . $errorInfo);
246255
}
247-
$this->getDb()->rollBack();
248-
throw new \RuntimeException("migration error, some SQL may be wrong\n\nid: {$migration->getId()}\nfile: {$migration->getFile()}\n" . $errorInfo);
249256
}
250257
$this->removeFromChangelog($migration);
251258
$this->getDb()->commit();

Migrate/Command/DownCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ protected function configure()
4141
InputOption::VALUE_REQUIRED,
4242
'If you need to down this migration id only'
4343
)
44+
->addOption(
45+
'changelog-only',
46+
null,
47+
InputOption::VALUE_NONE,
48+
'Mark as applied without executing SQL '
49+
)
4450
;
4551
}
4652

@@ -50,6 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5056

5157
$this->init($input, $output);
5258

59+
$changeLogOnly = (bool) $input->getOption('changelog-only');
5360
/* @var $questions QuestionHelper */
5461
$questions = $this->getHelperSet()->get('question');
5562

@@ -75,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7582
/* @var $migration \Migrate\Migration */
7683
foreach ($toExecute as $migration) {
7784
$progress->setMessage($migration->getDescription());
78-
$this->executeDownMigration($migration);
85+
$this->executeDownMigration($migration, $changeLogOnly);
7986
$progress->advance();
8087
}
8188

Migrate/Command/UpCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ protected function configure()
3838
InputOption::VALUE_REQUIRED,
3939
'If you need to up this migration id only'
4040
)
41+
->addOption(
42+
'changelog-only',
43+
null,
44+
InputOption::VALUE_NONE,
45+
'Mark as applied without executing SQL '
46+
)
4147
;
4248
}
4349

@@ -47,6 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4753

4854
$this->init($input, $output);
4955

56+
$changeLogOnly = (bool) $input->getOption('changelog-only');
5057
$toExecute = $this->filterMigrationsToExecute($input, $output);
5158

5259
if (count($toExecute) == 0) {
@@ -64,7 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6471
/* @var $migration \Migrate\Migration */
6572
foreach ($toExecute as $migration) {
6673
$progress->setMessage($migration->getDescription());
67-
$this->executeUpMigration($migration);
74+
$this->executeUpMigration($migration, $changeLogOnly);
6875
$progress->advance();
6976
}
7077

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PhpDbMigration - full PHP database migration tool
33

44
[![Build Status](https://travis-ci.org/alwex/php-database-migration.svg?branch=master)](https://travis-ci.org/alwex/php-database-migration)
55
[![Packagist](https://img.shields.io/packagist/dt/php-database-migration/php-database-migration.svg?maxAge=2592000)]()
6+
[![Version](http://img.shields.io/packagist/v/php-database-migration/php-database-migration.svg?style=flat)](https://packagist.org/packages/php-database-migration/php-database-migration)
67

78
This is a full standalone PHP tool based on [Symfony Console](http://symfony.com/doc/current/components/console)
89
and inspired by the Rails database migration tool and MyBatis. It merges the functionality of the two tools and
@@ -118,6 +119,12 @@ You can now up all the pending migrations. If you decided to down a migration, t
118119
$ ./bin/migrate migrate:up [env]
119120
```
120121

122+
You can mark migrations as applied without executing SQL (e.g. if you switched from another migration system)
123+
124+
```
125+
$ ./bin/migrate migrate:up [env] --changelog-only
126+
```
127+
121128
For developement purpose, it is also possible to up a single migration without taking care of the other ones:
122129

123130
```

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)