From 10108c4461ebdba1dbaaaaa81567c45e2078595f Mon Sep 17 00:00:00 2001 From: int2001 Date: Tue, 25 Jun 2024 05:07:39 +0000 Subject: [PATCH 1/4] Adding FKs to the Tables --- application/config/migration.php | 2 +- application/migrations/206_add_fks.php | 62 ++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 application/migrations/206_add_fks.php diff --git a/application/config/migration.php b/application/config/migration.php index 1b160613a..292abe539 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -22,7 +22,7 @@ $config['migration_enabled'] = TRUE; | */ -$config['migration_version'] = 205; +$config['migration_version'] = 206; /* |-------------------------------------------------------------------------- diff --git a/application/migrations/206_add_fks.php b/application/migrations/206_add_fks.php new file mode 100644 index 000000000..f8a1ed39a --- /dev/null +++ b/application/migrations/206_add_fks.php @@ -0,0 +1,62 @@ +db->query("delete from api where not exists (select 1 from users where user_id = api.user_id);"); + $this->db->query("delete from bandxuser where not exists (select 1 from users where user_id = bandxuser.userid);"); + $this->db->query("delete from station_logbooks_relationship where not exists (select 1 from station_logbooks where station_logbook_id = station_logbooks_relationship.station_logbook_id);"); + $this->db->query("delete from eQSL_images where not exists (select 1 from ".$this->config->item('table_name')." log where log.COL_PRIMARY_KEY=eQSL_images.qso_id);"); + $this->db->query("delete from qsl_images where not exists (select 1 from ".$this->config->item('table_name')." log where log.COL_PRIMARY_KEY=qsl_images.qsoid);"); + $this->db->query("delete from station_logbooks where not exists (select 1 from users where user_id = station_logbooks.user_id);"); + $this->db->query("delete from oqrs where not exists (select 1 from station_profile where station_id = oqrs.station_id);"); + $this->db->query("delete from lotw_certs where not exists (select 1 from users where user_id = lotw_certs.user_id);"); + $this->db->query("delete from label_types where not exists (select 1 from users where user_id = label_types.user_id);"); + $this->db->query("delete from notes where not exists (select 1 from users where user_id = notes.user_id);"); + $this->db->query("delete from station_profile where not exists (select 1 from users where user_id = station_profile.user_id);"); + $this->db->query("delete from cat where not exists (select 1 from users where user_id = cat.user_id);"); + $this->db->query("delete from ".$this->config->item('table_name')." where not exists (select 1 from station_profile where station_id = ".$this->config->item('table_name').".station_id);"); + + $this->db->query("ALTER TABLE api ADD CONSTRAINT api_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE bandxuser ADD CONSTRAINT bandxuser_users_FK FOREIGN KEY (userid) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE station_logbooks_relationship ADD CONSTRAINT station_logbooks_relationship_station_profile_FK FOREIGN KEY (station_location_id) REFERENCES station_profile (station_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE eQSL_images ADD CONSTRAINT eQSL_images_TABLE_HRD_CONTACTS_V01_FK FOREIGN KEY (qso_id) REFERENCES ".$this->config->item('table_name')." (COL_PRIMARY_KEY) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE qsl_images ADD CONSTRAINT qsl_images_TABLE_HRD_CONTACTS_V01_FK FOREIGN KEY (qsoid) REFERENCES ".$this->config->item('table_name')." (COL_PRIMARY_KEY) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE station_logbooks ADD CONSTRAINT station_logbooks_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE oqrs ADD CONSTRAINT oqrs_station_profile_FK FOREIGN KEY (station_id) REFERENCES station_profile (station_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE lotw_certs ADD CONSTRAINT lotw_certs_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE label_types ADD CONSTRAINT label_types_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE notes ADD CONSTRAINT notes_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE station_profile ADD CONSTRAINT station_profile_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE cat ADD CONSTRAINT cat_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->db->query("ALTER TABLE ".$this->config->item('table_name')." ADD CONSTRAINT TABLE_HRD_CONTACTS_V01_station_profile_FK FOREIGN KEY (station_id) REFERENCES station_profile (station_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + } catch (Exception $e) { + log_message("error", "Something gone wrong while creating FKs: ".$e." // Executing: ".$this->db->last_query()); + } + } + + public function down(){ + log_message("error","invoking downmig 206"); + try { + $this->db->query("alter table station_logbooks drop foreign key station_logbooks_users_FK;"); + $this->db->query("alter table oqrs drop foreign key oqrs_station_profile_FK;"); + $this->db->query("alter table api drop foreign key api_users_FK;"); + $this->db->query("alter table station_logbooks_relationship drop foreign key station_logbooks_relationship_station_profile_FK;"); + $this->db->query("alter table lotw_certs drop foreign key lotw_certs_users_FK;"); + $this->db->query("alter table qsl_images drop foreign key qsl_images_TABLE_HRD_CONTACTS_V01_FK;"); + $this->db->query("alter table eQSL_images drop foreign key eQSL_images_TABLE_HRD_CONTACTS_V01_FK;"); + $this->db->query("alter table label_types drop foreign key label_types_users_FK;"); + $this->db->query("alter table notes drop foreign key notes_users_FK;"); + $this->db->query("alter table station_profile drop foreign key station_profile_users_FK;"); + $this->db->query("alter table cat drop foreign key cat_users_FK;"); + $this->db->query("alter table bandxuser drop foreign key bandxuser_users_FK;"); + $this->db->query("alter table ".$this->config->item('table_name')." drop foreign key TABLE_HRD_CONTACTS_V01_station_profile_FK;"); + } catch (Exception $e) { + log_message("error", "Something gone wrong while dropping FKs: ".$e." // Executing: ".$this->db->last_query()); + } + } +} From 76a075b924e8a5b2a2b0e97671b779728bc8c5fa Mon Sep 17 00:00:00 2001 From: int2001 Date: Tue, 25 Jun 2024 12:21:22 +0000 Subject: [PATCH 2/4] Removed Debug-Msg for invoking migs --- application/migrations/206_add_fks.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/application/migrations/206_add_fks.php b/application/migrations/206_add_fks.php index f8a1ed39a..f0d828435 100644 --- a/application/migrations/206_add_fks.php +++ b/application/migrations/206_add_fks.php @@ -5,7 +5,6 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class Migration_add_fks extends CI_Migration { public function up() { - log_message("error","invoking mig 206"); try { $this->db->query("delete from api where not exists (select 1 from users where user_id = api.user_id);"); $this->db->query("delete from bandxuser where not exists (select 1 from users where user_id = bandxuser.userid);"); @@ -40,7 +39,6 @@ class Migration_add_fks extends CI_Migration { } public function down(){ - log_message("error","invoking downmig 206"); try { $this->db->query("alter table station_logbooks drop foreign key station_logbooks_users_FK;"); $this->db->query("alter table oqrs drop foreign key oqrs_station_profile_FK;"); From 8ca93e68ce75068318fc46494def869067487a61 Mon Sep 17 00:00:00 2001 From: int2001 Date: Tue, 25 Jun 2024 13:04:37 +0000 Subject: [PATCH 3/4] Failsafe per SQL not as "whole" --- application/migrations/206_add_fks.php | 87 +++++++++++++------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/application/migrations/206_add_fks.php b/application/migrations/206_add_fks.php index f0d828435..9be9bf054 100644 --- a/application/migrations/206_add_fks.php +++ b/application/migrations/206_add_fks.php @@ -5,56 +5,55 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class Migration_add_fks extends CI_Migration { public function up() { - try { - $this->db->query("delete from api where not exists (select 1 from users where user_id = api.user_id);"); - $this->db->query("delete from bandxuser where not exists (select 1 from users where user_id = bandxuser.userid);"); - $this->db->query("delete from station_logbooks_relationship where not exists (select 1 from station_logbooks where station_logbook_id = station_logbooks_relationship.station_logbook_id);"); - $this->db->query("delete from eQSL_images where not exists (select 1 from ".$this->config->item('table_name')." log where log.COL_PRIMARY_KEY=eQSL_images.qso_id);"); - $this->db->query("delete from qsl_images where not exists (select 1 from ".$this->config->item('table_name')." log where log.COL_PRIMARY_KEY=qsl_images.qsoid);"); - $this->db->query("delete from station_logbooks where not exists (select 1 from users where user_id = station_logbooks.user_id);"); - $this->db->query("delete from oqrs where not exists (select 1 from station_profile where station_id = oqrs.station_id);"); - $this->db->query("delete from lotw_certs where not exists (select 1 from users where user_id = lotw_certs.user_id);"); - $this->db->query("delete from label_types where not exists (select 1 from users where user_id = label_types.user_id);"); - $this->db->query("delete from notes where not exists (select 1 from users where user_id = notes.user_id);"); - $this->db->query("delete from station_profile where not exists (select 1 from users where user_id = station_profile.user_id);"); - $this->db->query("delete from cat where not exists (select 1 from users where user_id = cat.user_id);"); - $this->db->query("delete from ".$this->config->item('table_name')." where not exists (select 1 from station_profile where station_id = ".$this->config->item('table_name').".station_id);"); + $this->dbtry("delete from api where not exists (select 1 from users where user_id = api.user_id);"); + $this->dbtry("delete from bandxuser where not exists (select 1 from users where user_id = bandxuser.userid);"); + $this->dbtry("delete from station_logbooks_relationship where not exists (select 1 from station_logbooks where station_logbook_id = station_logbooks_relationship.station_logbook_id);"); + $this->dbtry("delete from eQSL_images where not exists (select 1 from ".$this->config->item('table_name')." log where log.COL_PRIMARY_KEY=eQSL_images.qso_id);"); + $this->dbtry("delete from qsl_images where not exists (select 1 from ".$this->config->item('table_name')." log where log.COL_PRIMARY_KEY=qsl_images.qsoid);"); + $this->dbtry("delete from station_logbooks where not exists (select 1 from users where user_id = station_logbooks.user_id);"); + $this->dbtry("delete from oqrs where not exists (select 1 from station_profile where station_id = oqrs.station_id);"); + $this->dbtry("delete from lotw_certs where not exists (select 1 from users where user_id = lotw_certs.user_id);"); + $this->dbtry("delete from label_types where not exists (select 1 from users where user_id = label_types.user_id);"); + $this->dbtry("delete from notes where not exists (select 1 from users where user_id = notes.user_id);"); + $this->dbtry("delete from station_profile where not exists (select 1 from users where user_id = station_profile.user_id);"); + $this->dbtry("delete from cat where not exists (select 1 from users where user_id = cat.user_id);"); + $this->dbtry("delete from ".$this->config->item('table_name')." where not exists (select 1 from station_profile where station_id = ".$this->config->item('table_name').".station_id);"); - $this->db->query("ALTER TABLE api ADD CONSTRAINT api_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE bandxuser ADD CONSTRAINT bandxuser_users_FK FOREIGN KEY (userid) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE station_logbooks_relationship ADD CONSTRAINT station_logbooks_relationship_station_profile_FK FOREIGN KEY (station_location_id) REFERENCES station_profile (station_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE eQSL_images ADD CONSTRAINT eQSL_images_TABLE_HRD_CONTACTS_V01_FK FOREIGN KEY (qso_id) REFERENCES ".$this->config->item('table_name')." (COL_PRIMARY_KEY) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE qsl_images ADD CONSTRAINT qsl_images_TABLE_HRD_CONTACTS_V01_FK FOREIGN KEY (qsoid) REFERENCES ".$this->config->item('table_name')." (COL_PRIMARY_KEY) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE station_logbooks ADD CONSTRAINT station_logbooks_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE oqrs ADD CONSTRAINT oqrs_station_profile_FK FOREIGN KEY (station_id) REFERENCES station_profile (station_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE lotw_certs ADD CONSTRAINT lotw_certs_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE label_types ADD CONSTRAINT label_types_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE notes ADD CONSTRAINT notes_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE station_profile ADD CONSTRAINT station_profile_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE cat ADD CONSTRAINT cat_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - $this->db->query("ALTER TABLE ".$this->config->item('table_name')." ADD CONSTRAINT TABLE_HRD_CONTACTS_V01_station_profile_FK FOREIGN KEY (station_id) REFERENCES station_profile (station_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); - } catch (Exception $e) { - log_message("error", "Something gone wrong while creating FKs: ".$e." // Executing: ".$this->db->last_query()); - } + $this->dbtry("ALTER TABLE api ADD CONSTRAINT api_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE bandxuser ADD CONSTRAINT bandxuser_users_FK FOREIGN KEY (userid) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE station_logbooks_relationship ADD CONSTRAINT station_logbooks_relationship_station_profile_FK FOREIGN KEY (station_location_id) REFERENCES station_profile (station_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE eQSL_images ADD CONSTRAINT eQSL_images_TABLE_HRD_CONTACTS_V01_FK FOREIGN KEY (qso_id) REFERENCES ".$this->config->item('table_name')." (COL_PRIMARY_KEY) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE qsl_images ADD CONSTRAINT qsl_images_TABLE_HRD_CONTACTS_V01_FK FOREIGN KEY (qsoid) REFERENCES ".$this->config->item('table_name')." (COL_PRIMARY_KEY) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE station_logbooks ADD CONSTRAINT station_logbooks_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE oqrs ADD CONSTRAINT oqrs_station_profile_FK FOREIGN KEY (station_id) REFERENCES station_profile (station_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE lotw_certs ADD CONSTRAINT lotw_certs_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE label_types ADD CONSTRAINT label_types_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE notes ADD CONSTRAINT notes_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE station_profile ADD CONSTRAINT station_profile_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE cat ADD CONSTRAINT cat_users_FK FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE ".$this->config->item('table_name')." ADD CONSTRAINT TABLE_HRD_CONTACTS_V01_station_profile_FK FOREIGN KEY (station_id) REFERENCES station_profile (station_id) ON DELETE CASCADE ON UPDATE RESTRICT;"); } public function down(){ + $this->dbtry("alter table station_logbooks drop foreign key station_logbooks_users_FK;"); + $this->dbtry("alter table oqrs drop foreign key oqrs_station_profile_FK;"); + $this->dbtry("alter table api drop foreign key api_users_FK;"); + $this->dbtry("alter table station_logbooks_relationship drop foreign key station_logbooks_relationship_station_profile_FK;"); + $this->dbtry("alter table lotw_certs drop foreign key lotw_certs_users_FK;"); + $this->dbtry("alter table qsl_images drop foreign key qsl_images_TABLE_HRD_CONTACTS_V01_FK;"); + $this->dbtry("alter table eQSL_images drop foreign key eQSL_images_TABLE_HRD_CONTACTS_V01_FK;"); + $this->dbtry("alter table label_types drop foreign key label_types_users_FK;"); + $this->dbtry("alter table notes drop foreign key notes_users_FK;"); + $this->dbtry("alter table station_profile drop foreign key station_profile_users_FK;"); + $this->dbtry("alter table cat drop foreign key cat_users_FK;"); + $this->dbtry("alter table bandxuser drop foreign key bandxuser_users_FK;"); + $this->dbtry("alter table ".$this->config->item('table_name')." drop foreign key TABLE_HRD_CONTACTS_V01_station_profile_FK;"); + } + function dbtry($what) { try { - $this->db->query("alter table station_logbooks drop foreign key station_logbooks_users_FK;"); - $this->db->query("alter table oqrs drop foreign key oqrs_station_profile_FK;"); - $this->db->query("alter table api drop foreign key api_users_FK;"); - $this->db->query("alter table station_logbooks_relationship drop foreign key station_logbooks_relationship_station_profile_FK;"); - $this->db->query("alter table lotw_certs drop foreign key lotw_certs_users_FK;"); - $this->db->query("alter table qsl_images drop foreign key qsl_images_TABLE_HRD_CONTACTS_V01_FK;"); - $this->db->query("alter table eQSL_images drop foreign key eQSL_images_TABLE_HRD_CONTACTS_V01_FK;"); - $this->db->query("alter table label_types drop foreign key label_types_users_FK;"); - $this->db->query("alter table notes drop foreign key notes_users_FK;"); - $this->db->query("alter table station_profile drop foreign key station_profile_users_FK;"); - $this->db->query("alter table cat drop foreign key cat_users_FK;"); - $this->db->query("alter table bandxuser drop foreign key bandxuser_users_FK;"); - $this->db->query("alter table ".$this->config->item('table_name')." drop foreign key TABLE_HRD_CONTACTS_V01_station_profile_FK;"); + $this->db->query($what); } catch (Exception $e) { log_message("error", "Something gone wrong while dropping FKs: ".$e." // Executing: ".$this->db->last_query()); } - } + } } From 3d7af0dccf52905cf5c00edc64cd9c782aacd699 Mon Sep 17 00:00:00 2001 From: int2001 Date: Tue, 25 Jun 2024 13:05:26 +0000 Subject: [PATCH 4/4] Fixed typo --- application/migrations/206_add_fks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/migrations/206_add_fks.php b/application/migrations/206_add_fks.php index 9be9bf054..6423ab511 100644 --- a/application/migrations/206_add_fks.php +++ b/application/migrations/206_add_fks.php @@ -53,7 +53,7 @@ class Migration_add_fks extends CI_Migration { try { $this->db->query($what); } catch (Exception $e) { - log_message("error", "Something gone wrong while dropping FKs: ".$e." // Executing: ".$this->db->last_query()); + log_message("error", "Something gone wrong while altering FKs: ".$e." // Executing: ".$this->db->last_query()); } } }