@@ -12,7 +12,16 @@ class RemoveClassAssignmentSemester extends Migration
|
||||
$this->db->tableExists('teacher_class')
|
||||
&& $this->db->fieldExists('semester', 'teacher_class')
|
||||
) {
|
||||
$this->forge->dropColumn('teacher_class', 'semester');
|
||||
foreach ($this->indexesContainingColumn('teacher_class', 'semester') as $index) {
|
||||
$this->db->query(
|
||||
'DROP INDEX ' . $this->db->escapeIdentifiers($index) . ' ON ' . $this->db->escapeIdentifiers('teacher_class')
|
||||
);
|
||||
}
|
||||
|
||||
$this->db->query(
|
||||
'ALTER TABLE ' . $this->db->escapeIdentifiers('teacher_class')
|
||||
. ' DROP COLUMN ' . $this->db->escapeIdentifiers('semester')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,12 +42,21 @@ class RemoveClassAssignmentSemester extends Migration
|
||||
}
|
||||
}
|
||||
|
||||
private function indexExists(string $table, string $index): bool
|
||||
private function indexesContainingColumn(string $table, string $column): array
|
||||
{
|
||||
$result = $this->db->query(
|
||||
'SHOW INDEX FROM `' . str_replace('`', '``', $table) . '` WHERE Key_name = ' . $this->db->escape($index)
|
||||
'SHOW INDEX FROM ' . $this->db->escapeIdentifiers($table)
|
||||
. ' WHERE Column_name = ' . $this->db->escape($column)
|
||||
);
|
||||
|
||||
return $result->getNumRows() > 0;
|
||||
$indexes = [];
|
||||
foreach ($result->getResultArray() as $row) {
|
||||
$index = (string) ($row['Key_name'] ?? '');
|
||||
if ($index !== '' && $index !== 'PRIMARY') {
|
||||
$indexes[] = $index;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(array_unique($indexes));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user