+12
-8
@@ -16,6 +16,10 @@ class Database extends Config
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
if (ENVIRONMENT === 'testing') {
|
||||
$this->defaultGroup = 'tests';
|
||||
}
|
||||
|
||||
$this->default = [
|
||||
'DSN' => '',
|
||||
'hostname' => env('database.default.hostname'),
|
||||
@@ -38,22 +42,22 @@ class Database extends Config
|
||||
|
||||
$this->tests = [
|
||||
'DSN' => '',
|
||||
'hostname' => env('database.tests.hostname', env('database.default.hostname')),
|
||||
'username' => env('database.tests.username', env('database.default.username')),
|
||||
'password' => env('database.tests.password', env('database.default.password')),
|
||||
'database' => env('database.tests.database', env('database.default.database')),
|
||||
'hostname' => env('TEST_DB_HOST', env('database.tests.hostname', env('database.default.hostname'))),
|
||||
'username' => env('TEST_DB_USER', env('database.tests.username', env('database.default.username'))),
|
||||
'password' => env('TEST_DB_PASSWORD', env('database.tests.password', env('database.default.password'))),
|
||||
'database' => env('TEST_DB_NAME', env('database.tests.database', env('database.default.database'))),
|
||||
'DBDriver' => env('database.tests.DBDriver', env('database.default.DBDriver', 'MySQLi')),
|
||||
'DBPrefix' => env('database.tests.DBPrefix', 'db_'),
|
||||
'DBPrefix' => env('database.tests.DBPrefix', ''),
|
||||
'pConnect' => false,
|
||||
'DBDebug' => true,
|
||||
'charset' => 'utf8',
|
||||
'DBCollat' => 'utf8_general_ci',
|
||||
'charset' => 'utf8mb4',
|
||||
'DBCollat' => 'utf8mb4_general_ci',
|
||||
'swapPre' => '',
|
||||
'encrypt' => false,
|
||||
'compress' => false,
|
||||
'strictOn' => false,
|
||||
'failover' => [],
|
||||
'port' => (int) env('database.tests.port', env('database.default.port', 3306)),
|
||||
'port' => (int) env('TEST_DB_PORT', env('database.tests.port', env('database.default.port', 3306))),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,10 @@ class FixPrintRequestsForeignKey extends Migration
|
||||
// Drop the old foreign key if it exists
|
||||
$this->forge->dropForeignKey('print_requests', 'print_requests_class_id_foreign');
|
||||
|
||||
if (! $this->db->tableExists('class_sections')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the new foreign key
|
||||
$this->forge->addForeignKey('class_id', 'class_sections', 'id', 'CASCADE', 'CASCADE');
|
||||
|
||||
|
||||
@@ -9,7 +9,14 @@ class RevertPrintRequestsForeignKey extends Migration
|
||||
public function up()
|
||||
{
|
||||
// Drop the old foreign key if it exists
|
||||
$this->forge->dropForeignKey('print_requests', 'print_requests_class_id_foreign');
|
||||
$db = \Config\Database::connect();
|
||||
$keys = $db->getForeignKeyData('print_requests');
|
||||
foreach ($keys as $key) {
|
||||
if ($key->constraint_name === 'print_requests_class_id_foreign') {
|
||||
$this->forge->dropForeignKey('print_requests', 'print_requests_class_id_foreign');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the new foreign key
|
||||
$this->forge->addForeignKey('class_id', 'classes', 'id', 'CASCADE', 'CASCADE');
|
||||
|
||||
@@ -12,8 +12,7 @@ class FixPrintRequestsForeignKeyAgain extends Migration
|
||||
$keys = $db->getForeignKeyData('print_requests');
|
||||
foreach ($keys as $key) {
|
||||
if ($key->constraint_name === 'print_requests_class_id_foreign') {
|
||||
$this->forge->dropForeignKey('print_requests', 'print_requests_class_id_foreign');
|
||||
break;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,10 +8,18 @@ class RemoveClassAssignmentSemester extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
if ($this->db->tableExists('teacher_class')) {
|
||||
if ($this->db->tableExists('teacher_class') && $this->db->fieldExists('semester', 'teacher_class')) {
|
||||
if ($this->indexExists('teacher_class', 'unique_teacher_assignment')) {
|
||||
$this->db->query('ALTER TABLE `teacher_class` DROP INDEX `unique_teacher_assignment`');
|
||||
}
|
||||
|
||||
$this->forge->dropColumn('teacher_class', 'semester');
|
||||
|
||||
$this->db->query(
|
||||
'ALTER TABLE `teacher_class` ADD UNIQUE KEY `unique_teacher_assignment` (`teacher_id`, `class_section_id`, `school_year`)'
|
||||
);
|
||||
}
|
||||
if ($this->db->tableExists('student_class')) {
|
||||
if ($this->db->tableExists('student_class') && $this->db->fieldExists('semester', 'student_class')) {
|
||||
$this->forge->dropColumn('student_class', 'semester');
|
||||
}
|
||||
}
|
||||
@@ -39,4 +47,13 @@ class RemoveClassAssignmentSemester extends Migration
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function indexExists(string $table, string $index): bool
|
||||
{
|
||||
$result = $this->db->query(
|
||||
'SHOW INDEX FROM `' . str_replace('`', '``', $table) . '` WHERE Key_name = ' . $this->db->escape($index)
|
||||
);
|
||||
|
||||
return $result->getNumRows() > 0;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
use CodeIgniter\Database\RawSql;
|
||||
|
||||
class CreateTeacherSubmissionNotificationHistory extends Migration
|
||||
{
|
||||
@@ -56,7 +57,7 @@ class CreateTeacherSubmissionNotificationHistory extends Migration
|
||||
'sent_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => false,
|
||||
'default' => 'CURRENT_TIMESTAMP',
|
||||
'default' => new RawSql('CURRENT_TIMESTAMP'),
|
||||
],
|
||||
]);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
use CodeIgniter\Database\RawSql;
|
||||
|
||||
class CreateExamDraftSubmissions extends Migration
|
||||
{
|
||||
@@ -89,7 +90,7 @@ class CreateExamDraftSubmissions extends Migration
|
||||
'created_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => false,
|
||||
'default' => 'CURRENT_TIMESTAMP',
|
||||
'default' => new RawSql('CURRENT_TIMESTAMP'),
|
||||
],
|
||||
'updated_at' => [
|
||||
'type' => 'DATETIME',
|
||||
|
||||
@@ -24,7 +24,7 @@ class CreatePlacementLevels extends Migration
|
||||
]);
|
||||
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addKey(['student_id', 'school_year'], true);
|
||||
$this->forge->addUniqueKey(['student_id', 'school_year'], 'unique_student_school_year');
|
||||
$this->forge->addKey('school_year');
|
||||
$this->forge->createTable('placement_levels');
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class CreatePlacementScores extends Migration
|
||||
]);
|
||||
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addKey(['batch_id', 'student_id'], true);
|
||||
$this->forge->addUniqueKey(['batch_id', 'student_id'], 'unique_batch_student');
|
||||
$this->forge->addKey('batch_id');
|
||||
$this->forge->createTable('placement_scores');
|
||||
}
|
||||
|
||||
@@ -65,7 +65,10 @@ class CreateMissingScoreOverrides extends Migration
|
||||
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addKey(['class_section_id', 'semester', 'school_year', 'item_type'], false, 'missing_score_overrides_scope');
|
||||
$this->forge->addKey(['student_id', 'class_section_id', 'semester', 'school_year', 'item_type', 'item_index'], true, 'missing_score_overrides_unique');
|
||||
$this->forge->addUniqueKey(
|
||||
['student_id', 'class_section_id', 'semester', 'school_year', 'item_type', 'item_index'],
|
||||
'missing_score_overrides_unique'
|
||||
);
|
||||
$this->forge->createTable('missing_score_overrides', true);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,13 +10,18 @@ class AddStyleToPreferences extends Migration
|
||||
{
|
||||
if ($this->db->tableExists('user_preferences')) {
|
||||
if (! $this->db->fieldExists('style_color', 'user_preferences')) {
|
||||
$styleColor = [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 32,
|
||||
'null' => true,
|
||||
];
|
||||
|
||||
if ($this->db->fieldExists('timezone', 'user_preferences')) {
|
||||
$styleColor['after'] = 'timezone';
|
||||
}
|
||||
|
||||
$this->forge->addColumn('user_preferences', [
|
||||
'style_color' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 32,
|
||||
'null' => true,
|
||||
'after' => 'timezone',
|
||||
],
|
||||
'style_color' => $styleColor,
|
||||
]);
|
||||
}
|
||||
if (! $this->db->fieldExists('menu_color', 'user_preferences')) {
|
||||
|
||||
@@ -122,6 +122,8 @@ class FixSchoolYearColumns extends Migration
|
||||
);
|
||||
}
|
||||
|
||||
$this->db->resetDataCache();
|
||||
|
||||
// These annual entities lacked a school_year column in the audited schema.
|
||||
foreach (['class_progress_reports', 'exams', 'print_requests'] as $table) {
|
||||
if ($this->db->tableExists($table) && ! $this->db->fieldExists('school_year', $table)) {
|
||||
@@ -137,6 +139,8 @@ class FixSchoolYearColumns extends Migration
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->resetDataCache();
|
||||
|
||||
// Validate and normalize every direct owner to VARCHAR(9).
|
||||
foreach ($this->yearOwnerTables as $table) {
|
||||
if (! $this->db->tableExists($table) || ! $this->db->fieldExists('school_year', $table)) {
|
||||
@@ -163,6 +167,8 @@ class FixSchoolYearColumns extends Migration
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->resetDataCache();
|
||||
|
||||
// Remove redundant columns. Dependent non-primary indexes are removed first.
|
||||
foreach ($this->tablesWithoutDirectYear as $table) {
|
||||
if (! $this->db->tableExists($table) || ! $this->db->fieldExists('school_year', $table)) {
|
||||
@@ -172,6 +178,7 @@ class FixSchoolYearColumns extends Migration
|
||||
$this->dropIndexesContainingColumn($table, 'school_year');
|
||||
$this->dropChecksContainingColumn($table, 'school_year');
|
||||
$this->forge->dropColumn($table, 'school_year');
|
||||
$this->db->resetDataCache();
|
||||
}
|
||||
|
||||
foreach ($this->tablesWithoutDirectSemester as $table) {
|
||||
@@ -182,8 +189,11 @@ class FixSchoolYearColumns extends Migration
|
||||
$this->dropIndexesContainingColumn($table, 'semester');
|
||||
$this->dropChecksContainingColumn($table, 'semester');
|
||||
$this->forge->dropColumn($table, 'semester');
|
||||
$this->db->resetDataCache();
|
||||
}
|
||||
|
||||
$this->db->resetDataCache();
|
||||
|
||||
foreach ($this->yearOwnerTables as $table) {
|
||||
if (! $this->db->tableExists($table) || ! $this->db->fieldExists('school_year', $table)) {
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user