From 68f9d4b832a043bc0d492210e295921ce371fc68 Mon Sep 17 00:00:00 2001 From: Denozordec Date: Wed, 8 Jul 2026 21:16:57 +0700 Subject: [PATCH] refactor(firewall): simplify SQL queries for firewall client retrieval Refactored the SQL queries in the Postgres repository for listing and retrieving firewall clients by introducing a constant for the selected columns. This change improves code readability and maintainability by reducing duplication in the query definitions. No functional changes were made to the data retrieval process. --- internal/repository/postgres_firewall.go | 25 ++++----- internal/repository/postgres_firewall_test.go | 52 +++++++++++++++++++ 2 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 internal/repository/postgres_firewall_test.go diff --git a/internal/repository/postgres_firewall.go b/internal/repository/postgres_firewall.go index 4a3973a..41613ce 100644 --- a/internal/repository/postgres_firewall.go +++ b/internal/repository/postgres_firewall.go @@ -13,14 +13,17 @@ import ( "github.com/jackc/pgx/v5" ) +const firewallClientSelectCols = ` + id, name, COALESCE(hostname, ''), token_prefix, status, + last_seen_at, COALESCE(last_seen_at_source, ''), COALESCE(last_seen_ip, ''), + last_apply_at, COALESCE(last_apply_status, ''), COALESCE(last_apply_error, ''), + COALESCE(last_apply_prefix_count, 0), COALESCE(last_apply_ip_count, 0), COALESCE(last_apply_source, ''), + COALESCE(client_version, ''), created_at, approved_at, approved_by_api_key_id, revoked_at` + func (p *Postgres) ListFirewallClients(tenantID string) ([]*store.FirewallClient, error) { ctx := context.Background() rows, err := p.pool.Query(ctx, ` - SELECT id, name, hostname, token_prefix, status, - last_seen_at, last_seen_at_source, last_seen_ip, - last_apply_at, last_apply_status, last_apply_error, - last_apply_prefix_count, last_apply_ip_count, last_apply_source, - client_version, created_at, approved_at, approved_by_api_key_id, revoked_at + SELECT `+firewallClientSelectCols+` FROM firewall_client WHERE tenant_id=$1 ORDER BY created_at DESC`, tenantID) if err != nil { return nil, err @@ -40,11 +43,7 @@ func (p *Postgres) ListFirewallClients(tenantID string) ([]*store.FirewallClient func (p *Postgres) GetFirewallClient(tenantID, id string) (*store.FirewallClient, error) { ctx := context.Background() row := p.pool.QueryRow(ctx, ` - SELECT id, name, hostname, token_prefix, status, - last_seen_at, last_seen_at_source, last_seen_ip, - last_apply_at, last_apply_status, last_apply_error, - last_apply_prefix_count, last_apply_ip_count, last_apply_source, - client_version, created_at, approved_at, approved_by_api_key_id, revoked_at + SELECT `+firewallClientSelectCols+` FROM firewall_client WHERE id=$1 AND tenant_id=$2`, id, tenantID) c, err := scanFirewallClientRow(row.Scan, tenantID) if err != nil { @@ -147,11 +146,7 @@ func (p *Postgres) LookupFirewallClientByTokenHash(hash []byte) (*store.Firewall } ctx := context.Background() row := p.pool.QueryRow(ctx, ` - SELECT tenant_id, id, name, hostname, token_prefix, status, - last_seen_at, last_seen_at_source, last_seen_ip, - last_apply_at, last_apply_status, last_apply_error, - last_apply_prefix_count, last_apply_ip_count, last_apply_source, - client_version, created_at, approved_at, approved_by_api_key_id, revoked_at + SELECT tenant_id, `+firewallClientSelectCols+` FROM firewall_client WHERE token_hash=$1`, hash) c, err := scanFirewallClientLookupRow(row.Scan) if err != nil { diff --git a/internal/repository/postgres_firewall_test.go b/internal/repository/postgres_firewall_test.go new file mode 100644 index 0000000..3313350 --- /dev/null +++ b/internal/repository/postgres_firewall_test.go @@ -0,0 +1,52 @@ +package repository + +import ( + "context" + "os" + "testing" + + "evobgp/internal/authkey" + "evobgp/internal/db" + "evobgp/internal/store" +) + +func TestPostgresFirewallClientCreateAndGetIntegration(t *testing.T) { + dsn := os.Getenv("EVOBGP_TEST_DATABASE_URL") + if dsn == "" { + t.Skip("EVOBGP_TEST_DATABASE_URL not set") + } + ctx := context.Background() + pool, err := db.OpenPostgresPool(ctx, dsn) + if err != nil { + t.Fatal(err) + } + defer pool.Close() + pg, err := NewPostgres(ctx, pool, true) + if err != nil { + t.Fatal(err) + } + tenant, _, _, _, _ := pg.DemoIDs() + if tenant == "" { + t.Fatal("demo tenant required") + } + tok := "evobgp_fw_pgtest_" + t.Name() + hash := authkey.HashToken(tok) + client, err := pg.CreateFirewallClient(tenant, &store.FirewallClientCreate{ + Name: "pg-firewall-test", + Hostname: "test.local", + TokenPrefix: tok[:12], + TokenHash: hash, + ClientVersion: "test/1", + }) + if err != nil { + t.Fatalf("create: %v", err) + } + got, err := pg.GetFirewallClient(tenant, client.ID) + if err != nil { + t.Fatalf("get: %v", err) + } + if got.Name != "pg-firewall-test" || got.Status != "pending" { + t.Fatalf("got %+v", got) + } + _ = pg.DeleteFirewallClient(tenant, client.ID) +}