migrations/Version20241120134000.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. final class Version20241120134000 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Set effective date for existing non closed member applications';
  11.     }
  12.     public function up(Schema $schema): void
  13.     {
  14.         // This migration sets the effective date for member applications
  15.         // The effective date is calculated as: first day of next month based on membership start date or created date
  16.         $this->addSql("
  17.             UPDATE member_application 
  18.             SET 
  19.                 -- Calculate effective date as first day of next month
  20.                 -- COALESCE: takes membership start date if available, otherwise uses created date
  21.                 -- DATE_FORMAT with '%Y-%m-01': formats date to first day of the month
  22.                 -- DATE_ADD with INTERVAL 1 MONTH: adds one month to get first day of next month
  23.                 effective_date = DATE_ADD(
  24.                     DATE_FORMAT(
  25.                         COALESCE(member_ship_start_at, created_at), 
  26.                         '%Y-%m-01'
  27.                     ), 
  28.                     INTERVAL 1 MONTH
  29.                 )
  30.             -- Only update records with these specific processing statuses
  31.             WHERE processing_status IN ('processing', 'pending', 'incomplete')
  32.               -- Only update if effective_date hasn't been set already
  33.               AND effective_date IS NULL
  34.         ");
  35.     }
  36.     public function down(Schema $schema): void
  37.     {
  38.         // Reset effective date to NULL for the affected records
  39.         $this->addSql("
  40.             UPDATE member_application 
  41.             SET effective_date = NULL
  42.             WHERE processing_status IN ('processing', 'pending', 'incomplete')
  43.         ");
  44.     }
  45. }