Skip to content

Commit

Permalink
[Version20230616085142] Recreate Foreign key checks for object_metad…
Browse files Browse the repository at this point in the history
…ata_ table (#15771)

* [Version20230616085142] Recreate Foreign key checks for object_metadata_ table - fixes problem with deleting objects follow up to #15746

* Update bundles/CoreBundle/Migrations/Version20230616085142.php

Co-authored-by: robertSt7 <104770750+robertSt7@users.noreply.github.com>

* Use AbstractDao instead of Dao

* Update bundles/CoreBundle/Migrations/Version20230616085142.php

Co-authored-by: Divesh Pahuja <divesh.pahuja@pimcore.com>

* Update bundles/CoreBundle/Migrations/Version20230616085142.php

Co-authored-by: Divesh Pahuja <divesh.pahuja@pimcore.com>

---------

Co-authored-by: robertSt7 <104770750+robertSt7@users.noreply.github.com>
Co-authored-by: robertSt7 <robert.steinkellner@pimcore.com>
  • Loading branch information
3 people committed Aug 18, 2023
1 parent ea91c83 commit 3d1df7e
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions bundles/CoreBundle/Migrations/Version20230616085142.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Pimcore\Model\Dao\AbstractDao;

final class Version20230616085142 extends AbstractMigration
{
Expand Down Expand Up @@ -49,8 +50,13 @@ public function up(Schema $schema): void
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 IF EXISTS `'.$foreignKeyName.'`');
}

if ($metaDataTable->hasPrimaryKey()) {
$this->addSql('ALTER TABLE `' . $tableName . '` DROP PRIMARY KEY');
}
Expand All @@ -60,8 +66,20 @@ public function up(Schema $schema): void

if (!$metaDataTable->hasIndex(self::UNIQUE_KEY_NAME)) {
$this->addSql(
'ALTER TABLE `' . $tableName . '` ADD ' .
'CONSTRAINT `' . self::UNIQUE_KEY_NAME . '` UNIQUE (' . self::PK_COLUMNS . ')'
'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;'
);
}
}
Expand All @@ -83,15 +101,31 @@ public function down(Schema $schema): void
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 IF EXISTS `'.$foreignKeyName.'`');
}

$this->addSql('ALTER TABLE `' . $tableName . '` DROP COLUMN `' . self::AUTO_ID . '`');
$this->addSql(
'ALTER TABLE `' . $tableName . '` ADD PRIMARY KEY (' . self::PK_COLUMNS . ')'
);
$this->addSql(
'ALTER TABLE `' . $tableName . '` DROP INDEX IF EXISTS `' . 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;'
);
}
}
}

Expand Down

0 comments on commit 3d1df7e

Please sign in to comment.