vendor/pimcore/pimcore/bundles/CoreBundle/Migrations/Version20230616085142.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. use Pimcore\Model\Dao\AbstractDao;
  19. final class Version20230616085142 extends AbstractMigration
  20. {
  21.     private const ID_COLUMN 'o_id';
  22.     private const PK_COLUMNS '`' self::ID_COLUMN .
  23.     '`,`dest_id`, `type`, `fieldname`, `column`, `ownertype`, `ownername`, `position`, `index`';
  24.     private const UNIQUE_KEY_NAME 'metadata_un';
  25.     private const AUTO_ID 'auto_id';
  26.     public function getDescription(): string
  27.     {
  28.         return 'Migrate object_metadata schema to have a auto increment column';
  29.     }
  30.     public function up(Schema $schema): void
  31.     {
  32.         $this->addSql('SET foreign_key_checks = 0');
  33.         $metaDataTables $this->connection->fetchAllAssociative(
  34.             "SHOW FULL TABLES
  35.                    WHERE `Tables_in_{$this->connection->getDatabase()}`
  36.                     LIKE 'object_metadata_%' AND Table_type = 'BASE TABLE'"
  37.         );
  38.         foreach ($metaDataTables as $table) {
  39.             $tableName current($table);
  40.             $metaDataTable $schema->getTable($tableName);
  41.             $foreignKeyName AbstractDao::getForeignKeyName($tableNameself::ID_COLUMN);
  42.             if (!$metaDataTable->hasColumn(self::AUTO_ID)) {
  43.                 if ($recreateForeignKey $metaDataTable->hasForeignKey($foreignKeyName)) {
  44.                     $this->addSql('ALTER TABLE `' $tableName '` DROP FOREIGN KEY `' $foreignKeyName '`');
  45.                 }
  46.                 if ($metaDataTable->hasPrimaryKey()) {
  47.                     $this->addSql('ALTER TABLE `' $tableName '` DROP PRIMARY KEY');
  48.                 }
  49.                 $this->addSql('ALTER TABLE ' $tableName ' ADD `' self::AUTO_ID .
  50.                     '` int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST');
  51.                 if (!$metaDataTable->hasIndex(self::UNIQUE_KEY_NAME)) {
  52.                     $this->addSql(
  53.                         'ALTER TABLE `' $tableName '`
  54.                             ADD CONSTRAINT `' self::UNIQUE_KEY_NAME '`
  55.                             UNIQUE (' self::PK_COLUMNS ')'
  56.                     );
  57.                 }
  58.                 if ($recreateForeignKey) {
  59.                     $this->addSql(
  60.                         'ALTER TABLE `' $tableName '`
  61.                             ADD CONSTRAINT `'.$foreignKeyName.'`
  62.                             FOREIGN KEY (`' self::ID_COLUMN '`)
  63.                             REFERENCES `objects` (`' self::ID_COLUMN '`)
  64.                             ON UPDATE NO ACTION
  65.                             ON DELETE CASCADE;'
  66.                     );
  67.                 }
  68.             }
  69.         }
  70.         $this->addSql('SET foreign_key_checks = 1');
  71.     }
  72.     public function down(Schema $schema): void
  73.     {
  74.         $this->addSql('SET foreign_key_checks = 0');
  75.         $metaDataTables $this->connection->fetchAllAssociative(
  76.             "SHOW FULL TABLES
  77.                    WHERE `Tables_in_{$this->connection->getDatabase()}`
  78.                     LIKE 'object_metadata_%' AND Table_type = 'BASE TABLE'"
  79.         );
  80.         foreach ($metaDataTables as $table) {
  81.             $tableName current($table);
  82.             $metaDataTable $schema->getTable($tableName);
  83.             $foreignKeyName AbstractDao::getForeignKeyName($tableNameself::ID_COLUMN);
  84.             if ($metaDataTable->hasColumn(self::AUTO_ID)) {
  85.                 if ($recreateForeignKey $metaDataTable->hasForeignKey($foreignKeyName)) {
  86.                     $this->addSql('ALTER TABLE `' $tableName '` DROP FOREIGN KEY `' $foreignKeyName '`');
  87.                 }
  88.                 $this->addSql('ALTER TABLE `' $tableName '` DROP COLUMN `' self::AUTO_ID '`');
  89.                 $this->addSql(
  90.                     'ALTER TABLE `' $tableName '` ADD PRIMARY KEY (' self::PK_COLUMNS  ')'
  91.                 );
  92.                 if ($metaDataTable->hasIndex(self::UNIQUE_KEY_NAME)) {
  93.                     $this->addSql(
  94.                         'ALTER TABLE `' $tableName '` DROP INDEX `' self::UNIQUE_KEY_NAME '`'
  95.                     );
  96.                 }
  97.                 if ($recreateForeignKey) {
  98.                     $this->addSql(
  99.                         'ALTER TABLE `' $tableName '`
  100.                             ADD CONSTRAINT `'.$foreignKeyName.'`
  101.                             FOREIGN KEY (`' self::ID_COLUMN '`)
  102.                             REFERENCES `objects` (`' self::ID_COLUMN '`)
  103.                             ON UPDATE RESTRICT
  104.                             ON DELETE CASCADE;'
  105.                     );
  106.                 }
  107.             }
  108.         }
  109.         $this->addSql('SET foreign_key_checks = 1');
  110.     }
  111. }