From 08f75b0b99a96a919158ce818d6f59ed61ff0dd2 Mon Sep 17 00:00:00 2001 From: Nelson Chan <3271800+chakflying@users.noreply.github.com> Date: Sun, 31 Mar 2024 12:04:22 +0800 Subject: [PATCH] Fix: Add missing FK for monitor-tls-info table (#4632) --- db/knex_init_db.js | 5 ++++- .../patch-monitor-tls-info-add-fk.sql | 18 ++++++++++++++++++ server/database.js | 3 ++- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 db/old_migrations/patch-monitor-tls-info-add-fk.sql diff --git a/db/knex_init_db.js b/db/knex_init_db.js index 7ce1bb48e..46bff4bfa 100644 --- a/db/knex_init_db.js +++ b/db/knex_init_db.js @@ -318,7 +318,10 @@ async function createTables() { // monitor_tls_info await knex.schema.createTable("monitor_tls_info", (table) => { table.increments("id"); - table.integer("monitor_id").unsigned().notNullable(); //TODO: no fk ? + table.integer("monitor_id").unsigned().notNullable() + .references("id").inTable("monitor") + .onDelete("CASCADE") + .onUpdate("CASCADE"); table.text("info_json"); }); diff --git a/db/old_migrations/patch-monitor-tls-info-add-fk.sql b/db/old_migrations/patch-monitor-tls-info-add-fk.sql new file mode 100644 index 000000000..9b9c2d2cb --- /dev/null +++ b/db/old_migrations/patch-monitor-tls-info-add-fk.sql @@ -0,0 +1,18 @@ +BEGIN TRANSACTION; + +PRAGMA writable_schema = TRUE; + +UPDATE + SQLITE_MASTER +SET + sql = replace(sql, + 'monitor_id INTEGER NOT NULL', + 'monitor_id INTEGER NOT NULL REFERENCES [monitor] ([id]) ON DELETE CASCADE ON UPDATE CASCADE' +) +WHERE + name = 'monitor_tls_info' + AND type = 'table'; + +PRAGMA writable_schema = RESET; + +COMMIT; diff --git a/server/database.js b/server/database.js index cfe14fe71..ed4b9e681 100644 --- a/server/database.js +++ b/server/database.js @@ -105,7 +105,8 @@ class Database { "patch-add-gamedig-given-port.sql": true, "patch-notification-config.sql": true, "patch-fix-kafka-producer-booleans.sql": true, - "patch-timeout.sql": true, // The last file so far converted to a knex migration file + "patch-timeout.sql": true, + "patch-monitor-tls-info-add-fk.sql": true, // The last file so far converted to a knex migration file }; /**