vendor/bluue/products-bundle/migrations/Version20240229124327.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ProductsBundleMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. use Symfony\Component\Uid\UuidV6;
  7. final class Version20240229124327 extends AbstractMigration
  8. {
  9.     private $stockMovements = [];
  10.     public function preUp(Schema $schema): void
  11.     {
  12.         $this->stockMovements $this->connection->executeQuery('SELECT * FROM products_bundle__brand')->fetchAllAssociative();
  13.     }
  14.     public function up(Schema $schema): void
  15.     {
  16.         $this->addSql('SET FOREIGN_KEY_CHECKS=0');
  17.         $this->addSql('DROP INDEX name ON products_bundle__brand');
  18.         $this->addSql('ALTER TABLE products_bundle__brand DROP name, DROP description, DROP summary');
  19.         $this->addSql(
  20.             'ALTER TABLE products_bundle__brand_context ADD name VARCHAR(64) NOT NULL,
  21.             ADD description LONGTEXT DEFAULT NULL, ADD summary LONGTEXT DEFAULT NULL'
  22.         );
  23.         $this->addSql('CREATE INDEX name ON products_bundle__brand_context (name)');
  24.         $this->addSql('ALTER TABLE products_bundle__brand_translations DROP FOREIGN KEY FK_2941B107232D562B');
  25.         $this->addSql(
  26.             'ALTER TABLE products_bundle__brand_translations ADD CONSTRAINT FK_2941B107232D562B 
  27.             FOREIGN KEY (object_id) REFERENCES products_bundle__brand_context (id) ON DELETE CASCADE'
  28.         );
  29.         $this->addSql('SET FOREIGN_KEY_CHECKS=1');
  30.     }
  31.     public function postUp(Schema $schema): void
  32.     {
  33.         parent::postUp($schema);
  34.         $conn $this->connection;
  35.         foreach ($this->stockMovements as $sm) {
  36.             $smId UuidV6::fromBinary($sm['id'])->toRfc4122();
  37.             $conn->executeStatement(
  38.                 "UPDATE `products_bundle__brand_context` SET `name` = '" addslashes((string) $sm['name']) . "', `description` = '" .
  39.                 addslashes((string) $sm['description']) . "', `summary` = '" addslashes((string) $sm['summary']) .
  40.                 "' WHERE brand_id = UNHEX(CONCAT('', REPLACE('" $smId "', '-', '')))"
  41.             );
  42.             $brandTranslations $conn->executeQuery(
  43.                 "SELECT id, object_id, locale, field FROM products_bundle__brand_translations
  44.                 WHERE object_id = UNHEX(CONCAT('', REPLACE('" $smId "', '-', '')))"
  45.             )->fetchAllAssociative();
  46.             $brandContexts $conn->executeQuery(
  47.                 "SELECT id, `name`, `summary`, `description` FROM products_bundle__brand_context
  48.                 WHERE brand_id = UNHEX(CONCAT('', REPLACE('" $smId "', '-', '')))"
  49.             )->fetchAllAssociative();
  50.             $first true;
  51.             foreach ($brandContexts as $bc) {
  52.                 $bc_uuid UuidV6::fromBinary($bc['id'])->toRfc4122();
  53.                 $bc_uuid '0x' str_replace('-'''$bc_uuid);
  54.                 $summary_fr false;
  55.                 $description_fr false;
  56.                 if ($first) {
  57.                     foreach ($brandTranslations as $bt) {
  58.                         if ($bt['locale'] == 'fr') {
  59.                             if ($bt['field'] == 'summary') {
  60.                                 $summary_fr true;
  61.                             } elseif ($bt['field'] == 'description') {
  62.                                 $description_fr true;
  63.                             }
  64.                         }
  65.                         $bt_id $bt['id'];
  66.                         $conn->executeStatement(
  67.                             "UPDATE products_bundle__brand_translations SET object_id = " .
  68.                             $bc_uuid " WHERE id = " $bt_id
  69.                         );
  70.                     }
  71.                     $first false;
  72.                 }
  73.                 if (!$summary_fr) {
  74.                     $value $bc['summary'];
  75.                     if (!$value) {
  76.                         $value 'NULL';
  77.                     } else {
  78.                         $value addslashes($value);
  79.                         $value "'" $value "'";
  80.                     }
  81.                     $conn->executeStatement(
  82.                         "INSERT INTO products_bundle__brand_translations (id, object_id, locale, field, content)
  83.                         VALUES (NULL, " $bc_uuid ", 'fr', 'summary', " $value ")"
  84.                     );
  85.                 }
  86.                 if (!$description_fr) {
  87.                     $value $bc['description'];
  88.                     if (!$value) {
  89.                         $value 'NULL';
  90.                     } else {
  91.                         $value addslashes($value);
  92.                         $value "'" $value "'";
  93.                     }
  94.                     $conn->executeStatement(
  95.                         "INSERT INTO products_bundle__brand_translations (id, object_id, locale, field, content)
  96.                         VALUES (NULL, " $bc_uuid ", 'fr', 'description', " $value ")"
  97.                     );
  98.                 }
  99.             }
  100.         }
  101.         echo "fin brands \n";
  102.     }
  103.     public function down(Schema $schema): void
  104.     {
  105.         $this->addSql('SET FOREIGN_KEY_CHECKS=0');
  106.         $this->addSql(
  107.             'ALTER TABLE products_bundle__brand ADD name VARCHAR(64) NOT NULL,
  108.             ADD description LONGTEXT DEFAULT NULL, ADD summary LONGTEXT DEFAULT NULL'
  109.         );
  110.         $this->addSql('CREATE INDEX name ON products_bundle__brand (name)');
  111.         $this->addSql('DROP INDEX name ON products_bundle__brand_context');
  112.         $this->addSql('ALTER TABLE products_bundle__brand_context DROP name, DROP description, DROP summary');
  113.         $this->addSql('ALTER TABLE products_bundle__brand_translations DROP FOREIGN KEY FK_2941B107232D562B');
  114.         $this->addSql(
  115.             'ALTER TABLE products_bundle__brand_translations ADD CONSTRAINT FK_2941B107232D562B
  116.             FOREIGN KEY (object_id) REFERENCES products_bundle__brand (id) ON DELETE CASCADE'
  117.         );
  118.         $this->addSql('SET FOREIGN_KEY_CHECKS=1');
  119.     }
  120. }