Skip to content

Commit 82a276e

Browse files
author
Alexandre Guidet
committed
Merge remote-tracking branch 'origin/stable'
2 parents 27892f5 + 8884623 commit 82a276e

File tree

6 files changed

+142
-4
lines changed

6 files changed

+142
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ clean.sh
77
.idea
88
tests/test.sqlite
99
vendor
10+
/bin/phpunit
1011
.php-database-migration
11-
.twgit_features_subject
12+
.twgit_features_subject

Migrate/Command/AbstractEnvCommand.php

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

1212
use Migrate\Migration;
1313
use Migrate\Utils\ArrayUtil;
14+
use SebastianBergmann\GlobalState\RuntimeException;
1415
use Symfony\Component\Config\FileLocator;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Output\OutputInterface;
@@ -52,6 +53,13 @@ public function getChangelogTable()
5253
return ArrayUtil::get($this->getConfig(), 'changelog');
5354
}
5455

56+
protected function checkEnv()
57+
{
58+
if (!file_exists(getcwd() . '/.php-database-migration/environments')) {
59+
throw new \RuntimeException("you are not in an initialized php-database-migration directory");
60+
}
61+
}
62+
5563
protected function init(InputInterface $input, OutputInterface $output, $env = null)
5664
{
5765
$configDirectory = array(getcwd() . '/.php-database-migration/environments');
@@ -204,17 +212,44 @@ public function removeFromChangelog(Migration $migration)
204212
*/
205213
public function executeUpMigration(Migration $migration)
206214
{
207-
$this->getDb()->query($migration->getSqlUp());
215+
$this->getDb()->beginTransaction();
216+
$result = $this->getDb()->exec($migration->getSqlUp());
217+
218+
if ($result === false) {
219+
// error while executing the migration
220+
$errorInfo = "";
221+
$errorInfos = $this->getDb()->errorInfo();
222+
foreach ($errorInfos as $line) {
223+
$errorInfo .= "\n$line";
224+
}
225+
$this->getDb()->rollBack();
226+
throw new \RuntimeException("migration error, some SQL may be wrong\n\nid: {$migration->getId()}\nfile: {$migration->getFile()}\n" . $errorInfo);
227+
}
228+
208229
$this->saveToChangelog($migration);
230+
$this->getDb()->commit();
209231
}
210232

211233
/**
212234
* @param Migration $migration
213235
*/
214236
public function executeDownMigration(Migration $migration)
215237
{
216-
$this->getDb()->query($migration->getSqlDown());
238+
$this->getDb()->beginTransaction();
239+
$result = $this->getDb()->exec($migration->getSqlDown());
240+
241+
if ($result === false) {
242+
// error while executing the migration
243+
$errorInfo = "";
244+
$errorInfos = $this->getDb()->errorInfo();
245+
foreach ($errorInfos as $line) {
246+
$errorInfo .= "\n$line";
247+
}
248+
$this->getDb()->rollBack();
249+
throw new \RuntimeException("migration error, some SQL may be wrong\n\nid: {$migration->getId()}\nfile: {$migration->getFile()}\n" . $errorInfo);
250+
}
217251
$this->removeFromChangelog($migration);
252+
$this->getDb()->commit();
218253
}
219254

220255
protected function filterMigrationsToExecute(InputInterface $input, OutputInterface $output)

Migrate/Command/CreateCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ protected function configure()
2828

2929
protected function execute(InputInterface $input, OutputInterface $output)
3030
{
31+
$this->checkEnv();
32+
3133
/* @var $questions QuestionHelper */
3234
$questions = $this->getHelperSet()->get('question');
3335

Migrate/Command/DownCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ protected function configure()
4646

4747
protected function execute(InputInterface $input, OutputInterface $output)
4848
{
49+
$this->checkEnv();
50+
4951
$this->init($input, $output);
5052

5153
/* @var $questions QuestionHelper */

Migrate/Command/UpCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ protected function configure()
4343

4444
protected function execute(InputInterface $input, OutputInterface $output)
4545
{
46+
$this->checkEnv();
47+
4648
$this->init($input, $output);
4749

4850
$toExecute = $this->filterMigrationsToExecute($input, $output);

tests/Command/UpDownCommandTest.php

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function setUp()
2828
$this->initEnv();
2929

3030
$this->createMigration('0', "CREATE TABLE test (id INTEGER, thevalue TEXT);", "DROP TABLE test;");
31-
$this->createMigration('1', "INSERT INTO test VALUES (1, 'one');", "DELETE FROM test WHERE id = 1;");
31+
$this->createMigration('1', "SELECT 1", "DELETE FROM test WHERE id = 1;");
3232
$this->createMigration('2', "INSERT INTO test VALUES (2, 'two');", "DELETE FROM test WHERE id = 2;");
3333

3434
self::$application = new Application();
@@ -42,6 +42,50 @@ public function tearDown()
4242
$this->cleanEnv();
4343
}
4444

45+
/**
46+
* @expectedException \RuntimeException
47+
*/
48+
public function testUpMigrationWithError()
49+
{
50+
$this->createMigration('3', "SELECT ;", "SELECT ;");
51+
$command = self::$application->find('migrate:up');
52+
$commandTester = new CommandTester($command);
53+
54+
$commandTester->execute(array(
55+
'command' => $command->getName(),
56+
'env' => 'testing'
57+
));
58+
}
59+
60+
/**
61+
* @expectedException \RuntimeException
62+
*/
63+
public function testDownMigrationWithError()
64+
{
65+
$this->createMigration('3', "SELECT 1;", "SELECT ;");
66+
67+
68+
$command = self::$application->find('migrate:up');
69+
$commandTester = new CommandTester($command);
70+
71+
$commandTester->execute(array(
72+
'command' => $command->getName(),
73+
'env' => 'testing'
74+
));
75+
76+
$command = self::$application->find('migrate:down');
77+
$commandTester = new CommandTester($command);
78+
79+
/* @var $question QuestionHelper */
80+
$question = $command->getHelper('question');
81+
$question->setInputStream(InputStreamUtil::type("yes\n"));
82+
83+
$commandTester->execute(array(
84+
'command' => $command->getName(),
85+
'env' => 'testing'
86+
));
87+
}
88+
4589
public function testUpAllPendingMigrations()
4690
{
4791

@@ -308,4 +352,56 @@ public function testDownTo()
308352

309353
$this->assertEquals($expected, $commandTester->getDisplay());
310354
}
355+
356+
/**
357+
* @expectedException \RuntimeException
358+
* @expectedExceptionMessage you are not in an initialized php-database-migration directory
359+
*/
360+
public function testUpInANotInitializedDirectory()
361+
{
362+
$this->cleanEnv();
363+
364+
$command = self::$application->find('migrate:up');
365+
$commandTester = new CommandTester($command);
366+
367+
$commandTester->execute(array(
368+
'command' => $command->getName(),
369+
'env' => 'testing',
370+
));
371+
372+
$command = self::$application->find('migrate:down');
373+
$commandTester = new CommandTester($command);
374+
375+
$commandTester->execute(array(
376+
'command' => $command->getName(),
377+
'env' => 'testing',
378+
'--to' => '1'
379+
));
380+
}
381+
382+
/**
383+
* @expectedException \RuntimeException
384+
* @expectedExceptionMessage you are not in an initialized php-database-migration directory
385+
*/
386+
public function testDownInANotInitializedDirectory()
387+
{
388+
$this->cleanEnv();
389+
390+
$command = self::$application->find('migrate:down');
391+
$commandTester = new CommandTester($command);
392+
393+
$commandTester->execute(array(
394+
'command' => $command->getName(),
395+
'env' => 'testing',
396+
));
397+
398+
$command = self::$application->find('migrate:down');
399+
$commandTester = new CommandTester($command);
400+
401+
$commandTester->execute(array(
402+
'command' => $command->getName(),
403+
'env' => 'testing',
404+
'--to' => '1'
405+
));
406+
}
311407
}

0 commit comments

Comments
 (0)