refactor(store): sqlc override uuid→google/uuid.UUID (убирает pgtype boilerplate)

This commit is contained in:
2026-07-03 14:20:03 +07:00
parent 34bc49ee8c
commit 635b05361f
8 changed files with 65 additions and 62 deletions
+1 -1
View File
@@ -3,6 +3,7 @@ module github.com/vasyakrg/dns-autoresolver
go 1.26.4 go 1.26.4
require ( require (
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.10.0 github.com/jackc/pgx/v5 v5.10.0
github.com/pressly/goose/v3 v3.27.2 github.com/pressly/goose/v3 v3.27.2
github.com/testcontainers/testcontainers-go v0.43.0 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/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // 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/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect
+11 -11
View File
@@ -8,7 +8,7 @@ package db
import ( import (
"context" "context"
"github.com/jackc/pgx/v5/pgtype" "github.com/google/uuid"
) )
const createAccount = `-- name: CreateAccount :one const createAccount = `-- name: CreateAccount :one
@@ -18,11 +18,11 @@ RETURNING id, project_id, provider, secret_enc, comment, created_at
` `
type CreateAccountParams struct { type CreateAccountParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
Provider string `json:"provider"` Provider string `json:"provider"`
SecretEnc string `json:"secret_enc"` SecretEnc string `json:"secret_enc"`
Comment string `json:"comment"` Comment string `json:"comment"`
} }
func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (ProviderAccount, error) { 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 { type DeleteAccountParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
} }
func (q *Queries) DeleteAccount(ctx context.Context, arg DeleteAccountParams) error { 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 { type GetAccountParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
} }
func (q *Queries) GetAccount(ctx context.Context, arg GetAccountParams) (ProviderAccount, error) { 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 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) rows, err := q.db.Query(ctx, listAccounts, projectID)
if err != nil { if err != nil {
return nil, err return nil, err
+4 -4
View File
@@ -8,7 +8,7 @@ package db
import ( import (
"context" "context"
"github.com/jackc/pgx/v5/pgtype" "github.com/google/uuid"
) )
const createCheckRun = `-- name: CreateCheckRun :one const createCheckRun = `-- name: CreateCheckRun :one
@@ -18,9 +18,9 @@ RETURNING id, domain_id, result, created_at
` `
type CreateCheckRunParams struct { type CreateCheckRunParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
DomainID pgtype.UUID `json:"domain_id"` DomainID uuid.UUID `json:"domain_id"`
Result []byte `json:"result"` Result []byte `json:"result"`
} }
func (q *Queries) CreateCheckRun(ctx context.Context, arg CreateCheckRunParams) (CheckRun, error) { func (q *Queries) CreateCheckRun(ctx context.Context, arg CreateCheckRunParams) (CheckRun, error) {
+12 -12
View File
@@ -8,7 +8,7 @@ package db
import ( import (
"context" "context"
"github.com/jackc/pgx/v5/pgtype" "github.com/google/uuid"
) )
const createDomain = `-- name: CreateDomain :one 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 { type CreateDomainParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
ProviderAccountID pgtype.UUID `json:"provider_account_id"` ProviderAccountID uuid.UUID `json:"provider_account_id"`
ZoneName string `json:"zone_name"` ZoneName string `json:"zone_name"`
ZoneID string `json:"zone_id"` ZoneID string `json:"zone_id"`
TemplateID pgtype.UUID `json:"template_id"` TemplateID *uuid.UUID `json:"template_id"`
} }
func (q *Queries) CreateDomain(ctx context.Context, arg CreateDomainParams) (Domain, error) { 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 { type DeleteDomainParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
} }
func (q *Queries) DeleteDomain(ctx context.Context, arg DeleteDomainParams) error { 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 { type GetDomainParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
} }
func (q *Queries) GetDomain(ctx context.Context, arg GetDomainParams) (Domain, error) { 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 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) rows, err := q.db.Query(ctx, listDomains, projectID)
if err != nil { if err != nil {
return nil, err return nil, err
+14 -13
View File
@@ -5,37 +5,38 @@
package db package db
import ( import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgtype"
dto "github.com/vasyakrg/dns-autoresolver/internal/store/dto" dto "github.com/vasyakrg/dns-autoresolver/internal/store/dto"
) )
type CheckRun struct { type CheckRun struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
DomainID pgtype.UUID `json:"domain_id"` DomainID uuid.UUID `json:"domain_id"`
Result []byte `json:"result"` Result []byte `json:"result"`
CreatedAt pgtype.Timestamptz `json:"created_at"` CreatedAt pgtype.Timestamptz `json:"created_at"`
} }
type Domain struct { type Domain struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
ProviderAccountID pgtype.UUID `json:"provider_account_id"` ProviderAccountID uuid.UUID `json:"provider_account_id"`
ZoneName string `json:"zone_name"` ZoneName string `json:"zone_name"`
ZoneID string `json:"zone_id"` ZoneID string `json:"zone_id"`
TemplateID pgtype.UUID `json:"template_id"` TemplateID *uuid.UUID `json:"template_id"`
CreatedAt pgtype.Timestamptz `json:"created_at"` CreatedAt pgtype.Timestamptz `json:"created_at"`
} }
type Project struct { type Project struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
UserID pgtype.UUID `json:"user_id"` UserID uuid.UUID `json:"user_id"`
Name string `json:"name"` Name string `json:"name"`
CreatedAt pgtype.Timestamptz `json:"created_at"` CreatedAt pgtype.Timestamptz `json:"created_at"`
} }
type ProviderAccount struct { type ProviderAccount struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
Provider string `json:"provider"` Provider string `json:"provider"`
SecretEnc string `json:"secret_enc"` SecretEnc string `json:"secret_enc"`
Comment string `json:"comment"` Comment string `json:"comment"`
@@ -43,8 +44,8 @@ type ProviderAccount struct {
} }
type Template struct { type Template struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
Name string `json:"name"` Name string `json:"name"`
Doc *dto.TemplateDoc `json:"doc"` Doc *dto.TemplateDoc `json:"doc"`
Version int32 `json:"version"` Version int32 `json:"version"`
@@ -53,7 +54,7 @@ type Template struct {
} }
type User struct { type User struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
Email string `json:"email"` Email string `json:"email"`
CreatedAt pgtype.Timestamptz `json:"created_at"` CreatedAt pgtype.Timestamptz `json:"created_at"`
} }
+10 -10
View File
@@ -8,7 +8,7 @@ package db
import ( import (
"context" "context"
"github.com/jackc/pgx/v5/pgtype" "github.com/google/uuid"
dto "github.com/vasyakrg/dns-autoresolver/internal/store/dto" 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 { type CreateTemplateParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
Name string `json:"name"` Name string `json:"name"`
Doc *dto.TemplateDoc `json:"doc"` Doc *dto.TemplateDoc `json:"doc"`
} }
@@ -50,8 +50,8 @@ DELETE FROM templates WHERE id = $1 AND project_id = $2
` `
type DeleteTemplateParams struct { type DeleteTemplateParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
} }
func (q *Queries) DeleteTemplate(ctx context.Context, arg DeleteTemplateParams) error { 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 { type GetTemplateParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
} }
func (q *Queries) GetTemplate(ctx context.Context, arg GetTemplateParams) (Template, error) { 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 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) rows, err := q.db.Query(ctx, listTemplates, projectID)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -123,8 +123,8 @@ RETURNING id, project_id, name, doc, version, created_at, updated_at
` `
type UpdateTemplateParams struct { type UpdateTemplateParams struct {
ID pgtype.UUID `json:"id"` ID uuid.UUID `json:"id"`
ProjectID pgtype.UUID `json:"project_id"` ProjectID uuid.UUID `json:"project_id"`
Name string `json:"name"` Name string `json:"name"`
Doc *dto.TemplateDoc `json:"doc"` Doc *dto.TemplateDoc `json:"doc"`
} }
+3 -11
View File
@@ -5,7 +5,6 @@ import (
"testing" "testing"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/pgxpool"
"github.com/vasyakrg/dns-autoresolver/internal/store/db" "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). // defaultProject is the seed default tenant project (see migrations/0001_init.sql).
// var defaultProject = uuid.MustParse("00000000-0000-0000-0000-000000000002")
// 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}
}
func newStore(t *testing.T) (*Store, context.Context) { func newStore(t *testing.T) (*Store, context.Context) {
dsn := startPostgres(t) dsn := startPostgres(t)
@@ -35,7 +27,7 @@ func newStore(t *testing.T) (*Store, context.Context) {
func TestAccountCRUD(t *testing.T) { func TestAccountCRUD(t *testing.T) {
s, ctx := newStore(t) s, ctx := newStore(t)
acc, err := s.Queries().CreateAccount(ctx, db.CreateAccountParams{ 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", Provider: "selectel", SecretEnc: "enc-blob", Comment: "prod",
}) })
if err != nil { 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."}}, {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{ 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
+10
View File
@@ -17,3 +17,13 @@ sql:
package: dto package: dto
type: TemplateDoc type: TemplateDoc
pointer: true 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