<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use App\Entity\Admin\LocaleInterface;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20260116114142 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create a new translation entity LoyaltyTranslation & move Loyalty\'s translation attributes to it';
}
public function up(Schema $schema): void
{
$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');
$this->addSql('ALTER TABLE loyalty_translation ADD CONSTRAINT FK_8ABAEE7FE559DFD1 FOREIGN KEY (locale_id) REFERENCES locale (id)');
$this->addSql('ALTER TABLE loyalty_translation ADD CONSTRAINT FK_8ABAEE7FC906750D FOREIGN KEY (loyalty_id) REFERENCES loyalty (id)');
$this->addSql('
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)
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
FROM loyalty ly
CROSS JOIN locale l
');
$this->addSql('ALTER TABLE loyalty DROP signup_message, DROP successful_signup_message, DROP successful_signup_with_notification_message, DROP ask_for_notifications_message');
}
public function down(Schema $schema): void
{
$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');
$this->addSql('
UPDATE loyalty ly
INNER JOIN loyalty_translation lt ON lt.loyalty_id = ly.id
INNER JOIN locale l ON l.id = lt.locale_id
SET
ly.signup_message = lt.signup_message,
ly.successful_signup_message = lt.successful_signup_message,
ly.successful_signup_with_notification_message = lt.successful_signup_with_notification_message,
ly.ask_for_notifications_message = lt.ask_for_notifications_message,
ly.updated_at = lt.updated_at
WHERE l.code = :locale
', [
'locale' => LocaleInterface::LOCALE_FR,
]);
$this->addSql('ALTER TABLE loyalty_translation DROP FOREIGN KEY FK_8ABAEE7FE559DFD1');
$this->addSql('ALTER TABLE loyalty_translation DROP FOREIGN KEY FK_8ABAEE7FC906750D');
$this->addSql('DROP TABLE loyalty_translation');
}
}