<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20241029124704 extends AbstractMigration
{
public function getDescription(): string
{
return 'Set default waiting period dates for existing non closed member applications';
}
public function up(Schema $schema): void
{
// This migration sets waiting period end dates for member applications
// The waiting period is calculated as: first day of next month + 3 months
// Update member_application table
$this->addSql("
UPDATE member_application
SET
-- COALESCE: returns the first non-NULL value (if member_ship_start_at is NULL, use created_at)
-- LAST_DAY: gets the last day of the month for the given date
-- DATE_ADD with INTERVAL 1 DAY: adds 1 day to get first day of next month
-- DATE_ADD with INTERVAL 3 MONTH: adds 3 months to get the waiting period end date
rc_waiting_period_end_date = DATE_ADD(
DATE_ADD(
LAST_DAY(COALESCE(member_ship_start_at, created_at)),
INTERVAL 1 DAY
),
INTERVAL 3 MONTH
),
pp_waiting_period_end_date = DATE_ADD(
DATE_ADD(
LAST_DAY(COALESCE(member_ship_start_at, created_at)),
INTERVAL 1 DAY
),
INTERVAL 3 MONTH
),
do_waiting_period_end_date = DATE_ADD(
DATE_ADD(
LAST_DAY(COALESCE(member_ship_start_at, created_at)),
INTERVAL 1 DAY
),
INTERVAL 3 MONTH
)
-- Only update records with these specific statuses
WHERE processing_status IN ('processing', 'pending', 'incomplete')
");
// Update member_application_co_member table (co-members get same dates as main member)
$this->addSql("
UPDATE member_application_co_member macm
-- JOIN: connects co-member records with their main member application
INNER JOIN member_application ma ON macm.member_application_id = ma.id
SET
-- Use the same date calculation based on the main member's dates
macm.rc_waiting_period_end_date = DATE_ADD(
DATE_ADD(
LAST_DAY(COALESCE(ma.member_ship_start_at, ma.created_at)),
INTERVAL 1 DAY
),
INTERVAL 3 MONTH
),
macm.pp_waiting_period_end_date = DATE_ADD(
DATE_ADD(
LAST_DAY(COALESCE(ma.member_ship_start_at, ma.created_at)),
INTERVAL 1 DAY
),
INTERVAL 3 MONTH
),
macm.do_waiting_period_end_date = DATE_ADD(
DATE_ADD(
LAST_DAY(COALESCE(ma.member_ship_start_at, ma.created_at)),
INTERVAL 1 DAY
),
INTERVAL 3 MONTH
)
-- Only update co-members whose main application has these statuses
WHERE ma.processing_status IN ('processing', 'pending', 'incomplete')
");
}
public function down(Schema $schema): void
{
// Reverse the migration by setting waiting period dates back to NULL
// Reset dates for member_application table
$this->addSql("
UPDATE member_application
SET
rc_waiting_period_end_date = NULL,
pp_waiting_period_end_date = NULL,
do_waiting_period_end_date = NULL
WHERE processing_status IN ('processing', 'pending', 'incomplete')
");
// Reset dates for member_application_co_member table
$this->addSql("
UPDATE member_application_co_member macm
INNER JOIN member_application ma ON macm.member_application_id = ma.id
SET
macm.rc_waiting_period_end_date = NULL,
macm.pp_waiting_period_end_date = NULL,
macm.do_waiting_period_end_date = NULL
WHERE ma.processing_status IN ('processing', 'pending', 'incomplete')
");
}
}