Make mig downgrade failsafe

This commit is contained in:
phl0
2025-01-02 22:02:55 +01:00
parent c050e20819
commit 16d242876c

View File

@@ -40,12 +40,18 @@ class Migration_clubstations extends CI_Migration {
$this->db->query("ALTER TABLE `cat` MODIFY operator INT(6) NOT NULL;"); $this->db->query("ALTER TABLE `cat` MODIFY operator INT(6) NOT NULL;");
} }
public function down() { public function down() {
// Due the risk of data loss we can't drop any new created columns or tables // Due the risk of data loss we can't drop any new created columns or tables
// But we can drop some of the timestamp columns // But we can drop some of the timestamp columns
$this->db->query("ALTER TABLE `users` DROP COLUMN created_at;"); if ($this->db->field_exists('created_at', 'users')) {
$this->db->query("ALTER TABLE `users` DROP COLUMN modified_at;"); $this->db->query("ALTER TABLE `users` DROP COLUMN created_at;");
$this->db->query("ALTER TABLE `api` DROP COLUMN created_at;"); }
if ($this->db->field_exists('modified_at', 'users')) {
$this->db->query("ALTER TABLE `users` DROP COLUMN modified_at;");
}
if ($this->db->field_exists('created_at', 'api')) {
$this->db->query("ALTER TABLE `api` DROP COLUMN created_at;");
}
} }
private function add_column_if_not_exists($table, $column, $definition) { private function add_column_if_not_exists($table, $column, $definition) {
@@ -61,4 +67,4 @@ class Migration_clubstations extends CI_Migration {
log_message('info', "Mig 230 - Column '$column' already exists in table '$table', skipping ALTER TABLE."); log_message('info', "Mig 230 - Column '$column' already exists in table '$table', skipping ALTER TABLE.");
} }
} }
} }