From 20c21beca9e8395f8cdb2b4a1d0fc780d5572665 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Wed, 17 Jul 2024 19:45:46 -0400 Subject: [PATCH] use composite key for talkgroups --- pkg/gordio/database/models.go | 4 +-- pkg/gordio/database/talkgroups.sql.go | 34 ++++++++++++++-------- sql/postgres/migrations/001_initial.up.sql | 6 ++-- sql/postgres/queries/talkgroups.sql | 4 +-- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/pkg/gordio/database/models.go b/pkg/gordio/database/models.go index 4ebd199..e11b14f 100644 --- a/pkg/gordio/database/models.go +++ b/pkg/gordio/database/models.go @@ -63,16 +63,16 @@ type System struct { } type Talkgroup struct { - ID int32 System int32 + Tgid int32 Name pgtype.Text Frequency pgtype.Int4 - Tgid int32 AutoCreated pgtype.Bool Metadata []byte } type TalkgroupsTag struct { + SystemID int32 TalkgroupID int32 Tags []string } diff --git a/pkg/gordio/database/talkgroups.sql.go b/pkg/gordio/database/talkgroups.sql.go index 9efa182..9362bd8 100644 --- a/pkg/gordio/database/talkgroups.sql.go +++ b/pkg/gordio/database/talkgroups.sql.go @@ -22,23 +22,28 @@ func (q *Queries) GetTalkgroupTags(ctx context.Context, talkgroupID int32) ([]st } const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many -SELECT talkgroup_id FROM talkgroups_tags +SELECT system_id, talkgroup_id FROM talkgroups_tags WHERE tags && ARRAY[$1] ` -func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]int32, error) { +type GetTalkgroupsWithAllTagsRow struct { + SystemID int32 + TalkgroupID int32 +} + +func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error) { rows, err := q.db.Query(ctx, getTalkgroupsWithAllTags, tags) if err != nil { return nil, err } defer rows.Close() - var items []int32 + var items []GetTalkgroupsWithAllTagsRow for rows.Next() { - var talkgroup_id int32 - if err := rows.Scan(&talkgroup_id); err != nil { + var i GetTalkgroupsWithAllTagsRow + if err := rows.Scan(&i.SystemID, &i.TalkgroupID); err != nil { return nil, err } - items = append(items, talkgroup_id) + items = append(items, i) } if err := rows.Err(); err != nil { return nil, err @@ -47,23 +52,28 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ( } const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many -SELECT talkgroup_id FROM talkgroups_tags +SELECT system_id, talkgroup_id FROM talkgroups_tags WHERE tags @> ARRAY[$1] ` -func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]int32, error) { +type GetTalkgroupsWithAnyTagsRow struct { + SystemID int32 + TalkgroupID int32 +} + +func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error) { rows, err := q.db.Query(ctx, getTalkgroupsWithAnyTags, tags) if err != nil { return nil, err } defer rows.Close() - var items []int32 + var items []GetTalkgroupsWithAnyTagsRow for rows.Next() { - var talkgroup_id int32 - if err := rows.Scan(&talkgroup_id); err != nil { + var i GetTalkgroupsWithAnyTagsRow + if err := rows.Scan(&i.SystemID, &i.TalkgroupID); err != nil { return nil, err } - items = append(items, talkgroup_id) + items = append(items, i) } if err := rows.Err(); err != nil { return nil, err diff --git a/sql/postgres/migrations/001_initial.up.sql b/sql/postgres/migrations/001_initial.up.sql index 669ba5a..7308cb5 100644 --- a/sql/postgres/migrations/001_initial.up.sql +++ b/sql/postgres/migrations/001_initial.up.sql @@ -26,21 +26,21 @@ CREATE TABLE IF NOT EXISTS systems( ); CREATE TABLE IF NOT EXISTS talkgroups( - id INTEGER NOT NULL, system INTEGER REFERENCES systems(id) NOT NULL, + tgid INTEGER, name TEXT, frequency INTEGER, - tgid INTEGER, auto_created BOOLEAN, metadata JSONB, PRIMARY KEY (tgid, system) ); CREATE TABLE IF NOT EXISTS talkgroups_tags( + system_id INTEGER NOT NULL REFERENCES systems(id), talkgroup_id INTEGER NOT NULL REFERENCES talkgroups(id), tags TEXT[] NOT NULL DEFAULT '{}' ); -CREATE UNIQUE INDEX IF NOT EXISTS talkgroups_tags_id_talkgroup_id ON talkgroups_tags(talkgroup_id); +CREATE UNIQUE INDEX IF NOT EXISTS talkgroups_tags_id_talkgroup_id ON talkgroups_tags(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( diff --git a/sql/postgres/queries/talkgroups.sql b/sql/postgres/queries/talkgroups.sql index 1d57b50..7680bff 100644 --- a/sql/postgres/queries/talkgroups.sql +++ b/sql/postgres/queries/talkgroups.sql @@ -1,9 +1,9 @@ -- name: GetTalkgroupsWithAnyTags :many -SELECT talkgroup_id FROM talkgroups_tags +SELECT system_id, talkgroup_id FROM talkgroups_tags WHERE tags @> ARRAY[$1]; -- name: GetTalkgroupsWithAllTags :many -SELECT talkgroup_id FROM talkgroups_tags +SELECT system_id, talkgroup_id FROM talkgroups_tags WHERE tags && ARRAY[$1]; -- name: GetTalkgroupTags :one