Files
alrahma_sunday_school/app/Database/Migrations/2026-08-27-000100_RemoveSemesterFromInvoiceStudentsList.php
root 38644b32ae
Deploy to Shared Hosting / Shared hosting deploy (push) Failing after 48s
Tests / PHPUnit (push) Failing after 1m19s
fix invoice and enrollment fees
2026-08-27 18:34:36 -04:00

91 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
final class RemoveSemesterFromInvoiceStudentsList extends Migration
{
public function up(): void
{
if (! $this->db->tableExists('invoice_students_list')
|| ! $this->db->fieldExists('semester', 'invoice_students_list')) {
return;
}
foreach ($this->indexesContainingColumn('invoice_students_list', 'semester') as $indexName) {
$this->dropIndexIfExists('invoice_students_list', $indexName);
}
$this->forge->dropColumn('invoice_students_list', 'semester');
$this->db->resetDataCache();
}
public function down(): void
{
if (! $this->db->tableExists('invoice_students_list')
|| $this->db->fieldExists('semester', 'invoice_students_list')) {
return;
}
$this->forge->addColumn('invoice_students_list', [
'semester' => [
'type' => 'VARCHAR',
'constraint' => 10,
'null' => true,
'after' => 'school_year',
],
]);
$this->db->resetDataCache();
}
/**
* @return list<string>
*/
private function indexesContainingColumn(string $table, string $column): array
{
$rows = $this->db->query(
'SELECT DISTINCT INDEX_NAME
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = ?
AND COLUMN_NAME = ?
AND INDEX_NAME <> \'PRIMARY\'',
[$table, $column]
)->getResultArray();
return array_values(array_filter(array_map(
static fn (array $row): string => (string) ($row['INDEX_NAME'] ?? ''),
$rows
)));
}
private function dropIndexIfExists(string $table, string $index): void
{
if ($index === '') {
return;
}
$exists = $this->db->query(
'SELECT COUNT(*) AS aggregate
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = ?
AND INDEX_NAME = ?',
[$table, $index]
)->getRow();
if ((int) ($exists->aggregate ?? 0) === 0) {
return;
}
$this->db->query(sprintf(
'ALTER TABLE %s DROP INDEX %s',
$this->db->escapeIdentifiers($table),
$this->db->escapeIdentifiers($index)
));
}
}