Flatten migrations

This commit is contained in:
Daniel 2024-11-21 07:13:18 -05:00
parent 5fd035561c
commit 692f7d69a3
4 changed files with 18 additions and 109 deletions

View file

@ -115,6 +115,7 @@ type TalkgroupVersion struct {
AlertConfig []byte `json:"alert_config"` AlertConfig []byte `json:"alert_config"`
Weight *float32 `json:"weight"` Weight *float32 `json:"weight"`
Learned *bool `json:"learned"` Learned *bool `json:"learned"`
Ignored *bool `json:"ignored"`
} }
type User struct { type User struct {

View file

@ -37,6 +37,7 @@ CREATE TABLE IF NOT EXISTS talkgroups(
alert_config JSONB, alert_config JSONB,
weight REAL NOT NULL DEFAULT 1.0, weight REAL NOT NULL DEFAULT 1.0,
learned BOOLEAN NOT NULL DEFAULT FALSE, learned BOOLEAN NOT NULL DEFAULT FALSE,
ignored BOOLEAN NOT NULL DEFAULT FALSE,
UNIQUE (system_id, tgid) 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 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, id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
system_id INTEGER REFERENCES systems(id) NOT NULL, time TIMESTAMPTZ NOT NULL,
tgid INTEGER NOT NULL, created_by INTEGER REFERENCES users(id),
name TEXT NOT NULL, -- talkgroup snapshot
system_id INT4 REFERENCES systems(id),
tgid INT4,
name TEXT,
alpha_tag TEXT, alpha_tag TEXT,
tg_group TEXT, tg_group TEXT,
ignored BOOLEAN, frequency INTEGER,
UNIQUE (system_id, tgid, name) metadata JSONB,
tags TEXT[],
alert BOOLEAN,
alert_config JSONB,
weight REAL,
learned BOOLEAN,
ignored BOOLEAN
); );
CREATE TABLE IF NOT EXISTS alerts( CREATE TABLE IF NOT EXISTS alerts(

View file

@ -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;

View file

@ -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;