From f05a717e2f727a123ac17996399f9ffdb7059d17 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Sun, 28 Jul 2024 23:07:04 -0400 Subject: [PATCH] Move tg tags into the tg table --- .gitignore | 1 + pkg/gordio/database/models.go | 19 +++---- pkg/gordio/database/querier.go | 8 +-- pkg/gordio/database/talkgroups.sql.go | 62 ++++++++++++---------- sql/postgres/migrations/001_initial.up.sql | 12 ++--- sql/postgres/queries/talkgroups.sql | 13 +++-- 6 files changed, 55 insertions(+), 60 deletions(-) diff --git a/.gitignore b/.gitignore index e133da3..8401db6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ config.yaml +mydb.sql /gordio diff --git a/pkg/gordio/database/models.go b/pkg/gordio/database/models.go index afcfc98..bf210e6 100644 --- a/pkg/gordio/database/models.go +++ b/pkg/gordio/database/models.go @@ -68,12 +68,13 @@ type System struct { } type Talkgroup struct { - SystemID int `json:"system_id"` - Tgid int `json:"tgid"` - Name *string `json:"name"` - TgGroup *string `json:"tg_group"` - Frequency *int32 `json:"frequency"` - Metadata []byte `json:"metadata"` + SystemID int `json:"system_id"` + Tgid int `json:"tgid"` + Name *string `json:"name"` + TgGroup *string `json:"tg_group"` + Frequency *int32 `json:"frequency"` + Metadata []byte `json:"metadata"` + Tags []string `json:"tags"` } type TalkgroupsLearned struct { @@ -85,12 +86,6 @@ type TalkgroupsLearned struct { Ignored *bool `json:"ignored"` } -type TalkgroupsTag struct { - SystemID int `json:"system_id"` - TalkgroupID int `json:"talkgroup_id"` - Tags []string `json:"tags"` -} - type User struct { ID int32 `json:"id"` Username string `json:"username"` diff --git a/pkg/gordio/database/querier.go b/pkg/gordio/database/querier.go index 3070403..a15d01d 100644 --- a/pkg/gordio/database/querier.go +++ b/pkg/gordio/database/querier.go @@ -18,15 +18,15 @@ type Querier interface { DeleteAPIKey(ctx context.Context, apiKey uuid.UUID) error DeleteUser(ctx context.Context, username string) error GetAPIKey(ctx context.Context, apiKey uuid.UUID) (ApiKey, error) - GetTalkgroupTags(ctx context.Context, talkgroupID int) ([]string, error) - GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error) - GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error) + GetTalkgroupTags(ctx context.Context, systemID int, tgid int) ([]string, error) + GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]Talkgroup, error) + GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]Talkgroup, error) GetUserByID(ctx context.Context, id int32) (User, error) GetUserByUID(ctx context.Context, id int32) (User, error) GetUserByUsername(ctx context.Context, username string) (User, error) GetUsers(ctx context.Context) ([]User, error) SetCallTranscript(ctx context.Context, iD uuid.UUID, transcript *string) error - SetTalkgroupTags(ctx context.Context, systemID int, talkgroupID int, tags []string) error + SetTalkgroupTags(ctx context.Context, tags []string, tgid int) error UpdatePassword(ctx context.Context, username string, password string) error } diff --git a/pkg/gordio/database/talkgroups.sql.go b/pkg/gordio/database/talkgroups.sql.go index 740aa11..398d77b 100644 --- a/pkg/gordio/database/talkgroups.sql.go +++ b/pkg/gordio/database/talkgroups.sql.go @@ -10,37 +10,40 @@ import ( ) const getTalkgroupTags = `-- name: GetTalkgroupTags :one -SELECT tags FROM talkgroups_tags -WHERE talkgroup_id = $1 +SELECT tags FROM talkgroups +WHERE system_id = $1 AND tgid = $2 ` -func (q *Queries) GetTalkgroupTags(ctx context.Context, talkgroupID int) ([]string, error) { - row := q.db.QueryRow(ctx, getTalkgroupTags, talkgroupID) +func (q *Queries) GetTalkgroupTags(ctx context.Context, systemID int, tgid int) ([]string, error) { + row := q.db.QueryRow(ctx, getTalkgroupTags, systemID, tgid) var tags []string err := row.Scan(&tags) return tags, err } const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many -SELECT system_id, talkgroup_id FROM talkgroups_tags +SELECT system_id, tgid, name, tg_group, frequency, metadata, tags FROM talkgroups WHERE tags && ARRAY[$1] ` -type GetTalkgroupsWithAllTagsRow struct { - SystemID int `json:"system_id"` - TalkgroupID int `json:"talkgroup_id"` -} - -func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error) { +func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]Talkgroup, error) { rows, err := q.db.Query(ctx, getTalkgroupsWithAllTags, tags) if err != nil { return nil, err } defer rows.Close() - var items []GetTalkgroupsWithAllTagsRow + var items []Talkgroup for rows.Next() { - var i GetTalkgroupsWithAllTagsRow - if err := rows.Scan(&i.SystemID, &i.TalkgroupID); err != nil { + var i Talkgroup + if err := rows.Scan( + &i.SystemID, + &i.Tgid, + &i.Name, + &i.TgGroup, + &i.Frequency, + &i.Metadata, + &i.Tags, + ); err != nil { return nil, err } items = append(items, i) @@ -52,25 +55,28 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ( } const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many -SELECT system_id, talkgroup_id FROM talkgroups_tags +SELECT system_id, tgid, name, tg_group, frequency, metadata, tags FROM talkgroups WHERE tags @> ARRAY[$1] ` -type GetTalkgroupsWithAnyTagsRow struct { - SystemID int `json:"system_id"` - TalkgroupID int `json:"talkgroup_id"` -} - -func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error) { +func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]Talkgroup, error) { rows, err := q.db.Query(ctx, getTalkgroupsWithAnyTags, tags) if err != nil { return nil, err } defer rows.Close() - var items []GetTalkgroupsWithAnyTagsRow + var items []Talkgroup for rows.Next() { - var i GetTalkgroupsWithAnyTagsRow - if err := rows.Scan(&i.SystemID, &i.TalkgroupID); err != nil { + var i Talkgroup + if err := rows.Scan( + &i.SystemID, + &i.Tgid, + &i.Name, + &i.TgGroup, + &i.Frequency, + &i.Metadata, + &i.Tags, + ); err != nil { return nil, err } items = append(items, i) @@ -82,11 +88,11 @@ func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ( } const setTalkgroupTags = `-- name: SetTalkgroupTags :exec -INSERT INTO talkgroups_tags(system_id, talkgroup_id, tags) VALUES($1, $2, $3) -ON CONFLICT (system_id, talkgroup_id) DO UPDATE SET tags = $3 +UPDATE talkgroups SET tags = $1 +WHERE system_id = $1 AND tgid = $2 ` -func (q *Queries) SetTalkgroupTags(ctx context.Context, systemID int, talkgroupID int, tags []string) error { - _, err := q.db.Exec(ctx, setTalkgroupTags, systemID, talkgroupID, tags) +func (q *Queries) SetTalkgroupTags(ctx context.Context, tags []string, tgid int) error { + _, err := q.db.Exec(ctx, setTalkgroupTags, tags, tgid) return err } diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index b613c04..0c62020 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -30,9 +30,12 @@ CREATE TABLE IF NOT EXISTS talkgroups( tg_group TEXT, frequency INTEGER, metadata JSONB, + tags TEXT[] NOT NULL DEFAULT '{}', PRIMARY KEY (system_id, tgid) ); +CREATE INDEX IF NOT EXISTS talkgroup_id_tags ON talkgroups USING GIN (tags); + CREATE TABLE IF NOT EXISTS talkgroups_learned( id SERIAL PRIMARY KEY, system_id INTEGER REFERENCES systems(id) NOT NULL, @@ -59,15 +62,6 @@ BEGIN END $$ LANGUAGE plpgsql; -CREATE TABLE IF NOT EXISTS talkgroups_tags( - system_id INTEGER NOT NULL, - talkgroup_id INTEGER NOT NULL, - tags TEXT[] NOT NULL DEFAULT '{}', - FOREIGN KEY (system_id, talkgroup_id) REFERENCES talkgroups (system_id, tgid), - PRIMARY KEY (system_id, talkgroup_id) -); -CREATE INDEX IF NOT EXISTS talkgroup_tags_id_tags ON talkgroups_tags USING GIN (tags); - CREATE TABLE IF NOT EXISTS calls( id UUID PRIMARY KEY, submitter INTEGER REFERENCES api_keys(id) ON DELETE SET NULL, diff --git a/sql/postgres/queries/talkgroups.sql b/sql/postgres/queries/talkgroups.sql index 2af1f1e..fa224a3 100644 --- a/sql/postgres/queries/talkgroups.sql +++ b/sql/postgres/queries/talkgroups.sql @@ -1,16 +1,15 @@ -- name: GetTalkgroupsWithAnyTags :many -SELECT system_id, talkgroup_id FROM talkgroups_tags +SELECT * FROM talkgroups WHERE tags @> ARRAY[$1]; -- name: GetTalkgroupsWithAllTags :many -SELECT system_id, talkgroup_id FROM talkgroups_tags +SELECT * FROM talkgroups WHERE tags && ARRAY[$1]; -- name: GetTalkgroupTags :one -SELECT tags FROM talkgroups_tags -WHERE talkgroup_id = $1; +SELECT tags FROM talkgroups +WHERE system_id = $1 AND tgid = $2; -- name: SetTalkgroupTags :exec -INSERT INTO talkgroups_tags(system_id, talkgroup_id, tags) VALUES($1, $2, $3) -ON CONFLICT (system_id, talkgroup_id) DO UPDATE SET tags = $3; - +UPDATE talkgroups SET tags = $1 +WHERE system_id = $1 AND tgid = $2;