Skip to content

Commit

Permalink
Restore UNIQUE_CHECKS mysql variable to its original value when done …
Browse files Browse the repository at this point in the history
…with changing the database structure. This prevents potentially inserting duplicated values while manipulating data in the setup:upgrade command.
  • Loading branch information
hostep committed Feb 25, 2021
1 parent d3f202a commit 2979876
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ public function getDestructiveOperations()
private function startSetupForAllConnections()
{
foreach ($this->sharding->getResources() as $resource) {
$this->resourceConnection->getConnection($resource)
->startSetup();
$this->resourceConnection->getConnection($resource)
->query('SET UNIQUE_CHECKS=0');
$connection = $this->resourceConnection->getConnection($resource);

$connection->startSetup();
$connection->query('SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0');
}
}

Expand All @@ -148,8 +148,10 @@ private function startSetupForAllConnections()
private function endSetupForAllConnections()
{
foreach ($this->sharding->getResources() as $resource) {
$this->resourceConnection->getConnection($resource)
->endSetup();
$connection = $this->resourceConnection->getConnection($resource);

$connection->query('SET UNIQUE_CHECKS=IF(@OLD_UNIQUE_CHECKS=0, 0, 1)');
$connection->endSetup();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function testExecute()
$connectionMock = $this->getMockBuilder(Mysql::class)
->disableOriginalConstructor()
->getMock();
$this->resourceConnectionMock->expects(self::exactly(3))
$this->resourceConnectionMock->expects(self::exactly(2))
->method('getConnection')
->with('default')
->willReturn($connectionMock);
Expand Down

0 comments on commit 2979876

Please sign in to comment.