vendor/pimcore/pimcore/bundles/CoreBundle/Migrations/Version20220120162621.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 Version20220120162621 extends AbstractMigration
  19. {
  20.     public function getDescription(): string
  21.     {
  22.         return '';
  23.     }
  24.     public function up(Schema $schema): void
  25.     {
  26.         $schema->hasExplicitForeignKeyIndexes();
  27.         //disable foreign key checks
  28.         $this->addSql('SET foreign_key_checks = 0');
  29.         if (!$schema->getTable('users_workspaces_asset')->hasForeignKey('fk_users_workspaces_asset_assets')) {
  30.             $this->addSql(
  31.                 'ALTER TABLE `users_workspaces_asset`
  32.                 ADD CONSTRAINT `fk_users_workspaces_asset_assets`
  33.                 FOREIGN KEY (`cid`)
  34.                 REFERENCES `assets` (`id`)
  35.                 ON UPDATE NO ACTION
  36.                 ON DELETE CASCADE;'
  37.             );
  38.         }
  39.         $this->addSql(
  40.             'ALTER TABLE `users_workspaces_asset`
  41.             CHANGE `userId` `userId` int(11) unsigned NOT NULL DEFAULT \'0\';'
  42.         );
  43.         if (!$schema->getTable('users_workspaces_asset')->hasForeignKey('fk_users_workspaces_asset_users')) {
  44.             $this->addSql(
  45.                 'ALTER TABLE `users_workspaces_asset`
  46.                 ADD CONSTRAINT `fk_users_workspaces_asset_users`
  47.                 FOREIGN KEY (`userId`)
  48.                 REFERENCES `users` (`id`)
  49.                 ON UPDATE NO ACTION
  50.                 ON DELETE CASCADE;'
  51.             );
  52.         }
  53.         if (!$schema->getTable('users_workspaces_document')->hasForeignKey('fk_users_workspaces_document_documents')) {
  54.             $this->addSql(
  55.                 'ALTER TABLE `users_workspaces_document`
  56.                 ADD CONSTRAINT `fk_users_workspaces_document_documents`
  57.                 FOREIGN KEY (`cid`)
  58.                 REFERENCES `documents` (`id`)
  59.                 ON UPDATE NO ACTION
  60.                 ON DELETE CASCADE;'
  61.             );
  62.         }
  63.         $this->addSql(
  64.             'ALTER TABLE `users_workspaces_document`
  65.             CHANGE `userId` `userId` int(11) unsigned NOT NULL DEFAULT \'0\';');
  66.         if (!$schema->getTable('users_workspaces_document')->hasForeignKey('fk_users_workspaces_document_users')) {
  67.             $this->addSql(
  68.                 'ALTER TABLE `users_workspaces_document`
  69.                 ADD CONSTRAINT `fk_users_workspaces_document_users`
  70.                 FOREIGN KEY (`userId`)
  71.                 REFERENCES `users` (`id`)
  72.                 ON UPDATE NO ACTION
  73.                 ON DELETE CASCADE;'
  74.             );
  75.         }
  76.         if (!$schema->getTable('users_workspaces_object')->hasForeignKey('fk_users_workspaces_object_objects')) {
  77.             $this->addSql(
  78.                 'ALTER TABLE `users_workspaces_object`
  79.                 ADD CONSTRAINT `fk_users_workspaces_object_objects`
  80.                 FOREIGN KEY (`cid`)
  81.                 REFERENCES `objects` (`o_id`)
  82.                 ON UPDATE NO ACTION
  83.                 ON DELETE CASCADE;'
  84.             );
  85.         }
  86.         $this->addSql(
  87.             'ALTER TABLE `users_workspaces_object`
  88.             CHANGE `userId` `userId` int(11) unsigned NOT NULL DEFAULT \'0\';');
  89.         if (!$schema->getTable('users_workspaces_object')->hasForeignKey('fk_users_workspaces_object_users')) {
  90.             $this->addSql(
  91.                 'ALTER TABLE `users_workspaces_object`
  92.                 ADD CONSTRAINT `fk_users_workspaces_object_users`
  93.                 FOREIGN KEY (`userId`)
  94.                 REFERENCES `users` (`id`)
  95.                 ON UPDATE NO ACTION
  96.                 ON DELETE CASCADE;'
  97.             );
  98.         }
  99.         //enable foreign key checks
  100.         $this->addSql('SET foreign_key_checks = 1');
  101.     }
  102.     public function down(Schema $schema): void
  103.     {
  104.         foreach (['asset''document''object'] as $elementType) {
  105.             if ($schema->getTable('users_workspaces_'.$elementType)->hasForeignKey('fk_users_workspaces_'.$elementType.'_'.$elementType.'s')) {
  106.                 $this->addSql('ALTER TABLE `users_workspaces_'.$elementType.'` DROP FOREIGN KEY `fk_users_workspaces_'.$elementType.'_'.$elementType.'s`');
  107.             }
  108.             if ($schema->getTable('users_workspaces_'.$elementType)->hasForeignKey('fk_users_workspaces_'.$elementType.'_users')) {
  109.                 $this->addSql('ALTER TABLE `users_workspaces_'.$elementType.'` DROP FOREIGN KEY `fk_users_workspaces_'.$elementType.'_users`');
  110.             }
  111.             $this->addSql('ALTER TABLE `users_workspaces_'.$elementType.'` CHANGE `userId` `userId` int(11) NOT NULL DEFAULT \'0\'');
  112.         }
  113.     }
  114. }