-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add full-rollback option for production migrations #19
Open
Mikulas
wants to merge
1
commit into
nextras:master
Choose a base branch
from
Mikulas:full-rollback
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably wrong. It migrations contains more sqls and only the last one is wrong (throws exception), all previous queries are commited, aren't they?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've retained original transactions that wrap single file. This should only affect the new outer one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are actually right; this won't work with mysql (because it has terrible support for nested transactions). I will have to rewrite this for other than postgres driver which I initially had in mind when implementing this.