Skip to content

Commit 0824475

Browse files
EliSauderCopilot
andcommitted
fix(postgresql): name a domain's base as the parser does
The base was read the way the analysis core reads a type, lowercased and without pg_catalog, so a domain over a quoted enum named no enum and one over numeric missed a pg_catalog.numeric override. Keep the parser's name, as a column of the base type has it. Addresses review comment r4556992547 on #2. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b429383 commit 0824475

6 files changed

Lines changed: 81 additions & 11 deletions

File tree

‎internal/endtoend/testdata/codegen_json_domains_postgresql/gen/codegen.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
{
152152
"name": "title",
153153
"base": {
154-
"name": "varchar",
154+
"name": "pg_catalog.varchar",
155155
"nullable": false,
156156
"args": [
157157
{
@@ -166,7 +166,7 @@
166166
{
167167
"name": "price",
168168
"base": {
169-
"name": "numeric",
169+
"name": "pg_catalog.numeric",
170170
"nullable": false,
171171
"args": [
172172
{
@@ -68811,7 +68811,7 @@
6881168811
{
6881268812
"name": "ksuid",
6881368813
"base": {
68814-
"name": "bpchar",
68814+
"name": "pg_catalog.bpchar",
6881568815
"nullable": false,
6881668816
"args": [
6881768817
{

‎internal/endtoend/testdata/domain_types/postgresql/go/models.go‎

Lines changed: 52 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/domain_types/postgresql/go/query.sql.go‎

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/domain_types/postgresql/schema.sql‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CREATE DOMAIN tags AS text[];
22
CREATE DOMAIN title AS varchar(100) NOT NULL DEFAULT '';
33
CREATE DOMAIN price AS numeric(10, 2) CHECK (VALUE >= 0);
4+
-- The base keeps a quoted name's case and pg_catalog.
5+
CREATE TYPE "Mood" AS ENUM ('happy', 'sad');
6+
CREATE DOMAIN feeling AS "Mood";
7+
CREATE DOMAIN amount AS numeric(10, 2) NOT NULL;
48
CREATE SCHEMA billing;
59
CREATE DOMAIN billing.ksuid AS char(27);
610
-- Named like a built-in, but off the search path.
@@ -19,7 +23,9 @@ CREATE TABLE books (
1923
id serial PRIMARY KEY,
2024
title title,
2125
price price,
22-
ref billing.ksuid
26+
ref billing.ksuid,
27+
feeling feeling NOT NULL,
28+
amount amount
2329
);
2430

2531
CREATE TABLE shelves (

‎internal/endtoend/testdata/domain_types/postgresql/sqlc.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ sql:
1212
- db_type: "billing.ksuid"
1313
go_type: "github.com/google/uuid.NullUUID"
1414
nullable: true
15+
- db_type: "pg_catalog.numeric"
16+
go_type: "github.com/shopspring/decimal.Decimal"

‎internal/sql/catalog/types.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package catalog
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67

78
"github.com/sqlc-dev/sqlc/internal/core"
89
"github.com/sqlc-dev/sqlc/internal/sql/ast"
@@ -132,6 +133,12 @@ func (c *Catalog) createDomain(stmt *ast.CreateDomainStmt) error {
132133
Name: name.Name,
133134
Base: core.TypeExprOfTypeName(stmt.TypeName),
134135
}
136+
// The base keeps the name the parser gave it, a quoted name's case and
137+
// pg_catalog included, as a column of the type does, so the two map to
138+
// Go and match overrides alike.
139+
if domain.Base != nil && stmt.TypeName.Names != nil {
140+
domain.Base.Innermost().Name = strings.Join(stringSlice(stmt.TypeName.Names), ".")
141+
}
135142
if !isArrayTypeName(stmt.TypeName) {
136143
domain.base = domainName(stmt.TypeName.Names)
137144
}

0 commit comments

Comments
 (0)