vendor/pimcore/pimcore/bundles/CoreBundle/Migrations/Version20221222181745.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Bundle\CoreBundle\Migrations;
  16. use Doctrine\DBAL\Schema\Schema;
  17. use Doctrine\Migrations\AbstractMigration;
  18. final class Version20221222181745 extends AbstractMigration
  19. {
  20.     public function getDescription(): string
  21.     {
  22.         return 'Add CONSTRAINT of classificationstore_groups in object_classificationstore_groups';
  23.     }
  24.     public function up(Schema $schema): void
  25.     {
  26.         $tableList $this->connection->fetchAllAssociative("show tables like 'object_classificationstore_groups_%'");
  27.         foreach ($tableList as $tableGroups) {
  28.             $theTableGroups current($tableGroups);
  29.             $tableArray explode('_'$theTableGroups);
  30.             $tableNumber end($tableArray);
  31.             if (!is_numeric($tableNumber)) {
  32.                 continue;
  33.             }
  34.             $this->addSql("DELETE `object_classificationstore_groups_{$tableNumber}`
  35.             FROM `object_classificationstore_groups_{$tableNumber}`
  36.             LEFT JOIN `classificationstore_groups` ON object_classificationstore_groups_{$tableNumber}.groupId = classificationstore_groups.id
  37.             WHERE classificationstore_groups.id IS NULL;");
  38.             $this->addSql("DELETE `object_classificationstore_data_{$tableNumber}`
  39.             FROM `object_classificationstore_data_{$tableNumber}`
  40.             LEFT JOIN `$theTableGroups` ON object_classificationstore_data_{$tableNumber}.o_id = $theTableGroups.o_id AND
  41.             object_classificationstore_data_{$tableNumber}.fieldname = $theTableGroups.fieldname AND
  42.             object_classificationstore_data_{$tableNumber}.groupId = $theTableGroups.groupId
  43.             WHERE $theTableGroups.o_id IS NULL AND  $theTableGroups.fieldname IS NULL AND $theTableGroups.groupId IS NULL;");
  44.             $this->addSql("ALTER TABLE `$theTableGroups` MODIFY COLUMN groupId INT(11) UNSIGNED NOT NULL;");
  45.             $this->addSql("ALTER TABLE `$theTableGroups`
  46.             ADD CONSTRAINT `fk_object_classificationstore_groups_{$tableNumber}__groupId` FOREIGN KEY (`groupId`)
  47.             REFERENCES `classificationstore_groups` (`id`)
  48.             ON DELETE CASCADE;");
  49.             $theTableData "object_classificationstore_data_$tableNumber";
  50.             $this->addSql("ALTER TABLE `$theTableData` MODIFY COLUMN groupId INT(11) UNSIGNED NOT NULL;");
  51.             $this->addSql("CREATE INDEX `groupKeys` ON `$theTableData` (`o_id`, `fieldname`, `groupId`);");
  52.             $this->addSql("ALTER TABLE `$theTableData`
  53.             ADD CONSTRAINT `fk_object_classificationstore_data_{$tableNumber}__o_id__fieldname__groupId` FOREIGN KEY (`o_id`, `fieldname`, `groupId`)
  54.             REFERENCES `$theTableGroups` (`o_id`, `fieldname`, `groupId`)
  55.             ON DELETE CASCADE;");
  56.         }
  57.     }
  58.     public function down(Schema $schema): void
  59.     {
  60.         $tableList $this->connection->fetchAllAssociative("show tables like 'object_classificationstore_groups_%'");
  61.         foreach ($tableList as $theTableGroups) {
  62.             $theTableGroups current($theTableGroups);
  63.             $tableArray explode('_'$theTableGroups);
  64.             $tableNumber end($tableArray);
  65.             if (!is_numeric($tableNumber)) {
  66.                 continue;
  67.             }
  68.             $this->addSql("ALTER TABLE `$theTableGroups` DROP FOREIGN KEY `fk_object_classificationstore_groups_{$tableNumber}__groupId`;");
  69.             $theTableData "object_classificationstore_data_$tableNumber";
  70.             $this->addSql("ALTER TABLE `$theTableData` DROP FOREIGN KEY `fk_object_classificationstore_data_{$tableNumber}__o_id__fieldname__groupId`;");
  71.             $this->addSql("ALTER TABLE `$theTableData` DROP INDEX `groupKeys`;");
  72.             $this->addSql("ALTER TABLE `$theTableData` MODIFY COLUMN groupId BIGINT(20) NOT NULL;");
  73.             $this->addSql("ALTER TABLE `$theTableGroups` MODIFY COLUMN groupId BIGINT(20) NOT NULL;");
  74.         }
  75.     }
  76. }