From ec0ecd321bbc807cbed2dcbc82b7cef891c3ca8e Mon Sep 17 00:00:00 2001 From: MrAlders0n Date: Fri, 2 Oct 2026 16:49:09 -0400 Subject: [PATCH] refactor(db): move the heard-IATA lookup into sqlc --- db/meshmapper.go | 6 +----- db/queries/queries.sql | 3 +++ db/sqlc/mock/querier.go | 15 +++++++++++++++ db/sqlc/querier.go | 1 + db/sqlc/queries.sql.go | 24 ++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/db/meshmapper.go b/db/meshmapper.go index 82e2d59..fa2120d 100644 --- a/db/meshmapper.go +++ b/db/meshmapper.go @@ -79,11 +79,7 @@ func (s *Store) ListKnownIATAs(ctx context.Context) ([]string, error) { // ListHeardIATAs returns the IATAs observers last reported from. func (s *Store) ListHeardIATAs(ctx context.Context) ([]string, error) { - rows, err := s.pool.Query(ctx, `SELECT DISTINCT last_iata::text FROM observers WHERE last_iata IS NOT NULL ORDER BY 1`) - if err != nil { - return nil, err - } - return pgx.CollectRows(rows, pgx.RowTo[string]) + return s.q.ListHeardIATAs(ctx) } func (s *Store) PruneZoneBoundaries(ctx context.Context, keep []string) ([]string, error) { diff --git a/db/queries/queries.sql b/db/queries/queries.sql index 8ca8b3d..ecdcffe 100644 --- a/db/queries/queries.sql +++ b/db/queries/queries.sql @@ -21,6 +21,9 @@ SELECT * FROM iata_codes WHERE iata = $1; -- name: ListIATAs :many SELECT * FROM iata_codes ORDER BY iata; +-- name: ListHeardIATAs :many +SELECT DISTINCT last_iata::text AS iata FROM observers WHERE last_iata IS NOT NULL ORDER BY 1; + -- name: UpsertIATADetails :exec INSERT INTO iata_codes (iata, display_name, approx_lat, approx_lng) VALUES ($1, $2, $3, $4) diff --git a/db/sqlc/mock/querier.go b/db/sqlc/mock/querier.go index e61bc78..71191e6 100644 --- a/db/sqlc/mock/querier.go +++ b/db/sqlc/mock/querier.go @@ -1316,6 +1316,21 @@ func (mr *MockQuerierMockRecorder) ListDirtyRollupHours(ctx, limit any) *gomock. return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListDirtyRollupHours", reflect.TypeOf((*MockQuerier)(nil).ListDirtyRollupHours), ctx, limit) } +// ListHeardIATAs mocks base method. +func (m *MockQuerier) ListHeardIATAs(ctx context.Context) ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListHeardIATAs", ctx) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListHeardIATAs indicates an expected call of ListHeardIATAs. +func (mr *MockQuerierMockRecorder) ListHeardIATAs(ctx any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListHeardIATAs", reflect.TypeOf((*MockQuerier)(nil).ListHeardIATAs), ctx) +} + // ListIATAs mocks base method. func (m *MockQuerier) ListIATAs(ctx context.Context) ([]db.IataCode, error) { m.ctrl.T.Helper() diff --git a/db/sqlc/querier.go b/db/sqlc/querier.go index 8731ece..4f56b0f 100644 --- a/db/sqlc/querier.go +++ b/db/sqlc/querier.go @@ -200,6 +200,7 @@ type Querier interface { // Only complete hours whose raw rows are still intact can be re-rolled. Queued missing hours // wait: their first roll consumes the entry only if the entry predates its snapshot. ListDirtyRollupHours(ctx context.Context, limit int32) ([]pgtype.Timestamptz, error) + ListHeardIATAs(ctx context.Context) ([]string, error) ListIATAs(ctx context.Context) ([]IataCode, error) // Only one branch runs. Keep the IATA range ordered by the composite index: // generic plans can otherwise prefer scanning the global timestamp index. diff --git a/db/sqlc/queries.sql.go b/db/sqlc/queries.sql.go index dfa697d..435bb94 100644 --- a/db/sqlc/queries.sql.go +++ b/db/sqlc/queries.sql.go @@ -2912,6 +2912,30 @@ func (q *Queries) ListChannelsAfter(ctx context.Context, arg ListChannelsAfterPa return items, nil } +const listHeardIATAs = `-- name: ListHeardIATAs :many +SELECT DISTINCT last_iata::text AS iata FROM observers WHERE last_iata IS NOT NULL ORDER BY 1 +` + +func (q *Queries) ListHeardIATAs(ctx context.Context) ([]string, error) { + rows, err := q.db.Query(ctx, listHeardIATAs) + if err != nil { + return nil, err + } + defer rows.Close() + items := []string{} + for rows.Next() { + var iata string + if err := rows.Scan(&iata); err != nil { + return nil, err + } + items = append(items, iata) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const listIATAs = `-- name: ListIATAs :many SELECT iata, display_name, approx_lat, approx_lng, added_at, border FROM iata_codes ORDER BY iata `