<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250319094548 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add createdAt column to Code entity and populate it from first token created date';
}
public function up(Schema $schema): void
{
// Add the createdAt column to the code table
$this->addSql('ALTER TABLE code ADD created_at DATETIME DEFAULT NULL');
// Populate the createdAt column with data from the first created token for each code
$this->addSql('
UPDATE code c
LEFT JOIN (
SELECT code_promo_id, MIN(created) as first_created
FROM token
WHERE code_promo_id IS NOT NULL
GROUP BY code_promo_id
) t ON c.id = t.code_promo_id
SET c.created_at = t.first_created
');
// For code entities with no associated tokens, set createdAt to validity date minus 1 year (approximate creation date)
$this->addSql('
UPDATE code
SET created_at = DATE_SUB(validity, INTERVAL 1 YEAR)
WHERE created_at IS NULL
');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE code DROP created_at');
}
}