-
Notifications
You must be signed in to change notification settings - Fork 39
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
115 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,73 @@ | ||
<?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]; | ||
} | ||
|
||
|
||
/** | ||
* @param $mode | ||
* @return bool table exists | ||
*/ | ||
private function runInMode($mode) | ||
{ | ||
try { | ||
$this->runner->run($mode); | ||
} 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_schema = ' . $this->dbal->escapeString($this->dbName) . ' | ||
'); | ||
return (bool) $res[0]['count']; | ||
} | ||
|
||
|
||
public function testContinueRollbacksFailingOnly() | ||
{ | ||
Assert::true($this->runInMode(Runner::MODE_CONTINUE)); | ||
Assert::count(2, $this->driver->getAllMigrations()); | ||
} | ||
|
||
|
||
public function testFullRollback() | ||
{ | ||
$this->driver->createTable(); | ||
|
||
Assert::false($this->runInMode(Runner::MODE_CONTINUE_FULL_ROLLBACK)); | ||
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 |