migrations/Version20241029124704.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 Version20241029124704 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Set default waiting period dates for existing non closed member applications';
  11.     }
  12.     public function up(Schema $schema): void
  13.     {
  14.         // This migration sets waiting period end dates for member applications
  15.         // The waiting period is calculated as: first day of next month + 3 months
  16.         // Update member_application table
  17.         $this->addSql("
  18.             UPDATE member_application 
  19.             SET 
  20.                 -- COALESCE: returns the first non-NULL value (if member_ship_start_at is NULL, use created_at)
  21.                 -- LAST_DAY: gets the last day of the month for the given date
  22.                 -- DATE_ADD with INTERVAL 1 DAY: adds 1 day to get first day of next month
  23.                 -- DATE_ADD with INTERVAL 3 MONTH: adds 3 months to get the waiting period end date
  24.                 rc_waiting_period_end_date = DATE_ADD(
  25.                     DATE_ADD(
  26.                         LAST_DAY(COALESCE(member_ship_start_at, created_at)), 
  27.                         INTERVAL 1 DAY
  28.                     ), 
  29.                     INTERVAL 3 MONTH
  30.                 ),
  31.                 pp_waiting_period_end_date = DATE_ADD(
  32.                     DATE_ADD(
  33.                         LAST_DAY(COALESCE(member_ship_start_at, created_at)), 
  34.                         INTERVAL 1 DAY
  35.                     ), 
  36.                     INTERVAL 3 MONTH
  37.                 ),
  38.                 do_waiting_period_end_date = DATE_ADD(
  39.                     DATE_ADD(
  40.                         LAST_DAY(COALESCE(member_ship_start_at, created_at)), 
  41.                         INTERVAL 1 DAY
  42.                     ), 
  43.                     INTERVAL 3 MONTH
  44.                 )
  45.             -- Only update records with these specific statuses
  46.             WHERE processing_status IN ('processing', 'pending', 'incomplete')
  47.         ");
  48.         // Update member_application_co_member table (co-members get same dates as main member)
  49.         $this->addSql("
  50.             UPDATE member_application_co_member macm
  51.             -- JOIN: connects co-member records with their main member application
  52.             INNER JOIN member_application ma ON macm.member_application_id = ma.id
  53.             SET 
  54.                 -- Use the same date calculation based on the main member's dates
  55.                 macm.rc_waiting_period_end_date = DATE_ADD(
  56.                     DATE_ADD(
  57.                         LAST_DAY(COALESCE(ma.member_ship_start_at, ma.created_at)), 
  58.                         INTERVAL 1 DAY
  59.                     ), 
  60.                     INTERVAL 3 MONTH
  61.                 ),
  62.                 macm.pp_waiting_period_end_date = DATE_ADD(
  63.                     DATE_ADD(
  64.                         LAST_DAY(COALESCE(ma.member_ship_start_at, ma.created_at)), 
  65.                         INTERVAL 1 DAY
  66.                     ), 
  67.                     INTERVAL 3 MONTH
  68.                 ),
  69.                 macm.do_waiting_period_end_date = DATE_ADD(
  70.                     DATE_ADD(
  71.                         LAST_DAY(COALESCE(ma.member_ship_start_at, ma.created_at)), 
  72.                         INTERVAL 1 DAY
  73.                     ), 
  74.                     INTERVAL 3 MONTH
  75.                 )
  76.             -- Only update co-members whose main application has these statuses
  77.             WHERE ma.processing_status IN ('processing', 'pending', 'incomplete')
  78.         ");
  79.     }
  80.     public function down(Schema $schema): void
  81.     {
  82.         // Reverse the migration by setting waiting period dates back to NULL
  83.         // Reset dates for member_application table
  84.         $this->addSql("
  85.             UPDATE member_application 
  86.             SET 
  87.                 rc_waiting_period_end_date = NULL,
  88.                 pp_waiting_period_end_date = NULL,
  89.                 do_waiting_period_end_date = NULL
  90.             WHERE processing_status IN ('processing', 'pending', 'incomplete')
  91.         ");
  92.         // Reset dates for member_application_co_member table
  93.         $this->addSql("
  94.             UPDATE member_application_co_member macm
  95.             INNER JOIN member_application ma ON macm.member_application_id = ma.id
  96.             SET 
  97.                 macm.rc_waiting_period_end_date = NULL,
  98.                 macm.pp_waiting_period_end_date = NULL,
  99.                 macm.do_waiting_period_end_date = NULL
  100.             WHERE ma.processing_status IN ('processing', 'pending', 'incomplete')
  101.         ");
  102.     }
  103. }