Skip to content

Commit a33afeb

Browse files
sgielenlisitsky
authored andcommitted
Add support for ignoring DDL statements using // sqlc:ignore.
1 parent e623dc1 commit a33afeb

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

internal/cmd/createdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func CreateDB(ctx context.Context, dir, filename, querySetName string, o *Option
8585
if err != nil {
8686
return fmt.Errorf("read file: %w", err)
8787
}
88-
ddl = append(ddl, migrations.RemoveRollbackStatements(string(contents)))
88+
ddl = append(ddl, migrations.RemoveIgnoredStatements(string(contents)))
8989
}
9090

9191
client, err := quickdb.NewClientFromConfig(conf.Cloud)

internal/cmd/vet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ func (c *checker) fetchDatabaseUri(ctx context.Context, s config.SQL) (string, f
419419
if err != nil {
420420
return "", cleanup, fmt.Errorf("read file: %w", err)
421421
}
422-
ddl = append(ddl, migrations.RemoveRollbackStatements(string(contents)))
422+
ddl = append(ddl, migrations.RemoveIgnoredStatements(string(contents)))
423423
}
424424

425425
resp, err := c.Client.CreateDatabase(ctx, &dbmanager.CreateDatabaseRequest{

internal/compiler/compile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (c *Compiler) parseCatalog(schemas []string) error {
3737
merr.Add(filename, "", 0, err)
3838
continue
3939
}
40-
contents := migrations.RemoveRollbackStatements(string(blob))
40+
contents := migrations.RemoveIgnoredStatements(string(blob))
4141
c.schema = append(c.schema, contents)
4242
stmts, err := c.parser.Parse(strings.NewReader(contents))
4343
if err != nil {

internal/migrations/migrations.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,44 @@ import (
55
"strings"
66
)
77

8-
// Remove all lines after a rollback comment.
8+
// Remove all lines that should be ignored by sqlc, such as rollback
9+
// comments or explicit "sqlc:ignore" lines.
910
//
1011
// goose: -- +goose Down
1112
// sql-migrate: -- +migrate Down
1213
// tern: ---- create above / drop below ----
1314
// dbmate: -- migrate:down
14-
func RemoveRollbackStatements(contents string) string {
15+
// generic: `-- sqlc:ignore` until `-- sqlc:ignore end`
16+
func RemoveIgnoredStatements(contents string) string {
1517
s := bufio.NewScanner(strings.NewReader(contents))
1618
var lines []string
19+
var ignoring bool
1720
for s.Scan() {
18-
if strings.HasPrefix(s.Text(), "-- +goose Down") {
21+
line := s.Text()
22+
23+
if strings.HasPrefix(line, "-- +goose Down") {
1924
break
2025
}
21-
if strings.HasPrefix(s.Text(), "-- +migrate Down") {
26+
if strings.HasPrefix(line, "-- +migrate Down") {
2227
break
2328
}
24-
if strings.HasPrefix(s.Text(), "---- create above / drop below ----") {
29+
if strings.HasPrefix(line, "---- create above / drop below ----") {
2530
break
2631
}
27-
if strings.HasPrefix(s.Text(), "-- migrate:down") {
32+
if strings.HasPrefix(line, "-- migrate:down") {
2833
break
2934
}
30-
lines = append(lines, s.Text())
35+
36+
if strings.HasPrefix(line, "-- sqlc:ignore end") {
37+
ignoring = false
38+
} else if strings.HasPrefix(line, "-- sqlc:ignore") {
39+
ignoring = true
40+
} else if ignoring {
41+
// make this line empty, so that errors are still reported on the
42+
// correct line
43+
line = ""
44+
}
45+
lines = append(lines, line)
3146
}
3247
return strings.Join(lines, "\n")
3348
}

0 commit comments

Comments
 (0)