Files
alrahma_sunday_school/app/Database/Migrations/2024-11-07-000800_AddYearlyBatchNumberToReimbursementBatches.php
T
root 716cc8b8d3
Tests / PHPUnit (push) Failing after 1m20s
fix school year for all tables
2026-07-18 14:45:38 -04:00

56 lines
1.8 KiB
PHP

<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class AddYearlyBatchNumberToReimbursementBatches extends Migration
{
public function up()
{
if (! $this->db->tableExists('reimbursement_batches') || $this->db->fieldExists('yearly_batch_number', 'reimbursement_batches')) {
return;
}
$fields = [
'yearly_batch_number' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
'default' => 0,
'null' => false,
'after' => 'title',
],
];
$this->forge->addColumn('reimbursement_batches', $fields);
$this->fillExistingBatchNumbers();
}
public function down()
{
if (! $this->db->tableExists('reimbursement_batches') || ! $this->db->fieldExists('yearly_batch_number', 'reimbursement_batches')) {
return;
}
$this->forge->dropColumn('reimbursement_batches', 'yearly_batch_number');
}
private function fillExistingBatchNumbers(): void
{
$this->db->query("SET @prev_school_year := '', @row_number := 0");
$this->db->query(
'UPDATE reimbursement_batches b
JOIN (
SELECT id,
(@row_number := IF(@prev_school_year = COALESCE(school_year, \'\'), @row_number + 1, 1)) AS seq,
@prev_school_year := COALESCE(school_year, \'\') AS school_year_marker
FROM reimbursement_batches
ORDER BY COALESCE(school_year, \'\'), COALESCE(opened_at, \'1970-01-01 00:00:00\'), id
) ranks ON b.id = ranks.id
SET b.yearly_batch_number = ranks.seq'
);
}
}