From fbbc3d9830fa11404011fc6d0444c1b730afe813 Mon Sep 17 00:00:00 2001 From: Sjors Gielen Date: Fri, 12 Jan 2024 16:44:24 +0100 Subject: [PATCH] Add support for ignoring DDL statements using `// sqlc:ignore`. --- internal/cmd/createdb.go | 2 +- internal/cmd/vet.go | 2 +- internal/compiler/compile.go | 2 +- internal/migrations/migrations.go | 29 ++++++++++++++++++++++------- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/internal/cmd/createdb.go b/internal/cmd/createdb.go index 9298dfb088..2b73644e32 100644 --- a/internal/cmd/createdb.go +++ b/internal/cmd/createdb.go @@ -85,7 +85,7 @@ func CreateDB(ctx context.Context, dir, filename, querySetName string, o *Option if err != nil { return fmt.Errorf("read file: %w", err) } - ddl = append(ddl, migrations.RemoveRollbackStatements(string(contents))) + ddl = append(ddl, migrations.RemoveIgnoredStatements(string(contents))) } client, err := quickdb.NewClientFromConfig(conf.Cloud) diff --git a/internal/cmd/vet.go b/internal/cmd/vet.go index 5de23394d7..0f7e65aed5 100644 --- a/internal/cmd/vet.go +++ b/internal/cmd/vet.go @@ -422,7 +422,7 @@ func (c *checker) fetchDatabaseUri(ctx context.Context, s config.SQL) (string, f if err != nil { return "", cleanup, fmt.Errorf("read file: %w", err) } - ddl = append(ddl, migrations.RemoveRollbackStatements(string(contents))) + ddl = append(ddl, migrations.RemoveIgnoredStatements(string(contents))) } resp, err := c.Client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{ diff --git a/internal/compiler/compile.go b/internal/compiler/compile.go index 84fbb20a3c..ee8ab7a3f1 100644 --- a/internal/compiler/compile.go +++ b/internal/compiler/compile.go @@ -37,7 +37,7 @@ func (c *Compiler) parseCatalog(schemas []string) error { merr.Add(filename, "", 0, err) continue } - contents := migrations.RemoveRollbackStatements(string(blob)) + contents := migrations.RemoveIgnoredStatements(string(blob)) c.schema = append(c.schema, contents) stmts, err := c.parser.Parse(strings.NewReader(contents)) if err != nil { diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go index ac0e8b3d9a..1c33eacb10 100644 --- a/internal/migrations/migrations.go +++ b/internal/migrations/migrations.go @@ -5,29 +5,44 @@ import ( "strings" ) -// Remove all lines after a rollback comment. +// Remove all lines that should be ignored by sqlc, such as rollback +// comments or explicit "sqlc:ignore" lines. // // goose: -- +goose Down // sql-migrate: -- +migrate Down // tern: ---- create above / drop below ---- // dbmate: -- migrate:down -func RemoveRollbackStatements(contents string) string { +// generic: `-- sqlc:ignore` until `-- sqlc:ignore end` +func RemoveIgnoredStatements(contents string) string { s := bufio.NewScanner(strings.NewReader(contents)) var lines []string + var ignoring bool for s.Scan() { - if strings.HasPrefix(s.Text(), "-- +goose Down") { + line := s.Text() + + if strings.HasPrefix(line, "-- +goose Down") { break } - if strings.HasPrefix(s.Text(), "-- +migrate Down") { + if strings.HasPrefix(line, "-- +migrate Down") { break } - if strings.HasPrefix(s.Text(), "---- create above / drop below ----") { + if strings.HasPrefix(line, "---- create above / drop below ----") { break } - if strings.HasPrefix(s.Text(), "-- migrate:down") { + if strings.HasPrefix(line, "-- migrate:down") { break } - lines = append(lines, s.Text()) + + if strings.HasPrefix(line, "-- sqlc:ignore end") { + ignoring = false + } else if strings.HasPrefix(line, "-- sqlc:ignore") { + ignoring = true + } else if ignoring { + // make this line empty, so that errors are still reported on the + // correct line + line = "" + } + lines = append(lines, line) } return strings.Join(lines, "\n") }