@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user