vendor/bluue/suppliers-bundle/migrations/Version20241015114523.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace SuppliersBundleMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. use Symfony\Component\Uid\UuidV6;
  7. /**
  8.  * Auto-generated Migration: Please modify to your needs!
  9.  */
  10. final class Version20241015114523 extends AbstractMigration
  11. {
  12.     private array $productSuppliers = [];
  13.     public function preUp(Schema $schema): void
  14.     {
  15.         $this->productSuppliers $this->connection->executeQuery('SELECT id, product_id, declination_id, created_by_id, updated_by_id, deleted_by_id, created_at, updated_at, deleted_at, is_default FROM suppliers_bundle__product_supplier')->fetchAllAssociative();
  16.     }
  17.     public function up(Schema $schema): void
  18.     {
  19.         $this->addSql('CREATE TABLE suppliers_bundle__product_supplier_context (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', product_supplier_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', context_id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, is_default TINYINT(1) NOT NULL, INDEX IDX_6143A20C86F2F393 (product_supplier_id), INDEX IDX_6143A20C6B00C1CF (context_id), INDEX IDX_6143A20CB03A8386 (created_by_id), INDEX IDX_6143A20C896DBBDE (updated_by_id), INDEX IDX_6143A20CC76F1F52 (deleted_by_id), INDEX is_default (is_default), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  20.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context ADD CONSTRAINT FK_6143A20C86F2F393 FOREIGN KEY (product_supplier_id) REFERENCES suppliers_bundle__product_supplier (id)');
  21.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context ADD CONSTRAINT FK_6143A20C6B00C1CF FOREIGN KEY (context_id) REFERENCES context (id)');
  22.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context ADD CONSTRAINT FK_6143A20CB03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
  23.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context ADD CONSTRAINT FK_6143A20C896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
  24.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context ADD CONSTRAINT FK_6143A20CC76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
  25.         $this->addSql('DROP INDEX is_default ON suppliers_bundle__product_supplier');
  26.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier DROP is_default');
  27.     }
  28.     public function postUp(Schema $schema): void
  29.     {
  30.         parent::postUp($schema);
  31.         $conn $this->connection;
  32.         foreach ($this->productSuppliers as $ps) {
  33.             $psId '0x' str_replace('-'''UuidV6::fromBinary($ps['id'])->toRfc4122());
  34.             $createdById $ps['created_by_id'] ? '0x' str_replace('-'''UuidV6::fromBinary($ps['created_by_id'])->toRfc4122()) : 'NULL';
  35.             $createdAt $ps['created_at'] ? '"' $ps['created_at'] . '"' 'NULL';
  36.             $updatedById $ps['updated_by_id'] ? '0x' str_replace('-'''UuidV6::fromBinary($ps['updated_by_id'])->toRfc4122()) : 'NULL';
  37.             $updatedAt $ps['updated_at'] ? '"' $ps['updated_at'] . '"' 'NULL';
  38.             $deletedById $ps['deleted_by_id'] ? '0x' str_replace('-'''UuidV6::fromBinary($ps['deleted_by_id'])->toRfc4122()) : 'NULL';
  39.             $deletedAt $ps['deleted_at'] ? '"' $ps['deleted_at'] . '"' 'NULL';
  40.             $isDefault $ps['is_default'] ? '1' '0';
  41.             $decliId $ps['declination_id'] ? '0x' str_replace('-'''UuidV6::fromBinary($ps['declination_id'])->toRfc4122()) : null;
  42.             $productId $ps['product_id'] ? '0x' str_replace('-'''UuidV6::fromBinary($ps['product_id'])->toRfc4122()) : null;
  43.             if ($decliId) {
  44.                 $contexts $conn->executeQuery("SELECT context_id FROM products_bundle__declination_context WHERE declination_id = CAST(" $decliId " AS BINARY) AND deleted_at IS NULL")->fetchAllAssociative();
  45.             } else {
  46.                 $contexts $conn->executeQuery("SELECT context_id FROM products_bundle__product_context WHERE product_id = CAST(" $productId " AS BINARY) AND deleted_at IS NULL")->fetchAllAssociative();
  47.             }
  48.             foreach ($contexts as $context) {
  49.                 $id '0x' str_replace('-''', (new UuidV6())->toRfc4122());
  50.                 $contextId '0x' str_replace('-'''UuidV6::fromBinary($context['context_id'])->toRfc4122());
  51.                 $query 'INSERT INTO suppliers_bundle__product_supplier_context (id, product_supplier_id, context_id, created_by_id, updated_by_id, deleted_by_id, created_at, updated_at, deleted_at, is_default) VALUES (' $id ', ' $psId ', ' $contextId ', ' $createdById ', ' $updatedById ', ' $deletedById ', ' $createdAt ', ' $updatedAt ', ' $deletedAt ', ' $isDefault ')';
  52.                 $conn->executeStatement($query);
  53.             }
  54.         }
  55.     }
  56.     public function down(Schema $schema): void
  57.     {
  58.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context DROP FOREIGN KEY FK_6143A20C86F2F393');
  59.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context DROP FOREIGN KEY FK_6143A20C6B00C1CF');
  60.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context DROP FOREIGN KEY FK_6143A20CB03A8386');
  61.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context DROP FOREIGN KEY FK_6143A20C896DBBDE');
  62.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier_context DROP FOREIGN KEY FK_6143A20CC76F1F52');
  63.         $this->addSql('DROP TABLE suppliers_bundle__product_supplier_context');
  64.         $this->addSql('ALTER TABLE suppliers_bundle__product_supplier ADD is_default TINYINT(1) NOT NULL');
  65.         $this->addSql('CREATE INDEX is_default ON suppliers_bundle__product_supplier (is_default)');
  66.     }
  67. }