forked from pimcore/pimcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bug]: Fix unique constraint issue when updating advanced many-to-many (
pimcore#17344) * draft to run tests * Apply php-cs-fixer changes * readd migration * Apply php-cs-fixer changes * fix merge mistake --------- Co-authored-by: kingjia90 <[email protected]>
- Loading branch information
Showing
4 changed files
with
153 additions
and
39 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
bundles/CoreBundle/src/Migrations/Version20241025101923.php
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,142 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\CoreBundle\Migrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
use Pimcore\Model\Dao\AbstractDao; | ||
|
||
final class Version20241025101923 extends AbstractMigration | ||
{ | ||
private const ID_COLUMN = 'id'; | ||
|
||
private const O_PREFIX = 'o_'; | ||
|
||
private const PK_COLUMNS = '`' . self::ID_COLUMN . | ||
'`,`dest_id`, `type`, `fieldname`, `column`, `ownertype`, `ownername`, `position`, `index`'; | ||
|
||
private const UNIQUE_KEY_NAME = 'metadata_un'; | ||
|
||
private const AUTO_ID = 'auto_id'; | ||
|
||
public function getDescription(): string | ||
{ | ||
return 'Follow up migrating object_metadata schema to have a auto increment column'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
$this->addSql('SET foreign_key_checks = 0'); | ||
|
||
$metaDataTables = $this->connection->fetchAllAssociative( | ||
"SHOW FULL TABLES | ||
WHERE `Tables_in_{$this->connection->getDatabase()}` | ||
LIKE 'object_metadata_%' AND Table_type = 'BASE TABLE'" | ||
); | ||
|
||
foreach ($metaDataTables as $table) { | ||
$tableName = current($table); | ||
$metaDataTable = $schema->getTable($tableName); | ||
$foreignKeyName = AbstractDao::getForeignKeyName($tableName, self::ID_COLUMN); | ||
$foreignKeyNameWithOPrefix = AbstractDao::getForeignKeyName($tableName, self::O_PREFIX . self::ID_COLUMN); | ||
|
||
if (!$metaDataTable->hasColumn(self::AUTO_ID)) { | ||
if ($recreateForeignKey = $metaDataTable->hasForeignKey($foreignKeyName)) { | ||
$this->addSql('ALTER TABLE `' . $tableName . '` DROP FOREIGN KEY `' . $foreignKeyName . '`'); | ||
} elseif ($recreateForeignKey = $metaDataTable->hasForeignKey($foreignKeyNameWithOPrefix)) { | ||
$this->addSql('ALTER TABLE `' . $tableName . '` DROP FOREIGN KEY `' . $foreignKeyNameWithOPrefix . '`'); | ||
} | ||
|
||
if ($metaDataTable->getPrimaryKey()) { | ||
$this->addSql('ALTER TABLE `' . $tableName . '` DROP PRIMARY KEY'); | ||
} | ||
|
||
$this->addSql('ALTER TABLE ' . $tableName . ' ADD `' . self::AUTO_ID . | ||
'` int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST'); | ||
|
||
if (!$metaDataTable->hasIndex(self::UNIQUE_KEY_NAME)) { | ||
$this->addSql( | ||
'ALTER TABLE `' . $tableName . '` | ||
ADD CONSTRAINT `' . self::UNIQUE_KEY_NAME . '` | ||
UNIQUE (' . self::PK_COLUMNS . ')' | ||
); | ||
} | ||
|
||
if ($recreateForeignKey) { | ||
$this->addSql( | ||
'ALTER TABLE `' . $tableName . '` | ||
ADD CONSTRAINT `'.$foreignKeyName.'` | ||
FOREIGN KEY (`' . self::ID_COLUMN . '`) | ||
REFERENCES `objects` (`' . self::ID_COLUMN . '`) | ||
ON UPDATE NO ACTION | ||
ON DELETE CASCADE;' | ||
); | ||
} | ||
} | ||
} | ||
|
||
$this->addSql('SET foreign_key_checks = 1'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
$this->addSql('SET foreign_key_checks = 0'); | ||
|
||
$metaDataTables = $this->connection->fetchAllAssociative( | ||
"SHOW FULL TABLES | ||
WHERE `Tables_in_{$this->connection->getDatabase()}` | ||
LIKE 'object_metadata_%' AND Table_type = 'BASE TABLE'" | ||
); | ||
|
||
foreach ($metaDataTables as $table) { | ||
$tableName = current($table); | ||
$metaDataTable = $schema->getTable($tableName); | ||
$foreignKeyName = AbstractDao::getForeignKeyName($tableName, self::ID_COLUMN); | ||
|
||
if ($metaDataTable->hasColumn(self::AUTO_ID)) { | ||
if ($recreateForeignKey = $metaDataTable->hasForeignKey($foreignKeyName)) { | ||
$this->addSql('ALTER TABLE `' . $tableName . '` DROP FOREIGN KEY `' . $foreignKeyName . '`'); | ||
} | ||
|
||
$this->addSql('ALTER TABLE `' . $tableName . '` DROP COLUMN `' . self::AUTO_ID . '`'); | ||
$this->addSql( | ||
'ALTER TABLE `' . $tableName . '` ADD PRIMARY KEY (' . self::PK_COLUMNS . ')' | ||
); | ||
|
||
if ($metaDataTable->hasIndex(self::UNIQUE_KEY_NAME)) { | ||
$this->addSql( | ||
'ALTER TABLE `' . $tableName . '` DROP INDEX `' . self::UNIQUE_KEY_NAME . '`' | ||
); | ||
} | ||
|
||
if ($recreateForeignKey) { | ||
$this->addSql( | ||
'ALTER TABLE `' . $tableName . '` | ||
ADD CONSTRAINT `'.$foreignKeyName.'` | ||
FOREIGN KEY (`' . self::ID_COLUMN . '`) | ||
REFERENCES `objects` (`' . self::ID_COLUMN . '`) | ||
ON UPDATE RESTRICT | ||
ON DELETE CASCADE;' | ||
); | ||
} | ||
} | ||
} | ||
|
||
$this->addSql('SET foreign_key_checks = 1'); | ||
} | ||
} |
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