vendor/bluue/recurring-invoices-bundle/migrations/Version20240710092649.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace RecurringInvoicesBundleMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. use Symfony\Component\Uid\UuidV6;
  7. final class Version20240710092649 extends AbstractMigration
  8. {
  9.     private array $lines;
  10.     public function preUp(Schema $schema): void
  11.     {
  12.         parent::preUp($schema);
  13.         $this->lines $this->connection->executeQuery(
  14.             'SELECT * FROM recurring_invoices_bundle__recurring_invoice_line'
  15.         )->fetchAllAssociative();
  16.     }
  17.     public function up(Schema $schema): void
  18.     {
  19.         $this->addSql('ALTER TABLE recurring_invoices_bundle__recurring_invoice_line ADD product_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', ADD declination_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\'');
  20.         $this->addSql('ALTER TABLE recurring_invoices_bundle__recurring_invoice_line ADD CONSTRAINT FK_BD96F0684584665A FOREIGN KEY (product_id) REFERENCES products_bundle__product (id)');
  21.         $this->addSql('ALTER TABLE recurring_invoices_bundle__recurring_invoice_line ADD CONSTRAINT FK_BD96F0689941A932 FOREIGN KEY (declination_id) REFERENCES products_bundle__declination (id)');
  22.         $this->addSql('CREATE INDEX IDX_BD96F0684584665A ON recurring_invoices_bundle__recurring_invoice_line (product_id)');
  23.         $this->addSql('CREATE INDEX IDX_BD96F0689941A932 ON recurring_invoices_bundle__recurring_invoice_line (declination_id)');
  24.     }
  25.     public function postUp(Schema $schema): void
  26.     {
  27.         parent::postUp($schema);
  28.         foreach ($this->lines as $line) {
  29.             $lineId UuidV6::fromBinary($line['id'])->toRfc4122();
  30.             $options json_decode($line['options'], true);
  31.             if (!empty($options['product'])) {
  32.                 $productOptions $options['product'];
  33.                 $decliId = !empty($productOptions['declination_id']) ?
  34.                     '0x' str_replace('-'''$productOptions['declination_id']) : null
  35.                 ;
  36.                 $productId '0x' str_replace('-'''$productOptions['id']);
  37.                 $query "UPDATE recurring_invoices_bundle__recurring_invoice_line SET product_id = " $productId;
  38.                 if ($decliId) {
  39.                     $query .= ", declination_id = " $decliId;
  40.                 }
  41.                 $query .= " WHERE id = UNHEX(CONCAT('', REPLACE('" $lineId "', '-', '')))";
  42.                 $this->connection->executeStatement($query);
  43.             }
  44.         }
  45.     }
  46.     public function down(Schema $schema): void
  47.     {
  48.         $this->addSql('ALTER TABLE recurring_invoices_bundle__recurring_invoice_line DROP FOREIGN KEY FK_BD96F0684584665A');
  49.         $this->addSql('ALTER TABLE recurring_invoices_bundle__recurring_invoice_line DROP FOREIGN KEY FK_BD96F0689941A932');
  50.         $this->addSql('DROP INDEX IDX_BD96F0684584665A ON recurring_invoices_bundle__recurring_invoice_line');
  51.         $this->addSql('DROP INDEX IDX_BD96F0689941A932 ON recurring_invoices_bundle__recurring_invoice_line');
  52.         $this->addSql('ALTER TABLE recurring_invoices_bundle__recurring_invoice_line DROP product_id, DROP declination_id');
  53.     }
  54. }