From 635b05361f54189800f51ec8c841241121c0fe68 Mon Sep 17 00:00:00 2001 From: Vassiliy Yegorov Date: Fri, 3 Jul 2026 14:20:03 +0700 Subject: [PATCH] =?UTF-8?q?refactor(store):=20sqlc=20override=20uuid?= =?UTF-8?q?=E2=86=92google/uuid.UUID=20(=D1=83=D0=B1=D0=B8=D1=80=D0=B0?= =?UTF-8?q?=D0=B5=D1=82=20pgtype=20boilerplate)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- internal/store/db/accounts.sql.go | 22 +++++++++++----------- internal/store/db/check_runs.sql.go | 8 ++++---- internal/store/db/domains.sql.go | 24 ++++++++++++------------ internal/store/db/models.go | 27 ++++++++++++++------------- internal/store/db/templates.sql.go | 20 ++++++++++---------- internal/store/store_test.go | 14 +++----------- sqlc.yaml | 10 ++++++++++ 8 files changed, 65 insertions(+), 62 deletions(-) diff --git a/go.mod b/go.mod index ab1c0e0..ac55f81 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/vasyakrg/dns-autoresolver go 1.26.4 require ( + github.com/google/uuid v1.6.0 github.com/jackc/pgx/v5 v5.10.0 github.com/pressly/goose/v3 v3.27.2 github.com/testcontainers/testcontainers-go v0.43.0 @@ -29,7 +30,6 @@ require ( github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.2.6 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect diff --git a/internal/store/db/accounts.sql.go b/internal/store/db/accounts.sql.go index c8e7aa3..3afbeef 100644 --- a/internal/store/db/accounts.sql.go +++ b/internal/store/db/accounts.sql.go @@ -8,7 +8,7 @@ package db import ( "context" - "github.com/jackc/pgx/v5/pgtype" + "github.com/google/uuid" ) const createAccount = `-- name: CreateAccount :one @@ -18,11 +18,11 @@ RETURNING id, project_id, provider, secret_enc, comment, created_at ` type CreateAccountParams struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` - Provider string `json:"provider"` - SecretEnc string `json:"secret_enc"` - Comment string `json:"comment"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` + Provider string `json:"provider"` + SecretEnc string `json:"secret_enc"` + Comment string `json:"comment"` } func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (ProviderAccount, error) { @@ -50,8 +50,8 @@ DELETE FROM provider_accounts WHERE id = $1 AND project_id = $2 ` type DeleteAccountParams struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` } func (q *Queries) DeleteAccount(ctx context.Context, arg DeleteAccountParams) error { @@ -64,8 +64,8 @@ SELECT id, project_id, provider, secret_enc, comment, created_at FROM provider_a ` type GetAccountParams struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` } func (q *Queries) GetAccount(ctx context.Context, arg GetAccountParams) (ProviderAccount, error) { @@ -86,7 +86,7 @@ const listAccounts = `-- name: ListAccounts :many SELECT id, project_id, provider, secret_enc, comment, created_at FROM provider_accounts WHERE project_id = $1 ORDER BY created_at ` -func (q *Queries) ListAccounts(ctx context.Context, projectID pgtype.UUID) ([]ProviderAccount, error) { +func (q *Queries) ListAccounts(ctx context.Context, projectID uuid.UUID) ([]ProviderAccount, error) { rows, err := q.db.Query(ctx, listAccounts, projectID) if err != nil { return nil, err diff --git a/internal/store/db/check_runs.sql.go b/internal/store/db/check_runs.sql.go index 14a6ab5..3279246 100644 --- a/internal/store/db/check_runs.sql.go +++ b/internal/store/db/check_runs.sql.go @@ -8,7 +8,7 @@ package db import ( "context" - "github.com/jackc/pgx/v5/pgtype" + "github.com/google/uuid" ) const createCheckRun = `-- name: CreateCheckRun :one @@ -18,9 +18,9 @@ RETURNING id, domain_id, result, created_at ` type CreateCheckRunParams struct { - ID pgtype.UUID `json:"id"` - DomainID pgtype.UUID `json:"domain_id"` - Result []byte `json:"result"` + ID uuid.UUID `json:"id"` + DomainID uuid.UUID `json:"domain_id"` + Result []byte `json:"result"` } func (q *Queries) CreateCheckRun(ctx context.Context, arg CreateCheckRunParams) (CheckRun, error) { diff --git a/internal/store/db/domains.sql.go b/internal/store/db/domains.sql.go index 92eb755..0257e6e 100644 --- a/internal/store/db/domains.sql.go +++ b/internal/store/db/domains.sql.go @@ -8,7 +8,7 @@ package db import ( "context" - "github.com/jackc/pgx/v5/pgtype" + "github.com/google/uuid" ) const createDomain = `-- name: CreateDomain :one @@ -18,12 +18,12 @@ RETURNING id, project_id, provider_account_id, zone_name, zone_id, template_id, ` type CreateDomainParams struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` - ProviderAccountID pgtype.UUID `json:"provider_account_id"` - ZoneName string `json:"zone_name"` - ZoneID string `json:"zone_id"` - TemplateID pgtype.UUID `json:"template_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` + ProviderAccountID uuid.UUID `json:"provider_account_id"` + ZoneName string `json:"zone_name"` + ZoneID string `json:"zone_id"` + TemplateID *uuid.UUID `json:"template_id"` } func (q *Queries) CreateDomain(ctx context.Context, arg CreateDomainParams) (Domain, error) { @@ -53,8 +53,8 @@ DELETE FROM domains WHERE id = $1 AND project_id = $2 ` type DeleteDomainParams struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` } func (q *Queries) DeleteDomain(ctx context.Context, arg DeleteDomainParams) error { @@ -67,8 +67,8 @@ SELECT id, project_id, provider_account_id, zone_name, zone_id, template_id, cre ` type GetDomainParams struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` } func (q *Queries) GetDomain(ctx context.Context, arg GetDomainParams) (Domain, error) { @@ -90,7 +90,7 @@ const listDomains = `-- name: ListDomains :many SELECT id, project_id, provider_account_id, zone_name, zone_id, template_id, created_at FROM domains WHERE project_id = $1 ORDER BY created_at ` -func (q *Queries) ListDomains(ctx context.Context, projectID pgtype.UUID) ([]Domain, error) { +func (q *Queries) ListDomains(ctx context.Context, projectID uuid.UUID) ([]Domain, error) { rows, err := q.db.Query(ctx, listDomains, projectID) if err != nil { return nil, err diff --git a/internal/store/db/models.go b/internal/store/db/models.go index 8fff901..d3b98ab 100644 --- a/internal/store/db/models.go +++ b/internal/store/db/models.go @@ -5,37 +5,38 @@ package db import ( + "github.com/google/uuid" "github.com/jackc/pgx/v5/pgtype" dto "github.com/vasyakrg/dns-autoresolver/internal/store/dto" ) type CheckRun struct { - ID pgtype.UUID `json:"id"` - DomainID pgtype.UUID `json:"domain_id"` + ID uuid.UUID `json:"id"` + DomainID uuid.UUID `json:"domain_id"` Result []byte `json:"result"` CreatedAt pgtype.Timestamptz `json:"created_at"` } type Domain struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` - ProviderAccountID pgtype.UUID `json:"provider_account_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` + ProviderAccountID uuid.UUID `json:"provider_account_id"` ZoneName string `json:"zone_name"` ZoneID string `json:"zone_id"` - TemplateID pgtype.UUID `json:"template_id"` + TemplateID *uuid.UUID `json:"template_id"` CreatedAt pgtype.Timestamptz `json:"created_at"` } type Project struct { - ID pgtype.UUID `json:"id"` - UserID pgtype.UUID `json:"user_id"` + ID uuid.UUID `json:"id"` + UserID uuid.UUID `json:"user_id"` Name string `json:"name"` CreatedAt pgtype.Timestamptz `json:"created_at"` } type ProviderAccount struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` Provider string `json:"provider"` SecretEnc string `json:"secret_enc"` Comment string `json:"comment"` @@ -43,8 +44,8 @@ type ProviderAccount struct { } type Template struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` Name string `json:"name"` Doc *dto.TemplateDoc `json:"doc"` Version int32 `json:"version"` @@ -53,7 +54,7 @@ type Template struct { } type User struct { - ID pgtype.UUID `json:"id"` + ID uuid.UUID `json:"id"` Email string `json:"email"` CreatedAt pgtype.Timestamptz `json:"created_at"` } diff --git a/internal/store/db/templates.sql.go b/internal/store/db/templates.sql.go index cd8f95d..0977908 100644 --- a/internal/store/db/templates.sql.go +++ b/internal/store/db/templates.sql.go @@ -8,7 +8,7 @@ package db import ( "context" - "github.com/jackc/pgx/v5/pgtype" + "github.com/google/uuid" dto "github.com/vasyakrg/dns-autoresolver/internal/store/dto" ) @@ -19,8 +19,8 @@ RETURNING id, project_id, name, doc, version, created_at, updated_at ` type CreateTemplateParams struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` Name string `json:"name"` Doc *dto.TemplateDoc `json:"doc"` } @@ -50,8 +50,8 @@ DELETE FROM templates WHERE id = $1 AND project_id = $2 ` type DeleteTemplateParams struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` } func (q *Queries) DeleteTemplate(ctx context.Context, arg DeleteTemplateParams) error { @@ -64,8 +64,8 @@ SELECT id, project_id, name, doc, version, created_at, updated_at FROM templates ` type GetTemplateParams struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` } func (q *Queries) GetTemplate(ctx context.Context, arg GetTemplateParams) (Template, error) { @@ -87,7 +87,7 @@ const listTemplates = `-- name: ListTemplates :many SELECT id, project_id, name, doc, version, created_at, updated_at FROM templates WHERE project_id = $1 ORDER BY created_at ` -func (q *Queries) ListTemplates(ctx context.Context, projectID pgtype.UUID) ([]Template, error) { +func (q *Queries) ListTemplates(ctx context.Context, projectID uuid.UUID) ([]Template, error) { rows, err := q.db.Query(ctx, listTemplates, projectID) if err != nil { return nil, err @@ -123,8 +123,8 @@ RETURNING id, project_id, name, doc, version, created_at, updated_at ` type UpdateTemplateParams struct { - ID pgtype.UUID `json:"id"` - ProjectID pgtype.UUID `json:"project_id"` + ID uuid.UUID `json:"id"` + ProjectID uuid.UUID `json:"project_id"` Name string `json:"name"` Doc *dto.TemplateDoc `json:"doc"` } diff --git a/internal/store/store_test.go b/internal/store/store_test.go index 1e51df9..fb89309 100644 --- a/internal/store/store_test.go +++ b/internal/store/store_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/google/uuid" - "github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgxpool" "github.com/vasyakrg/dns-autoresolver/internal/store/db" @@ -13,14 +12,7 @@ import ( ) // defaultProject is the seed default tenant project (see migrations/0001_init.sql). -// -// NOTE: sqlc generated UUID columns as pgtype.UUID (not google/uuid.UUID) — -// pgUUID bridges the two so tests can still use uuid.New()/uuid.MustParse. -var defaultProject = pgUUID(uuid.MustParse("00000000-0000-0000-0000-000000000002")) - -func pgUUID(id uuid.UUID) pgtype.UUID { - return pgtype.UUID{Bytes: id, Valid: true} -} +var defaultProject = uuid.MustParse("00000000-0000-0000-0000-000000000002") func newStore(t *testing.T) (*Store, context.Context) { dsn := startPostgres(t) @@ -35,7 +27,7 @@ func newStore(t *testing.T) (*Store, context.Context) { func TestAccountCRUD(t *testing.T) { s, ctx := newStore(t) acc, err := s.Queries().CreateAccount(ctx, db.CreateAccountParams{ - ID: pgUUID(uuid.New()), ProjectID: defaultProject, + ID: uuid.New(), ProjectID: defaultProject, Provider: "selectel", SecretEnc: "enc-blob", Comment: "prod", }) if err != nil { @@ -66,7 +58,7 @@ func TestTemplateJSONBRoundTrip(t *testing.T) { {Type: "SRV", Name: "_autodiscover._tcp.example.com.", TTL: 3600, Values: []string{"0 0 443 mail.example.com."}}, }} tpl, err := s.Queries().CreateTemplate(ctx, db.CreateTemplateParams{ - ID: pgUUID(uuid.New()), ProjectID: defaultProject, Name: "base", Doc: &doc, + ID: uuid.New(), ProjectID: defaultProject, Name: "base", Doc: &doc, }) if err != nil { t.Fatal(err) diff --git a/sqlc.yaml b/sqlc.yaml index 068a34b..b10d882 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -17,3 +17,13 @@ sql: package: dto type: TemplateDoc pointer: true + - db_type: "uuid" + go_type: + import: "github.com/google/uuid" + type: "UUID" + - db_type: "uuid" + nullable: true + go_type: + import: "github.com/google/uuid" + type: "UUID" + pointer: true