From 692f7d69a381367655131aef9b4d811fc1ffefec Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Thu, 21 Nov 2024 07:13:18 -0500 Subject: [PATCH] Flatten migrations --- pkg/database/models.go | 1 + sql/postgres/migrations/001_initial.up.sql | 23 ++++-- sql/postgres/migrations/002_learned.down.sql | 30 -------- sql/postgres/migrations/002_learned.up.sql | 73 -------------------- 4 files changed, 18 insertions(+), 109 deletions(-) delete mode 100644 sql/postgres/migrations/002_learned.down.sql delete mode 100644 sql/postgres/migrations/002_learned.up.sql diff --git a/pkg/database/models.go b/pkg/database/models.go index 922a9d7..cc46a39 100644 --- a/pkg/database/models.go +++ b/pkg/database/models.go @@ -115,6 +115,7 @@ type TalkgroupVersion struct { AlertConfig []byte `json:"alert_config"` Weight *float32 `json:"weight"` Learned *bool `json:"learned"` + Ignored *bool `json:"ignored"` } type User struct { diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index 59aa9ec..14695d5 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -37,6 +37,7 @@ CREATE TABLE IF NOT EXISTS talkgroups( alert_config JSONB, weight REAL NOT NULL DEFAULT 1.0, learned BOOLEAN NOT NULL DEFAULT FALSE, + ignored BOOLEAN NOT NULL DEFAULT FALSE, UNIQUE (system_id, tgid) ); @@ -44,15 +45,25 @@ CREATE INDEX talkgroups_system_tgid_idx ON talkgroups (system_id, tgid); CREATE INDEX IF NOT EXISTS talkgroup_id_tags ON talkgroups USING GIN (tags); -CREATE TABLE IF NOT EXISTS talkgroups_learned( +CREATE TABLE IF NOT EXISTS talkgroup_versions( + -- version metadata id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - system_id INTEGER REFERENCES systems(id) NOT NULL, - tgid INTEGER NOT NULL, - name TEXT NOT NULL, + time TIMESTAMPTZ NOT NULL, + created_by INTEGER REFERENCES users(id), + -- talkgroup snapshot + system_id INT4 REFERENCES systems(id), + tgid INT4, + name TEXT, alpha_tag TEXT, tg_group TEXT, - ignored BOOLEAN, - UNIQUE (system_id, tgid, name) + frequency INTEGER, + metadata JSONB, + tags TEXT[], + alert BOOLEAN, + alert_config JSONB, + weight REAL, + learned BOOLEAN, + ignored BOOLEAN ); CREATE TABLE IF NOT EXISTS alerts( diff --git a/sql/postgres/migrations/002_learned.down.sql b/sql/postgres/migrations/002_learned.down.sql deleted file mode 100644 index 47523a9..0000000 --- a/sql/postgres/migrations/002_learned.down.sql +++ /dev/null @@ -1,30 +0,0 @@ -CREATE TABLE IF NOT EXISTS talkgroups_learned( - id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - system_id INTEGER REFERENCES systems(id) NOT NULL, - tgid INTEGER NOT NULL, - name TEXT NOT NULL, - alpha_tag TEXT, - tg_group TEXT, - ignored BOOLEAN, - UNIQUE (system_id, tgid, name) -); - -DROP TABLE IF EXISTS talkgroup_versions; - -INSERT INTO talkgroups_learned( - system_id, - tgid, - name, - alpha_tag, - tg_group, - ignored -) SELECT - tg.system_id, - tg.tgid, - tg.name, - tg.alpha_tag, - tg.tg_group - tg.ignored -FROM talkgroups tg; - -ALTER TABLE talkgroups DROP COLUMN ignored; diff --git a/sql/postgres/migrations/002_learned.up.sql b/sql/postgres/migrations/002_learned.up.sql deleted file mode 100644 index 529d6aa..0000000 --- a/sql/postgres/migrations/002_learned.up.sql +++ /dev/null @@ -1,73 +0,0 @@ -ALTER TABLE talkgroups ADD COLUMN ignored BOOLEAN NOT NULL DEFAULT FALSE; - -INSERT INTO talkgroups( - system_id, - tgid, - name, - alpha_tag, - tg_group, - alert, - ignored, - learned -) SELECT - tgl.system_id, - tgl.tgid, - tgl.name, - tgl.alpha_tag, - tgl.tg_group, - TRUE, - tgl.ignored, - TRUE -FROM talkgroups_learned tgl ON CONFLICT DO NOTHING; - -CREATE TABLE IF NOT EXISTS talkgroup_versions( - -- version metadata - id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY, - time TIMESTAMPTZ NOT NULL, - created_by INTEGER REFERENCES users(id), - -- talkgroup snapshot - system_id INT4 REFERENCES systems(id), - tgid INT4, - name TEXT, - alpha_tag TEXT, - tg_group TEXT, - frequency INTEGER, - metadata JSONB, - tags TEXT[], - alert BOOLEAN, - alert_config JSONB, - weight REAL, - learned BOOLEAN, - ignored BOOLEAN -); - --- Store current version -INSERT INTO talkgroup_versions(time, - system_id, - tgid, - name, - alpha_tag, - tg_group, - frequency, - metadata, - tags, - alert, - alert_config, - weight, - learned -) SELECT NOW(), - tg.system_id, - tg.tgid, - tg.name, - tg.alpha_tag, - tg.tg_group, - tg.frequency, - tg.metadata, - tg.tags, - tg.alert, - tg.alert_config, - tg.weight, - tg.learned -FROM talkgroups tg; - -DROP TABLE IF EXISTS talkgroups_learned;