-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add full-rollback option for production migrations
Usecase: running migrations during deploy should not alter the database at all. It’s handy to only rollback the last migration during development, so the original behaviour is retained. Side effect: migrations are committed at once, not per single migration. Also affects original continue mode, not just full rollback mode. Original behaviour could have caused transient problems during deploy.
- Loading branch information
Showing
9 changed files
with
118 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/** | ||
* @testCase | ||
* @dataProvider ../../dbals.ini | ||
*/ | ||
|
||
namespace NextrasTests\Migrations; | ||
|
||
use Mockery; | ||
use Nextras\Migrations\Engine\Runner; | ||
use Nextras\Migrations\Entities\Group; | ||
use Tester; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../../bootstrap.php'; | ||
|
||
|
||
class RollbackTest extends IntegrationTestCase | ||
{ | ||
|
||
protected function getGroups($dir) | ||
{ | ||
$rollback = new Group(); | ||
$rollback->enabled = TRUE; | ||
$rollback->name = 'rollback'; | ||
$rollback->directory = $dir . '/rollback'; | ||
$rollback->dependencies = []; | ||
|
||
return [$rollback]; | ||
} | ||
|
||
public function testContinueRollbacksFailingOnly() | ||
{ | ||
try { | ||
$this->runner->run(Runner::MODE_CONTINUE); | ||
} catch (\Exception $e) { | ||
} | ||
|
||
$res = $this->dbal->query(' | ||
SELECT Count(*) ' . $this->dbal->escapeIdentifier('count') . ' FROM information_schema.tables | ||
WHERE TABLE_NAME = ' . $this->dbal->escapeString('rollback') . ' | ||
AND table_catalog = ' . $this->dbal->escapeString('nextras_migrations_test') . ' | ||
AND table_schema = ' . $this->dbal->escapeString($this->dbName) . ' | ||
'); | ||
$tableExists = (bool) $res[0]['count']; | ||
|
||
Assert::true($tableExists); | ||
Assert::count(2, $this->driver->getAllMigrations()); | ||
} | ||
|
||
public function testFullRollback() | ||
{ | ||
$this->driver->createTable(); | ||
|
||
try { | ||
$this->runner->run(Runner::MODE_CONTINUE_FULL_ROLLBACK); | ||
} catch (\Exception $e) { | ||
} | ||
|
||
$res = $this->dbal->query(' | ||
SELECT Count(*) ' . $this->dbal->escapeIdentifier('count') . ' FROM information_schema.tables | ||
WHERE TABLE_NAME = ' . $this->dbal->escapeString('rollback') . ' | ||
AND table_catalog = ' . $this->dbal->escapeString('nextras_migrations_test') . ' | ||
AND table_schema = ' . $this->dbal->escapeString($this->dbName) . ' | ||
'); | ||
$tableExists = (bool) $res[0]['count']; | ||
|
||
Assert::false($tableExists); | ||
Assert::count(0, $this->driver->getAllMigrations()); | ||
} | ||
|
||
} | ||
|
||
|
||
(new RollbackTest)->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CREATE TABLE `rollback` ( | ||
`id` bigint NOT NULL, | ||
PRIMARY KEY (`id`) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
INSERT INTO `rollback` (`id`) VALUES (1), (2), (3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
INSERT INTO `rollback` (`id`) VALUES (3); -- duplicate key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CREATE TABLE "rollback" ( | ||
"id" serial4 NOT NULL, | ||
PRIMARY KEY ("id") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
INSERT INTO "rollback" ("id") VALUES (1), (2), (3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
INSERT INTO "rollback" ("id") VALUES (3); -- duplicate key |