Skip to content

Commit 5bc4ab2

Browse files
author
SAY-5
committed
fix(golang): cast string-enum fields in MySQL copyfrom encoder
mysqltsv.AppendValue uses a strict type-switch and rejects string-derived enum types (e.g. ExperienceLocationsType) with "can't encode type X to TSV". Detect string enums in the go-sql-driver-mysql copyfrom template and emit AppendString(string(field)) instead. Fixes #4324 Signed-off-by: SAY-5 <saiasish.cnp@gmail.com>
1 parent b84b1d6 commit 5bc4ab2

9 files changed

Lines changed: 191 additions & 5 deletions

File tree

internal/codegen/golang/gen.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum,
218218
"imports": i.Imports,
219219
"hasImports": i.HasImports,
220220
"hasPrefix": strings.HasPrefix,
221+
"hasSuffix": strings.HasSuffix,
221222

222223
// These methods are Go specific, they do not belong in the codegen package
223224
// (as that is language independent)

internal/codegen/golang/templates/go-sql-driver-mysql/copyfromCopy.tmpl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ func convertRowsFor{{.MethodName}}(w *io.PipeWriter, {{.Arg.SlicePair}}) {
77
e := mysqltsv.NewEncoder(w, {{ len .Arg.CopyFromMySQLFields }}, nil)
88
for _, row := range {{.Arg.Name}} {
99
{{- with $arg := .Arg }}
10+
{{- $enums := $.Enums }}
1011
{{- range $arg.CopyFromMySQLFields}}
11-
{{- if eq .Type "string"}}
12-
e.AppendString({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
13-
{{- else if or (eq .Type "[]byte") (eq .Type "json.RawMessage")}}
14-
e.AppendBytes({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
12+
{{- $expr := "row" }}{{ if $arg.Struct }}{{ $expr = print "row." .Name }}{{ end }}
13+
{{- $fieldType := .Type }}
14+
{{- $isStringEnum := false }}
15+
{{- range $enums }}{{ if or (eq $fieldType .Name) (hasSuffix $fieldType (print "." .Name)) }}{{ $isStringEnum = true }}{{ end }}{{ end }}
16+
{{- if eq $fieldType "string"}}
17+
e.AppendString({{$expr}})
18+
{{- else if or (eq $fieldType "[]byte") (eq $fieldType "json.RawMessage")}}
19+
e.AppendBytes({{$expr}})
20+
{{- else if $isStringEnum }}
21+
e.AppendString(string({{$expr}}))
1522
{{- else}}
16-
e.AppendValue({{if $arg.Struct}}row.{{.Name}}{{else}}row{{end}})
23+
e.AppendValue({{$expr}})
1724
{{- end}}
1825
{{- end}}
1926
{{- end}}

internal/endtoend/testdata/copyfrom_mysql_enum/mysql/go/copyfrom.go

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

internal/endtoend/testdata/copyfrom_mysql_enum/mysql/go/db.go

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

internal/endtoend/testdata/copyfrom_mysql_enum/mysql/go/models.go

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

internal/endtoend/testdata/copyfrom_mysql_enum/mysql/go/query.sql.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- name: UpsertExperienceLocations :copyfrom
2+
REPLACE INTO experience_locations (location_id, type)
3+
VALUES (?, ?);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE experience_locations (
2+
location_id varchar(512) NOT NULL,
3+
type ENUM('start_point', 'pickup_point', 'redemption_point', 'end_point') NOT NULL
4+
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"sql_package": "database/sql",
7+
"sql_driver": "github.com/go-sql-driver/mysql",
8+
"engine": "mysql",
9+
"name": "querytest",
10+
"schema": "schema.sql",
11+
"queries": "query.sql"
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)