Skip to content

Commit

Permalink
sqlite: fixed sqlite column type override
Browse files Browse the repository at this point in the history
We should not assume that a table has a default schema name in the override config.
Because different sql databases can have different default schemas.

Fixes: sqlc-dev#1985
Signed-off-by: David Anderson <[email protected]>
  • Loading branch information
mcdoker18 authored and danderson committed May 7, 2023
1 parent c13b156 commit 6aeafc2
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func pluginOverride(o config.Override) *plugin.Override {
colParts := strings.Split(o.Column, ".")
switch len(colParts) {
case 2:
table.Schema = "public"
//table.Schema = "public"
table.Name = colParts[0]
column = colParts[1]
case 3:
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Matches(o *plugin.Override, n *plugin.Identifier, defaultSchema string) boo
if o.Table.Catalog != "" && !MatchString(o.Table.Catalog, n.Catalog) {
return false
}
if o.Table.Schema == "" && schema != "" {
if o.Table.Schema == "" && n.Schema != "" {
return false
}
if o.Table.Schema != "" && !MatchString(o.Table.Schema, schema) {
Expand Down
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/overrides/sqlite/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/endtoend/testdata/overrides/sqlite/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/endtoend/testdata/overrides/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT 1;
5 changes: 5 additions & 0 deletions internal/endtoend/testdata/overrides/sqlite/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE foo (
other text NOT NULL,
total bigint NOT NULL,
retyped text NOT NULL
);
18 changes: 18 additions & 0 deletions internal/endtoend/testdata/overrides/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "override",
"engine": "sqlite",
"schema": "schema.sql",
"queries": "query.sql",
"overrides": [
{
"go_type": "github.com/kyleconroy/sqlc-testdata/pkg.CustomType",
"column": "foo.retyped"
}
]
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE foo (
other text NOT NULL,
tagged text NOT NULL
);

CREATE TABLE bar (
other text NOT NULL,
also_tagged text NOT NULL
);

CREATE TABLE baz (
other text NOT NULL,
also_tagged text NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "override",
"engine": "sqlite",
"schema": "schema.sql",
"queries": "query.sql",
"overrides": [
{
"go_struct_tag": "a:\"b\" x:\"y,z\"",
"column": "foo.tagged"
},
{
"go_struct_tag": "also:\"tagged\"",
"column": "*.also_tagged"
}
]
}
]
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/overrides_go_types/sqlite/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions internal/endtoend/testdata/overrides_go_types/sqlite/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT 1;
17 changes: 17 additions & 0 deletions internal/endtoend/testdata/overrides_go_types/sqlite/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CREATE TABLE foo (
other text NOT NULL,
total bigint NOT NULL,
retyped text NOT NULL
);

CREATE TABLE bar (
other text NOT NULL,
total bigint NOT NULL,
also_retyped text NOT NULL
);

CREATE TABLE baz (
other text NOT NULL,
total bigint NOT NULL,
also_retyped text NOT NULL
);
22 changes: 22 additions & 0 deletions internal/endtoend/testdata/overrides_go_types/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "override",
"engine": "sqlite",
"schema": "schema.sql",
"queries": "query.sql",
"overrides": [
{
"go_type": "github.com/kyleconroy/sqlc-testdata/pkg.CustomType",
"column": "foo.retyped"
},
{
"go_type": "github.com/kyleconroy/sqlc-testdata/pkg.CustomType",
"column": "*.also_retyped"
}
]
}
]
}

0 comments on commit 6aeafc2

Please sign in to comment.