Skip to content

Commit efcc4fe

Browse files
authored
mysql type overrides (#281)
* adds support for mysql type overrides * adds terminal color package * adjusts config fields for compatability * cleans up tests * adds mysql override tests * removes changes to booktest and colored warning * patches example
1 parent 5711734 commit efcc4fe

File tree

20 files changed

+300
-294
lines changed

20 files changed

+300
-294
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,14 @@ instead.
331331
"overrides": [
332332
{
333333
"go_type": "github.com/gofrs/uuid.UUID",
334-
"postgres_type": "uuid"
334+
"db_type": "uuid"
335335
}
336336
]
337337
}
338338
```
339339

340340
Each override document has the following keys:
341-
- `postgres_type`:
341+
- `db_type`:
342342
- The PostgreSQL type to override. Find the full list of supported types in [gen.go](https://github.com/kyleconroy/sqlc/blob/master/internal/dinosql/gen.go#L438).
343343
- `go_type`:
344344
- A fully qualified name to a Go type to use in the generated code.

examples/booktest/mysql/models.go

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

examples/booktest/mysql/query.sql

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,13 @@ INSERT INTO books (
2525
title,
2626
yr,
2727
available,
28-
tags,
29-
is_hardcover,
30-
rating,
31-
front_cover_img,
32-
back_cover_img
28+
tags
3329
) VALUES (
3430
?,
3531
?,
3632
?,
3733
?,
3834
?,
39-
?,
40-
?,
41-
?,
42-
?,
43-
?,
4435
?
4536
);
4637

examples/booktest/mysql/query.sql.go

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

examples/booktest/mysql/schema.sql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ CREATE TABLE books (
1919
title text NOT NULL DEFAULT '',
2020
yr integer NOT NULL DEFAULT 2000,
2121
available datetime NOT NULL DEFAULT NOW(),
22-
tags text NOT NULL DEFAULT '',
23-
is_hardcover BOOL NOT NULL DEFAULT 0,
24-
rating decimal(13, 2),
25-
front_cover_img varchar(255),
26-
back_cover_img varchar(255)
22+
tags text NOT NULL DEFAULT ''
2723
-- CONSTRAINT FOREIGN KEY (author_id) REFERENCES authors(author_id)
2824
) ENGINE=InnoDB;
2925

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
6565
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
6666
github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
6767
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
68+
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
69+
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
6870
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
6971
github.com/ghodss/yaml v0.0.0-20161207003320-04f313413ffd/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
7072
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
@@ -211,8 +213,10 @@ github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQ
211213
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
212214
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
213215
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
216+
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
214217
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
215218
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
219+
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
216220
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
217221
github.com/mattn/go-runewidth v0.0.1/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
218222
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=

internal/dinosql/config.go

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"go/types"
88
"io"
9+
"os"
910
"path/filepath"
1011
"strings"
1112

@@ -58,43 +59,74 @@ type Override struct {
5859
GoType string `json:"go_type"`
5960

6061
// fully qualified name of the Go type, e.g. `github.com/segmentio/ksuid.KSUID`
61-
PostgresType string `json:"postgres_type"`
62+
DBType string `json:"db_type"`
63+
Deprecated_PostgresType string `json:"postgres_type"`
64+
65+
// for global overrides only when two different engines are in use
66+
Engine Engine `json:"engine,omitempty"`
6267

6368
// True if the GoType should override if the maching postgres type is nullable
6469
Null bool `json:"null"`
6570

6671
// fully qualified name of the column, e.g. `accounts.id`
6772
Column string `json:"column"`
6873

69-
columnName string
70-
table pg.FQN
71-
goTypeName string
74+
ColumnName string
75+
Table pg.FQN
76+
GoTypeName string
7277
goPackage string
7378
goBasicType bool
7479
}
7580

81+
func (c *GenerateSettings) ValidateGlobalOverrides() error {
82+
engines := map[Engine]struct{}{}
83+
for _, pkg := range c.Packages {
84+
if _, ok := engines[pkg.Engine]; !ok {
85+
engines[pkg.Engine] = struct{}{}
86+
}
87+
}
88+
89+
usesMultipleEngines := len(engines) > 1
90+
for _, oride := range c.Overrides {
91+
if usesMultipleEngines && oride.Engine == "" {
92+
return fmt.Errorf(`the "engine" field is required for global type overrides because your configuration uses multiple database engines`)
93+
}
94+
}
95+
return nil
96+
}
97+
7698
func (o *Override) Parse() error {
99+
100+
// validate deprecated postgres_type field
101+
if o.Deprecated_PostgresType != "" {
102+
fmt.Fprintf(os.Stderr, "WARNING: \"postgres_type\" is deprecated. Instead, use \"db_type\" to specify a type override.\n")
103+
if o.DBType != "" {
104+
return fmt.Errorf(`Type override configurations cannot have "db_type" and "postres_type" together. Use "db_type" alone`)
105+
}
106+
o.DBType = o.Deprecated_PostgresType
107+
}
108+
77109
// validate option combinations
78110
switch {
79-
case o.Column != "" && o.PostgresType != "":
80-
return fmt.Errorf("Override specifying both `column` (%q) and `postgres_type` (%q) is not valid.", o.Column, o.PostgresType)
81-
case o.Column == "" && o.PostgresType == "":
82-
return fmt.Errorf("Override must specify one of either `column` or `postgres_type`")
111+
case o.Column != "" && o.DBType != "":
112+
return fmt.Errorf("Override specifying both `column` (%q) and `db_type` (%q) is not valid.", o.Column, o.DBType)
113+
case o.Column == "" && o.DBType == "":
114+
return fmt.Errorf("Override must specify one of either `column` or `db_type`")
83115
}
84116

85117
// validate Column
86118
if o.Column != "" {
87119
colParts := strings.Split(o.Column, ".")
88120
switch len(colParts) {
89121
case 2:
90-
o.columnName = colParts[1]
91-
o.table = pg.FQN{Schema: "public", Rel: colParts[0]}
122+
o.ColumnName = colParts[1]
123+
o.Table = pg.FQN{Schema: "public", Rel: colParts[0]}
92124
case 3:
93-
o.columnName = colParts[2]
94-
o.table = pg.FQN{Schema: colParts[0], Rel: colParts[1]}
125+
o.ColumnName = colParts[2]
126+
o.Table = pg.FQN{Schema: colParts[0], Rel: colParts[1]}
95127
case 4:
96-
o.columnName = colParts[3]
97-
o.table = pg.FQN{Catalog: colParts[0], Schema: colParts[1], Rel: colParts[2]}
128+
o.ColumnName = colParts[3]
129+
o.Table = pg.FQN{Catalog: colParts[0], Schema: colParts[1], Rel: colParts[2]}
98130
default:
99131
return fmt.Errorf("Override `column` specifier %q is not the proper format, expected '[catalog.][schema.]colname.tablename'", o.Column)
100132
}
@@ -144,11 +176,11 @@ func (o *Override) Parse() error {
144176
}
145177
o.goPackage = o.GoType[:lastDot]
146178
}
147-
o.goTypeName = typename
179+
o.GoTypeName = typename
148180
isPointer := o.GoType[0] == '*'
149181
if isPointer {
150182
o.goPackage = o.goPackage[1:]
151-
o.goTypeName = "*" + o.goTypeName
183+
o.GoTypeName = "*" + o.GoTypeName
152184
}
153185

154186
return nil
@@ -176,6 +208,9 @@ func ParseConfig(rd io.Reader) (GenerateSettings, error) {
176208
if len(config.Packages) == 0 {
177209
return config, ErrNoPackages
178210
}
211+
if err := config.ValidateGlobalOverrides(); err != nil {
212+
return config, err
213+
}
179214
for i := range config.Overrides {
180215
if err := config.Overrides[i].Parse(); err != nil {
181216
return config, err

internal/dinosql/config_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ func TestTypeOverrides(t *testing.T) {
7171
}{
7272
{
7373
Override{
74-
PostgresType: "uuid",
75-
GoType: "github.com/segmentio/ksuid.KSUID",
74+
DBType: "uuid",
75+
GoType: "github.com/segmentio/ksuid.KSUID",
7676
},
7777
"github.com/segmentio/ksuid",
7878
"ksuid.KSUID",
@@ -82,7 +82,7 @@ func TestTypeOverrides(t *testing.T) {
8282
//
8383
// {
8484
// Override{
85-
// PostgresType: "uuid",
85+
// DBType: "uuid",
8686
// GoType: "github.com/segmentio/*ksuid.KSUID",
8787
// },
8888
// "github.com/segmentio/ksuid",
@@ -91,8 +91,8 @@ func TestTypeOverrides(t *testing.T) {
9191
// },
9292
{
9393
Override{
94-
PostgresType: "citext",
95-
GoType: "string",
94+
DBType: "citext",
95+
GoType: "string",
9696
},
9797
"",
9898
"string",
@@ -104,7 +104,7 @@ func TestTypeOverrides(t *testing.T) {
104104
if err := tt.override.Parse(); err != nil {
105105
t.Fatalf("override parsing failed; %s", err)
106106
}
107-
if diff := cmp.Diff(tt.typeName, tt.override.goTypeName); diff != "" {
107+
if diff := cmp.Diff(tt.typeName, tt.override.GoTypeName); diff != "" {
108108
t.Errorf("type name mismatch;\n%s", diff)
109109
}
110110
if diff := cmp.Diff(tt.pkg, tt.override.goPackage); diff != "" {
@@ -121,15 +121,15 @@ func TestTypeOverrides(t *testing.T) {
121121
}{
122122
{
123123
Override{
124-
PostgresType: "uuid",
125-
GoType: "Pointer",
124+
DBType: "uuid",
125+
GoType: "Pointer",
126126
},
127127
"Package override `go_type` specifier \"Pointer\" is not a Go basic type e.g. 'string'",
128128
},
129129
{
130130
Override{
131-
PostgresType: "uuid",
132-
GoType: "untyped rune",
131+
DBType: "uuid",
132+
GoType: "untyped rune",
133133
},
134134
"Package override `go_type` specifier \"untyped rune\" is not a Go basic type e.g. 'string'",
135135
},

0 commit comments

Comments
 (0)