This commit is contained in:
+2
-2
@@ -31,8 +31,8 @@ class CreateStudentSectionDistributionDrafts extends Migration
|
||||
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addUniqueKey(['student_id', 'school_year'], 'unique_distribution_draft_student_year');
|
||||
$this->forge->addKey(['class_id', 'school_year', 'status'], false, 'distribution_draft_class_year_status');
|
||||
$this->forge->addKey(['class_section_id', 'school_year'], false, 'distribution_draft_section_year');
|
||||
$this->forge->addKey(['class_id', 'school_year', 'status'], false, false, 'distribution_draft_class_year_status');
|
||||
$this->forge->addKey(['class_section_id', 'school_year'], false, false, 'distribution_draft_section_year');
|
||||
$this->forge->createTable('student_section_distribution_drafts');
|
||||
}
|
||||
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class FixStudentSectionDistributionDraftIndexes extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
if (! $this->db->tableExists('student_section_distribution_drafts')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->dropIndexIfExists('student_section_distribution_drafts', 'class_id_school_year_status');
|
||||
$this->dropIndexIfExists('student_section_distribution_drafts', 'class_section_id_school_year');
|
||||
$this->dropIndexIfExists('student_section_distribution_drafts', 'distribution_draft_class_year_status');
|
||||
$this->dropIndexIfExists('student_section_distribution_drafts', 'distribution_draft_section_year');
|
||||
|
||||
$this->db->query(
|
||||
'CREATE INDEX distribution_draft_class_year_status ' .
|
||||
'ON student_section_distribution_drafts (class_id, school_year, status)'
|
||||
);
|
||||
$this->db->query(
|
||||
'CREATE INDEX distribution_draft_section_year ' .
|
||||
'ON student_section_distribution_drafts (class_section_id, school_year)'
|
||||
);
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
if (! $this->db->tableExists('student_section_distribution_drafts')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->dropIndexIfExists('student_section_distribution_drafts', 'distribution_draft_class_year_status');
|
||||
$this->dropIndexIfExists('student_section_distribution_drafts', 'distribution_draft_section_year');
|
||||
}
|
||||
|
||||
private function dropIndexIfExists(string $table, string $indexName): void
|
||||
{
|
||||
$row = $this->db->query(
|
||||
'SHOW INDEX FROM ' . $this->db->protectIdentifiers($table, true) .
|
||||
' WHERE Key_name = ' . $this->db->escape($indexName)
|
||||
)->getRowArray();
|
||||
|
||||
if ($row) {
|
||||
$this->db->query(
|
||||
'ALTER TABLE ' . $this->db->protectIdentifiers($table, true) .
|
||||
' DROP INDEX ' . $this->db->protectIdentifiers($indexName)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user