Files
alrahma_sunday_school/app/Database/Migrations/2026-08-14-000200_EnrollmentCarryOverAndExceptionUniqueness.php
2026-08-15 15:07:16 -04:00

51 lines
1.8 KiB
PHP

<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class EnrollmentCarryOverAndExceptionUniqueness extends Migration
{
public function up()
{
if ($this->db->tableExists('school_years') && $this->db->fieldExists('carry_over_balance_behavior', 'school_years')) {
$this->db->table('school_years')
->groupStart()
->where('carry_over_balance_behavior', 'information_only')
->orWhere('carry_over_balance_behavior', null)
->orWhere('carry_over_balance_behavior', '')
->groupEnd()
->update(['carry_over_balance_behavior' => 'submission_blocked_until_payment']);
}
if (! $this->db->tableExists('enrollment_exceptions')) {
return;
}
$indexes = $this->db->query('SHOW INDEX FROM enrollment_exceptions')->getResultArray();
$names = array_map(static fn (array $row): string => (string) ($row['Key_name'] ?? ''), $indexes);
if (in_array('uniq_enrollment_exceptions_scope_status', $names, true)) {
return;
}
try {
$this->db->query(
'ALTER TABLE enrollment_exceptions ADD UNIQUE INDEX uniq_enrollment_exceptions_scope_status (parent_id, student_id, school_year, status)'
);
} catch (\Throwable $e) {
log_message('error', 'Unable to add unique enrollment exception index: ' . $e->getMessage());
}
}
public function down()
{
if ($this->db->tableExists('enrollment_exceptions')) {
try {
$this->db->query('ALTER TABLE enrollment_exceptions DROP INDEX uniq_enrollment_exceptions_scope_status');
} catch (\Throwable $e) {
// Index may not exist.
}
}
}
}