From 930e42b0b03c60629b447718667402329e4d1c9a Mon Sep 17 00:00:00 2001 From: Denozordec Date: Mon, 25 May 2026 11:16:03 +0700 Subject: [PATCH] fix(db): update prefix handling in module_prefix_snapshot_row Modified the prefix column type in the module_prefix_snapshot_row table to TEXT, allowing for more flexible input. Adjusted related SQL queries and Go struct tags to ensure compatibility with JSON serialization. Cleaned up migration logic to handle prefix and community_id fields more robustly. --- internal/repository/module_snapshot.go | 4 +- internal/store/backend.go | 6 +-- .../000019_module_snapshot_rows.up.sql | 43 ++++++++++++++----- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/internal/repository/module_snapshot.go b/internal/repository/module_snapshot.go index 540a04b..6796fa3 100644 --- a/internal/repository/module_snapshot.go +++ b/internal/repository/module_snapshot.go @@ -39,7 +39,7 @@ func (p *Postgres) GetModulePrefixSnapshot(tenantID, moduleID string) (*store.Mo var prefixes []store.PrefixRow if moduleSnapshotRowTableExists(ctx, p.pool) { rows, qerr := p.pool.Query(ctx, ` - SELECT prefix::text, community_id::text, source + SELECT prefix, community_id::text, source FROM module_prefix_snapshot_row WHERE tenant_id = $1::uuid AND module_id = $2::uuid ORDER BY ord`, tenantID, moduleID) @@ -108,7 +108,7 @@ func (p *Postgres) SetModulePrefixSnapshot(tenantID, moduleID, inputHash string, } if _, err := tx.Exec(ctx, ` INSERT INTO module_prefix_snapshot_row (tenant_id, module_id, ord, prefix, community_id, source) - VALUES ($1::uuid, $2::uuid, $3, $4::cidr, $5::uuid, $6)`, + VALUES ($1::uuid, $2::uuid, $3, $4, $5::uuid, $6)`, tenantID, moduleID, i, strings.TrimSpace(pr.Prefix), comm, src); err != nil { return err } diff --git a/internal/store/backend.go b/internal/store/backend.go index 7208093..6e7275b 100644 --- a/internal/store/backend.go +++ b/internal/store/backend.go @@ -314,7 +314,7 @@ type SpeakerPatch struct { // PrefixRow is one materialized prefix for GET /revisions/.../prefixes. type PrefixRow struct { - Prefix string - CommunityID *string - Source string + Prefix string `json:"prefix"` + CommunityID *string `json:"community_id,omitempty"` + Source string `json:"source,omitempty"` } diff --git a/migrations/postgres/000019_module_snapshot_rows.up.sql b/migrations/postgres/000019_module_snapshot_rows.up.sql index 72415f6..041f319 100644 --- a/migrations/postgres/000019_module_snapshot_rows.up.sql +++ b/migrations/postgres/000019_module_snapshot_rows.up.sql @@ -2,7 +2,7 @@ CREATE TABLE module_prefix_snapshot_row ( tenant_id UUID NOT NULL, module_id UUID NOT NULL, ord INTEGER NOT NULL, - prefix CIDR NOT NULL, + prefix TEXT NOT NULL, community_id UUID, source TEXT NOT NULL DEFAULT '', PRIMARY KEY (tenant_id, module_id, ord), @@ -10,16 +10,37 @@ CREATE TABLE module_prefix_snapshot_row ( REFERENCES module_prefix_snapshot (tenant_id, module_id) ON DELETE CASCADE ); +-- prefixes_json from Go json.Marshal(PrefixRow) used "Prefix"/"CommunityID"/"Source" before json tags. INSERT INTO module_prefix_snapshot_row (tenant_id, module_id, ord, prefix, community_id, source) -SELECT mps.tenant_id, - mps.module_id, - (t.ordinality - 1)::int, - (t.elem->>'prefix')::cidr, - NULLIF(t.elem->>'community_id', '')::uuid, - COALESCE(NULLIF(t.elem->>'source', ''), '') -FROM module_prefix_snapshot mps -CROSS JOIN LATERAL jsonb_array_elements(mps.prefixes_json) WITH ORDINALITY AS t(elem, ordinality) -WHERE jsonb_typeof(mps.prefixes_json) = 'array' - AND jsonb_array_length(mps.prefixes_json) > 0; +SELECT tenant_id, + module_id, + (row_number() OVER (PARTITION BY tenant_id, module_id ORDER BY ordinality) - 1)::int, + prefix, + NULLIF(community_id, '')::uuid, + COALESCE(source, '') +FROM ( + SELECT mps.tenant_id, + mps.module_id, + t.ordinality, + COALESCE( + NULLIF(trim(t.elem->>'prefix'), ''), + NULLIF(trim(t.elem->>'Prefix'), '') + ) AS prefix, + COALESCE( + NULLIF(trim(t.elem->>'community_id'), ''), + NULLIF(trim(t.elem->>'CommunityID'), '') + ) AS community_id, + COALESCE( + NULLIF(trim(t.elem->>'source'), ''), + NULLIF(trim(t.elem->>'Source'), ''), + '' + ) AS source + FROM module_prefix_snapshot mps + CROSS JOIN LATERAL jsonb_array_elements(mps.prefixes_json) WITH ORDINALITY AS t(elem, ordinality) + WHERE jsonb_typeof(mps.prefixes_json) = 'array' + AND jsonb_array_length(mps.prefixes_json) > 0 +) parsed +WHERE parsed.prefix IS NOT NULL + AND trim(parsed.prefix) <> ''; ALTER TABLE module_prefix_snapshot DROP COLUMN prefixes_json;