60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Database\Migrations;
|
|
|
|
use CodeIgniter\Database\Migration;
|
|
|
|
class RemoveClassAssignmentSemester extends Migration
|
|
{
|
|
public function up()
|
|
{
|
|
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') && $this->db->fieldExists('semester', 'student_class')) {
|
|
$this->forge->dropColumn('student_class', 'semester');
|
|
}
|
|
}
|
|
|
|
public function down()
|
|
{
|
|
if ($this->db->tableExists('teacher_class')) {
|
|
$this->forge->addColumn('teacher_class', [
|
|
'semester' => [
|
|
'type' => 'VARCHAR',
|
|
'constraint' => 25,
|
|
'null' => false,
|
|
'default' => '',
|
|
],
|
|
]);
|
|
}
|
|
if ($this->db->tableExists('student_class')) {
|
|
$this->forge->addColumn('student_class', [
|
|
'semester' => [
|
|
'type' => 'VARCHAR',
|
|
'constraint' => 255,
|
|
'null' => false,
|
|
'default' => '',
|
|
],
|
|
]);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|