Skip to content

Commit 35f5883

Browse files
authored
cmd/vet: Add --no-database option (#2405)
* cmd/vet: Add --no-db option
1 parent d32d6af commit 35f5883

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

internal/cmd/cmd.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int
3737
rootCmd.PersistentFlags().StringP("file", "f", "", "specify an alternate config file (default: sqlc.yaml)")
3838
rootCmd.PersistentFlags().BoolP("experimental", "x", false, "DEPRECATED: enable experimental features (default: false)")
3939
rootCmd.PersistentFlags().Bool("no-remote", false, "disable remote execution (default: false)")
40+
rootCmd.PersistentFlags().Bool("no-database", false, "disable database connections (default: false)")
4041

4142
rootCmd.AddCommand(checkCmd)
4243
rootCmd.AddCommand(diffCmd)
@@ -133,18 +134,21 @@ var initCmd = &cobra.Command{
133134
}
134135

135136
type Env struct {
136-
DryRun bool
137-
Debug opts.Debug
138-
NoRemote bool
137+
DryRun bool
138+
Debug opts.Debug
139+
NoRemote bool
140+
NoDatabase bool
139141
}
140142

141143
func ParseEnv(c *cobra.Command) Env {
142144
dr := c.Flag("dry-run")
143145
nr := c.Flag("no-remote")
146+
nodb := c.Flag("no-database")
144147
return Env{
145-
DryRun: dr != nil && dr.Changed,
146-
Debug: opts.DebugFromEnv(),
147-
NoRemote: nr != nil && nr.Value.String() == "true",
148+
DryRun: dr != nil && dr.Changed,
149+
Debug: opts.DebugFromEnv(),
150+
NoRemote: nr != nil && nr.Value.String() == "true",
151+
NoDatabase: nodb != nil && nodb.Value.String() == "true",
148152
}
149153
}
150154

internal/cmd/vet.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,14 @@ func Vet(ctx context.Context, e Env, dir, filename string, stderr io.Writer) err
125125
}
126126

127127
c := checker{
128-
Checks: checks,
129-
Conf: conf,
130-
Dir: dir,
131-
Env: env,
132-
Envmap: map[string]string{},
133-
Msgs: msgs,
134-
Stderr: stderr,
128+
Checks: checks,
129+
Conf: conf,
130+
Dir: dir,
131+
Env: env,
132+
Envmap: map[string]string{},
133+
Msgs: msgs,
134+
Stderr: stderr,
135+
NoDatabase: e.NoDatabase,
135136
}
136137
errored := false
137138
for _, sql := range conf.SQL {
@@ -200,13 +201,14 @@ func (p *dbPreparer) Prepare(ctx context.Context, name, query string) error {
200201
}
201202

202203
type checker struct {
203-
Checks map[string]cel.Program
204-
Conf *config.Config
205-
Dir string
206-
Env *cel.Env
207-
Envmap map[string]string
208-
Msgs map[string]string
209-
Stderr io.Writer
204+
Checks map[string]cel.Program
205+
Conf *config.Config
206+
Dir string
207+
Env *cel.Env
208+
Envmap map[string]string
209+
Msgs map[string]string
210+
Stderr io.Writer
211+
NoDatabase bool
210212
}
211213

212214
func (c *checker) DSN(dsn string) (string, error) {
@@ -250,6 +252,9 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
250252
// TODO: Add MySQL support
251253
var prep preparer
252254
if s.Database != nil {
255+
if c.NoDatabase {
256+
return fmt.Errorf("database: connections disabled via command line flag")
257+
}
253258
dburl, err := c.DSN(s.Database.URL)
254259
if err != nil {
255260
return err

0 commit comments

Comments
 (0)