migrations/Version20260116114142.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use App\Entity\Admin\LocaleInterface;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Doctrine\Migrations\AbstractMigration;
  7. final class Version20260116114142 extends AbstractMigration
  8. {
  9.     public function getDescription(): string
  10.     {
  11.         return 'Create a new translation entity LoyaltyTranslation & move Loyalty\'s translation attributes to it';
  12.     }
  13.     public function up(Schema $schema): void
  14.     {
  15.         $this->addSql('CREATE TABLE loyalty_translation (id INT AUTO_INCREMENT NOT NULL, locale_id INT NOT NULL, loyalty_id INT NOT NULL, signup_message VARCHAR(255) DEFAULT NULL, successful_signup_message VARCHAR(255) DEFAULT NULL, successful_signup_with_notification_message VARCHAR(255) DEFAULT NULL, ask_for_notifications_message VARCHAR(255) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX IDX_8ABAEE7FE559DFD1 (locale_id), INDEX IDX_8ABAEE7FC906750D (loyalty_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  16.         $this->addSql('ALTER TABLE loyalty_translation ADD CONSTRAINT FK_8ABAEE7FE559DFD1 FOREIGN KEY (locale_id) REFERENCES locale (id)');
  17.         $this->addSql('ALTER TABLE loyalty_translation ADD CONSTRAINT FK_8ABAEE7FC906750D FOREIGN KEY (loyalty_id) REFERENCES loyalty (id)');
  18.         $this->addSql('
  19.             INSERT INTO loyalty_translation (signup_message, successful_signup_message, successful_signup_with_notification_message, ask_for_notifications_message, locale_id, loyalty_id, created_at, updated_at)
  20.             SELECT ly.signup_message, ly.successful_signup_message, ly.successful_signup_with_notification_message, ly.ask_for_notifications_message, l.id, ly.id, ly.created_at, ly.updated_at
  21.             FROM loyalty ly
  22.             CROSS JOIN locale l
  23.         ');
  24.         $this->addSql('ALTER TABLE loyalty DROP signup_message, DROP successful_signup_message, DROP successful_signup_with_notification_message, DROP ask_for_notifications_message');
  25.     }
  26.     public function down(Schema $schema): void
  27.     {
  28.         $this->addSql('ALTER TABLE loyalty ADD signup_message VARCHAR(255) DEFAULT NULL, ADD successful_signup_message VARCHAR(255) DEFAULT NULL, ADD successful_signup_with_notification_message VARCHAR(255) DEFAULT NULL, ADD ask_for_notifications_message VARCHAR(255) DEFAULT NULL');
  29.         $this->addSql('
  30.             UPDATE loyalty ly
  31.             INNER JOIN loyalty_translation lt ON lt.loyalty_id = ly.id
  32.             INNER JOIN locale l ON l.id = lt.locale_id
  33.             SET
  34.                 ly.signup_message = lt.signup_message,
  35.                 ly.successful_signup_message = lt.successful_signup_message,
  36.                 ly.successful_signup_with_notification_message = lt.successful_signup_with_notification_message,
  37.                 ly.ask_for_notifications_message = lt.ask_for_notifications_message,
  38.                 ly.updated_at = lt.updated_at
  39.             WHERE l.code = :locale
  40.         ', [
  41.             'locale' => LocaleInterface::LOCALE_FR,
  42.         ]);
  43.         $this->addSql('ALTER TABLE loyalty_translation DROP FOREIGN KEY FK_8ABAEE7FE559DFD1');
  44.         $this->addSql('ALTER TABLE loyalty_translation DROP FOREIGN KEY FK_8ABAEE7FC906750D');
  45.         $this->addSql('DROP TABLE loyalty_translation');
  46.     }
  47. }