From b32cb8209636d12c061b0604cbec91f1c849f5b2 Mon Sep 17 00:00:00 2001 From: phl0 Date: Fri, 4 Apr 2025 10:10:45 +0200 Subject: [PATCH] Add foreign key constraint also to satmode table --- application/config/migration.php | 2 +- application/migrations/241_satmode_fk.php | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 application/migrations/241_satmode_fk.php diff --git a/application/config/migration.php b/application/config/migration.php index e3a13ec96..67c62df4d 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -22,7 +22,7 @@ $config['migration_enabled'] = TRUE; | */ -$config['migration_version'] = 240; +$config['migration_version'] = 241; /* |-------------------------------------------------------------------------- diff --git a/application/migrations/241_satmode_fk.php b/application/migrations/241_satmode_fk.php new file mode 100644 index 000000000..8e6934d4b --- /dev/null +++ b/application/migrations/241_satmode_fk.php @@ -0,0 +1,22 @@ +dbtry("delete from satellitemode where not exists (select 1 from satellite where satelliteid = satellite.id);"); + $this->dbtry("ALTER TABLE satellitemode ADD CONSTRAINT satmode_satellite_FK FOREIGN KEY (satelliteid) REFERENCES satellite (id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + } + + public function down() { + $this->dbtry("alter table satellitemode drop foreign key satmode_satellite_FK;"); + } + function dbtry($what) { + try { + $this->db->query($what); + } catch (Exception $e) { + log_message("error", "Something gone wrong while altering FKs: ".$e." // Executing: ".$this->db->last_query()); + } + } +}