migrations/Version20250319094548.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. /**
  7.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20250319094548 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Add createdAt column to Code entity and populate it from first token created date';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // Add the createdAt column to the code table
  18.         $this->addSql('ALTER TABLE code ADD created_at DATETIME DEFAULT NULL');
  19.         
  20.         // Populate the createdAt column with data from the first created token for each code
  21.         $this->addSql('
  22.             UPDATE code c
  23.             LEFT JOIN (
  24.                 SELECT code_promo_id, MIN(created) as first_created
  25.                 FROM token
  26.                 WHERE code_promo_id IS NOT NULL
  27.                 GROUP BY code_promo_id
  28.             ) t ON c.id = t.code_promo_id
  29.             SET c.created_at = t.first_created
  30.         ');
  31.         
  32.         // For code entities with no associated tokens, set createdAt to validity date minus 1 year (approximate creation date)
  33.         $this->addSql('
  34.             UPDATE code 
  35.             SET created_at = DATE_SUB(validity, INTERVAL 1 YEAR)
  36.             WHERE created_at IS NULL
  37.         ');
  38.     }
  39.     public function down(Schema $schema): void
  40.     {
  41.         // this down() migration is auto-generated, please modify it to your needs
  42.         $this->addSql('ALTER TABLE code DROP created_at');
  43.     }
  44. }