Skip to content

Commit 651cafd

Browse files
fix(config): Validate database config in all cases (#2856)
A `continue` in `Validate` was preventing validation in cases where no Go codegen is configured. * Move some Go-specific config validation into codegen package * Update config validation test * Remove some dead code
1 parent f9bbfd9 commit 651cafd

File tree

6 files changed

+18
-65
lines changed

6 files changed

+18
-65
lines changed

internal/codegen/golang/gen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenR
108108
return nil, err
109109
}
110110

111+
if err := validateOpts(options); err != nil {
112+
return nil, err
113+
}
114+
111115
enums := buildEnums(req, options)
112116
structs := buildStructs(req, options)
113117
queries, err := buildQueries(req, options, structs)

internal/codegen/golang/opts.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type opts struct {
3939
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty"`
4040
BuildTags string `json:"build_tags,omitempty"`
4141

42-
// Unused but left in for parsing convenience
42+
// Unused but included in marshaled json we receive
4343
Overrides json.RawMessage `json:"overrides,omitempty"`
4444
Rename json.RawMessage `json:"rename,omitempty"`
4545
}
@@ -59,3 +59,11 @@ func parseOpts(req *plugin.CodeGenRequest) (*opts, error) {
5959

6060
return options, nil
6161
}
62+
63+
func validateOpts(opts *opts) error {
64+
if opts.EmitMethodsWithDbArgument && opts.EmitPreparedQueries {
65+
return fmt.Errorf("invalid options: emit_methods_with_db_argument and emit_prepared_queries options are mutually exclusive")
66+
}
67+
68+
return nil
69+
}

internal/config/config_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,12 @@ func FuzzConfig(f *testing.F) {
7979
func TestInvalidConfig(t *testing.T) {
8080
err := Validate(&Config{
8181
SQL: []SQL{{
82-
Gen: SQLGen{
83-
Go: &SQLGo{
84-
EmitMethodsWithDBArgument: true,
85-
EmitPreparedQueries: true,
86-
},
82+
Database: &Database{
83+
URI: "",
84+
Managed: false,
8785
},
88-
}}})
86+
}},
87+
})
8988
if err == nil {
9089
t.Errorf("expected err; got nil")
9190
}

internal/config/override.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77

88
"github.com/sqlc-dev/sqlc/internal/pattern"
9-
"github.com/sqlc-dev/sqlc/internal/sql/ast"
109
)
1110

1211
type Override struct {
@@ -49,39 +48,6 @@ type Override struct {
4948
GoStructTags map[string]string
5049
}
5150

52-
func (o *Override) Matches(n *ast.TableName, defaultSchema string) bool {
53-
if n == nil {
54-
return false
55-
}
56-
57-
schema := n.Schema
58-
if n.Schema == "" {
59-
schema = defaultSchema
60-
}
61-
62-
if o.TableCatalog != nil && !o.TableCatalog.MatchString(n.Catalog) {
63-
return false
64-
}
65-
66-
if o.TableSchema == nil && schema != "" {
67-
return false
68-
}
69-
70-
if o.TableSchema != nil && !o.TableSchema.MatchString(schema) {
71-
return false
72-
}
73-
74-
if o.TableRel == nil && n.Name != "" {
75-
return false
76-
}
77-
78-
if o.TableRel != nil && !o.TableRel.MatchString(n.Name) {
79-
return false
80-
}
81-
82-
return true
83-
}
84-
8551
func (o *Override) Parse() (err error) {
8652

8753
// validate deprecated postgres_type field

internal/config/python_type.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

internal/config/validate.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ import "fmt"
44

55
func Validate(c *Config) error {
66
for _, sql := range c.SQL {
7-
sqlGo := sql.Gen.Go
8-
if sqlGo == nil {
9-
continue
10-
}
11-
if sqlGo.EmitMethodsWithDBArgument && sqlGo.EmitPreparedQueries {
12-
return fmt.Errorf("invalid config: emit_methods_with_db_argument and emit_prepared_queries settings are mutually exclusive")
13-
}
147
if sql.Database != nil {
158
if sql.Database.URI == "" && !sql.Database.Managed {
169
return fmt.Errorf("invalid config: database must be managed or have a non-empty URI")

0 commit comments

Comments
 (0)