From 3408d5ab206e911a276670529862a5fe6e1ec8ac Mon Sep 17 00:00:00 2001 From: root Date: Wed, 15 Jul 2026 23:42:16 -0400 Subject: [PATCH] fix tests failure --- ...-07-16-000300_RepairModelSchemaColumns.php | 80 +++++++++++++++++++ tests/_support/DBReset.php | 15 ++++ 2 files changed, 95 insertions(+) create mode 100644 app/Database/Migrations/2026-07-16-000300_RepairModelSchemaColumns.php diff --git a/app/Database/Migrations/2026-07-16-000300_RepairModelSchemaColumns.php b/app/Database/Migrations/2026-07-16-000300_RepairModelSchemaColumns.php new file mode 100644 index 0000000..d0863fc --- /dev/null +++ b/app/Database/Migrations/2026-07-16-000300_RepairModelSchemaColumns.php @@ -0,0 +1,80 @@ +addColumnIfMissing('calendar_events', 'event_type', [ + 'type' => 'VARCHAR', + 'constraint' => 120, + 'null' => true, + 'after' => 'description', + ]); + + $this->addColumnIfMissing('exams', 'school_year', [ + 'type' => 'VARCHAR', + 'constraint' => 20, + 'null' => true, + 'after' => 'exam_name', + ]); + + $this->addColumnIfMissing('flag', 'action_taken', [ + 'type' => 'TEXT', + 'null' => true, + 'after' => 'close_description', + ]); + + $this->addColumnIfMissing('user_preferences', 'timezone', [ + 'type' => 'VARCHAR', + 'constraint' => 64, + 'null' => true, + 'after' => 'language', + ]); + + $this->addColumnIfMissing('student_class', 'is_event_only', [ + 'type' => 'TINYINT', + 'constraint' => 1, + 'unsigned' => true, + 'default' => 0, + 'after' => 'school_year', + ]); + } + + public function down(): void + { + foreach ([ + 'calendar_events' => 'event_type', + 'exams' => 'school_year', + 'flag' => 'action_taken', + 'user_preferences' => 'timezone', + 'student_class' => 'is_event_only', + ] as $table => $column) { + if ($this->db->tableExists($table) && $this->db->fieldExists($column, $table)) { + $this->forge->dropColumn($table, $column); + } + } + } + + private function addColumnIfMissing(string $table, string $column, array $definition): void + { + if (! $this->db->tableExists($table)) { + return; + } + + $fields = $this->db->getFieldNames($table); + + if (in_array($column, $fields, true)) { + return; + } + + if (isset($definition['after']) && ! in_array($definition['after'], $fields, true)) { + unset($definition['after']); + } + + $this->forge->addColumn($table, [$column => $definition]); + } +} diff --git a/tests/_support/DBReset.php b/tests/_support/DBReset.php index ede98d9..2f9335b 100644 --- a/tests/_support/DBReset.php +++ b/tests/_support/DBReset.php @@ -10,6 +10,7 @@ class DBReset private const DB_GROUP = 'tests'; protected static array $excludeTables = ['migrations', 'school_years']; + private static bool $migrated = false; public static function resetDatabase(): void { @@ -34,6 +35,8 @@ class DBReset ); } + self::migrateTestDatabase($db); + $db->disableForeignKeyChecks(); $tables = $db->resetDataCache()->listTables(); @@ -47,6 +50,18 @@ class DBReset $db->enableForeignKeyChecks(); } + private static function migrateTestDatabase($db): void + { + if (self::$migrated) { + return; + } + + $runner = \Config\Services::migrations(config('Migrations'), $db, false); + $runner->setNamespace('App')->latest(); + + self::$migrated = true; + } + private static function assertSafeTestDatabase(array $defaultConfig, array $testConfig): void { $defaultDatabase = (string) ($defaultConfig['database'] ?? '');