Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add support for ignoring DDL statements using // sqlc:ignore.
  • Loading branch information
sgielen committed Jan 12, 2024
commit fbbc3d9830fa11404011fc6d0444c1b730afe813
2 changes: 1 addition & 1 deletion internal/cmd/createdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 22 additions & 7 deletions internal/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down