@@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Database\Migrations;
|
||||||
|
|
||||||
|
use CodeIgniter\Database\Migration;
|
||||||
|
|
||||||
|
class RepairModelSchemaColumns extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
$this->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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ class DBReset
|
|||||||
private const DB_GROUP = 'tests';
|
private const DB_GROUP = 'tests';
|
||||||
|
|
||||||
protected static array $excludeTables = ['migrations', 'school_years'];
|
protected static array $excludeTables = ['migrations', 'school_years'];
|
||||||
|
private static bool $migrated = false;
|
||||||
|
|
||||||
public static function resetDatabase(): void
|
public static function resetDatabase(): void
|
||||||
{
|
{
|
||||||
@@ -34,6 +35,8 @@ class DBReset
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self::migrateTestDatabase($db);
|
||||||
|
|
||||||
$db->disableForeignKeyChecks();
|
$db->disableForeignKeyChecks();
|
||||||
|
|
||||||
$tables = $db->resetDataCache()->listTables();
|
$tables = $db->resetDataCache()->listTables();
|
||||||
@@ -47,6 +50,18 @@ class DBReset
|
|||||||
$db->enableForeignKeyChecks();
|
$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
|
private static function assertSafeTestDatabase(array $defaultConfig, array $testConfig): void
|
||||||
{
|
{
|
||||||
$defaultDatabase = (string) ($defaultConfig['database'] ?? '');
|
$defaultDatabase = (string) ($defaultConfig['database'] ?? '');
|
||||||
|
|||||||
Reference in New Issue
Block a user