vendor/pimcore/pimcore/bundles/CoreBundle/Migrations/Version20211018104331.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\DBAL\Schema\SchemaException;
  18. use Doctrine\DBAL\Schema\Table;
  19. use Doctrine\DBAL\Types\IntegerType;
  20. use Doctrine\Migrations\AbstractMigration;
  21. use Pimcore\Model\Dao\AbstractDao;
  22. use Pimcore\Model\DataObject;
  23. use Pimcore\Tool;
  24. final class Version20211018104331 extends AbstractMigration
  25. {
  26.     public function getDescription(): string
  27.     {
  28.         return '';
  29.     }
  30.     public function preUp(Schema $schema): void
  31.     {
  32.         //disable foreign key checks
  33.         $this->connection->executeQuery('SET foreign_key_checks = 0');
  34.     }
  35.     public function postUp(Schema $schema): void
  36.     {
  37.         //enable foreign key checks
  38.         $this->connection->executeQuery('SET foreign_key_checks = 1');
  39.     }
  40.     public function up(Schema $schema): void
  41.     {
  42.         $list = new DataObject\ClassDefinition\Listing();
  43.         foreach ($list as $class) {
  44.             $foreignKeys $this->getForeignKeys($class);
  45.             foreach ($foreignKeys as $table => $objectIdColumn) {
  46.                 try {
  47.                     $tableSchema $schema->getTable($table);
  48.                 } catch (SchemaException $e) {
  49.                     continue;
  50.                 }
  51.                 $this->createForeignKey($tableSchema$objectIdColumn);
  52.             }
  53.         }
  54.         $this->createForeignKey($schema->getTable('object_url_slugs'), 'objectId');
  55.     }
  56.     public function down(Schema $schema): void
  57.     {
  58.         $list = new DataObject\ClassDefinition\Listing();
  59.         foreach ($list as $class) {
  60.             $foreignKeys $this->getForeignKeys($class);
  61.             foreach ($foreignKeys as $table => $objectIdColumn) {
  62.                 try {
  63.                     $tableSchema $schema->getTable($table);
  64.                 } catch (SchemaException $e) {
  65.                     continue;
  66.                 }
  67.                 $fkName AbstractDao::getForeignKeyName($table$objectIdColumn);
  68.                 if ($tableSchema->hasForeignKey($fkName)) {
  69.                     $tableSchema->removeForeignKey($fkName);
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     /**
  75.      * @param DataObject\ClassDefinition $class
  76.      *
  77.      * @return string[]
  78.      */
  79.     private function getForeignKeys(DataObject\ClassDefinition $class): array
  80.     {
  81.         $foreignKeys = [
  82.             'object_query_'.$class->getId() => 'oo_id',
  83.             'object_store_'.$class->getId() => 'oo_id',
  84.             'object_relations_'.$class->getId() => 'src_id',
  85.             'object_classificationstore_groups_'.$class->getId() => 'o_id',
  86.             'object_classificationstore_data_'.$class->getId() => 'o_id',
  87.             'object_metadata_'.$class->getId() => 'o_id',
  88.             'object_localized_data_'.$class->getId() => 'ooo_id',
  89.         ];
  90.         foreach (Tool::getValidLanguages() as $language) {
  91.             $foreignKeys['object_localized_query_'.$class->getId().'_'.$language] = 'ooo_id';
  92.         }
  93.         $brickList = new DataObject\Objectbrick\Definition\Listing();
  94.         foreach ($brickList->load() as $brickDefinition) {
  95.             $foreignKeys['object_brick_query_'.$brickDefinition->getKey().'_'.$class->getId()] = 'o_id';
  96.             $foreignKeys['object_brick_localized_'.$brickDefinition->getKey().'_'.$class->getId()] = 'ooo_id';
  97.             $foreignKeys['object_brick_store_'.$brickDefinition->getKey().'_'.$class->getId()] = 'o_id';
  98.         }
  99.         $fieldCollectionList = new DataObject\Fieldcollection\Definition\Listing();
  100.         foreach ($fieldCollectionList->load() as $fieldCollectionDefinition) {
  101.             $foreignKeys['object_collection_'.$fieldCollectionDefinition->getKey().'_'.$class->getId()] = 'o_id';
  102.             $foreignKeys['object_collection_'.$fieldCollectionDefinition->getKey().'_localized_'.$class->getId()] = 'ooo_id';
  103.         }
  104.         return $foreignKeys;
  105.     }
  106.     private function createForeignKey(Table $tableSchemastring $localForeignKeyColumn)
  107.     {
  108.         $fkName AbstractDao::getForeignKeyName($tableSchema->getName(), $localForeignKeyColumn);
  109.         if (!$tableSchema->hasForeignKey($fkName)) {
  110.             $column $tableSchema->getColumn($localForeignKeyColumn);
  111.             if ($column->getPrecision() !== 10 || !($column->getType() instanceof IntegerType)) {
  112.                 $tableSchema->changeColumn($localForeignKeyColumn, [
  113.                     'type' => new IntegerType(),
  114.                     'precision' => 10,
  115.                     'unsigned' => true,
  116.                 ]);
  117.             }
  118.             if (!$column->hasCustomSchemaOption('unsigned') || $column->getCustomSchemaOption('unsigned') === false) {
  119.                 $tableSchema->changeColumn($localForeignKeyColumn, ['unsigned' => true]);
  120.             }
  121.             $tableSchema->addForeignKeyConstraint('objects', [$localForeignKeyColumn], ['o_id'], ['onDelete' => 'CASCADE'], $fkName);
  122.         }
  123.     }
  124. }