Skip to content

Commit 2cd364c

Browse files
authored
fix(codegen): Correct column names in :copyfrom (#2838)
* fix(codegen): Correct column names in :copyfrom * Disable BuildKite reporting
1 parent bb84596 commit 2cd364c

File tree

14 files changed

+142
-8
lines changed

14 files changed

+142
-8
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
working-directory: internal/endtoend/testdata
7373

7474
- name: report
75-
if: ${{ github.ref == 'refs/heads/main' }}
75+
if: false
7676
run: ./scripts/report.sh
7777
env:
7878
BUILDKITE_ANALYTICS_TOKEN: ${{ secrets.BUILDKITE_ANALYTICS_TOKEN }}
@@ -87,4 +87,4 @@ jobs:
8787
with:
8888
go-version: '1.21.3'
8989
- run: go install golang.org/x/vuln/cmd/govulncheck@latest
90-
- run: govulncheck ./...
90+
- run: govulncheck ./...

examples/batch/sqlc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"database": {
1414
"managed": true
1515
},
16+
"analyzer": {
17+
"database": false
18+
},
1619
"rules": [
1720
"sqlc/db-prepare"
1821
],
@@ -22,4 +25,4 @@
2225
"emit_interface": true
2326
}
2427
]
25-
}
28+
}

internal/codegen/golang/query.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ func (v QueryValue) ColumnNamesAsGoSlice() string {
166166
}
167167
escapedNames := make([]string, len(v.Struct.Fields))
168168
for i, f := range v.Struct.Fields {
169-
escapedNames[i] = fmt.Sprintf("%q", f.DBName)
169+
if f.Column != nil && f.Column.OriginalName != "" {
170+
escapedNames[i] = fmt.Sprintf("%q", f.Column.OriginalName)
171+
} else {
172+
escapedNames[i] = fmt.Sprintf("%q", f.DBName)
173+
}
170174
}
171175
return "[]string{" + strings.Join(escapedNames, ", ") + "}"
172176
}

internal/config/v_one.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type v1PackageSettings struct {
2121
Name string `json:"name" yaml:"name"`
2222
Engine Engine `json:"engine,omitempty" yaml:"engine"`
2323
Database *Database `json:"database,omitempty" yaml:"database"`
24-
Analyzer Analyzer `json:"analyzer" yaml:"analyzer"`
24+
Analyzer Analyzer `json:"analyzer" yaml:"analyzer"`
2525
Path string `json:"path" yaml:"path"`
2626
Schema Paths `json:"schema" yaml:"schema"`
2727
Queries Paths `json:"queries" yaml:"queries"`
@@ -148,6 +148,7 @@ func (c *V1GenerateSettings) Translate() Config {
148148
Schema: pkg.Schema,
149149
Queries: pkg.Queries,
150150
Rules: pkg.Rules,
151+
Analyzer: pkg.Analyzer,
151152
Gen: SQLGen{
152153
Go: &SQLGo{
153154
EmitInterface: pkg.EmitInterface,

internal/endtoend/endtoend_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
func TestExamples(t *testing.T) {
24-
// t.Parallel()
24+
t.Parallel()
2525
ctx := context.Background()
2626

2727
examples, err := filepath.Abs(filepath.Join("..", "..", "examples"))
@@ -40,7 +40,7 @@ func TestExamples(t *testing.T) {
4040
}
4141
tc := replay.Name()
4242
t.Run(tc, func(t *testing.T) {
43-
// t.Parallel()
43+
t.Parallel()
4444
path := filepath.Join(examples, tc)
4545
var stderr bytes.Buffer
4646
opts := &cmd.Options{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/sqlc-dev/sqlc/issues/2833

internal/endtoend/testdata/copyfrom_named_params/postgresql/pgx/go/copyfrom.go

Lines changed: 43 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_named_params/postgresql/pgx/go/db.go

Lines changed: 33 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_named_params/postgresql/pgx/go/models.go

Lines changed: 12 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_named_params/postgresql/pgx/go/query.sql.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- name: StageUserData :copyfrom
2+
insert into "user_data" ("id", "user")
3+
values (
4+
sqlc.arg('id_param'),
5+
sqlc.arg('user_param')
6+
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
create table "user_data" (
2+
"id" varchar not null,
3+
"user" varchar not null,
4+
primary key ("id")
5+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"

internal/sql/validate/cmd.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ func validateCopyfrom(n ast.Node) error {
3939
return nil
4040
}
4141
for _, v := range sublist.Items {
42-
if _, ok := v.(*ast.ParamRef); !ok {
42+
_, ok := v.(*ast.ParamRef)
43+
ok = ok || named.IsParamFunc(v)
44+
ok = ok || named.IsParamSign(v)
45+
if !ok {
4346
return errors.New(":copyfrom doesn't support non-parameter values")
4447
}
4548
}

0 commit comments

Comments
 (0)