Skip to content

Commit

Permalink
Add support for ignoring DDL statements using -- sqlc:ignore
Browse files Browse the repository at this point in the history
This fix adds tests and some renamings RemoveRollbackStatements -> RemoveIgnoredStatements as now not only rollbacks are ignored.
  • Loading branch information
lisitsky committed Jun 21, 2024
1 parent 49f2cb1 commit 1af30a3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func Verify(ctx context.Context, dir, filename string, opts *Options) error {
if err != nil {
return fmt.Errorf("read file: %w", err)
}
ddl = append(ddl, migrations.RemoveRollbackStatements(string(contents)))
ddl = append(ddl, migrations.RemoveIgnoredStatements(string(contents)))
}

var codegen plugin.GenerateRequest
Expand Down
6 changes: 5 additions & 1 deletion internal/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ func RemoveIgnoredStatements(contents string) string {

if strings.HasPrefix(line, "-- sqlc:ignore end") {
ignoring = false
// no need to keep this line in result
line = ""
} else if strings.HasPrefix(line, "-- sqlc:ignore") {
ignoring = true
} else if ignoring {
}

if ignoring {
// make this line empty, so that errors are still reported on the
// correct line
line = ""
Expand Down
49 changes: 41 additions & 8 deletions internal/migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@ const inputGoose = `
-- +goose Up
ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
-- sqlc:ignore
CREATE TABLE countries (id int);
CREATE TABLE people (id int);
-- sqlc:ignore
-- +goose Down
ALTER TABLE archived_jobs DROP COLUMN expires_at;
`

const outputGoose = `
-- +goose Up
ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
`

const inputMigrate = `
-- +migrate Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE people (id int);
-- sqlc:ignore
INVALID SYNTAX HERE IS OK, WE SHOULD IGNORE THIS
-- sqlc:ignore end
-- +migrate Down
-- SQL section 'Down' is executed when this migration is rolled back
DROP TABLE people;
Expand All @@ -33,47 +47,66 @@ const outputMigrate = `
-- +migrate Up
-- SQL in section 'Up' is executed when this migration is applied
CREATE TABLE people (id int);
`

const inputTern = `
-- sqlc:ignore
As first row also ok, all contents after should be processed
-- sqlc:ignore end
-- Write your migrate up statements here
ALTER TABLE todo RENAME COLUMN done TO is_done;
---- create above / drop below ----
ALTER TABLE todo RENAME COLUMN is_done TO done;
`

const outputTern = `
-- Write your migrate up statements here
ALTER TABLE todo RENAME COLUMN done TO is_done;`

const inputDbmate = `
-- migrate:up
CREATE TABLE foo (bar int);
-- sqlc:ignore
In up section
-- sqlc:ignore end
-- migrate:down
DROP TABLE foo;`
DROP TABLE foo;
-- sqlc:ignore
In down section
-- sqlc:ignore end`

const outputDbmate = `
-- migrate:up
CREATE TABLE foo (bar int);`
CREATE TABLE foo (bar int);
`

func TestRemoveRollback(t *testing.T) {
if diff := cmp.Diff(outputGoose, RemoveRollbackStatements(inputGoose)); diff != "" {
func TestRemoveIgnored(t *testing.T) {
if diff := cmp.Diff(outputGoose, RemoveIgnoredStatements(inputGoose)); diff != "" {
t.Errorf("goose migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputMigrate, RemoveRollbackStatements(inputMigrate)); diff != "" {
if diff := cmp.Diff(outputMigrate, RemoveIgnoredStatements(inputMigrate)); diff != "" {
t.Errorf("sql-migrate migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputTern, RemoveRollbackStatements(inputTern)); diff != "" {
if diff := cmp.Diff(outputTern, RemoveIgnoredStatements(inputTern)); diff != "" {
t.Errorf("tern migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputDbmate, RemoveRollbackStatements(inputDbmate)); diff != "" {
if diff := cmp.Diff(outputDbmate, RemoveIgnoredStatements(inputDbmate)); diff != "" {
t.Errorf("dbmate migration mismatch:\n%s", diff)
}
}

func TestRemoveGolangMigrateRollback(t *testing.T) {
filenames := map[string]bool{
// make sure we let through golang-migrate files that aren't rollbacks
// make sure we let through golang-migrate files that aren't ignored
"migrations/1.up.sql": false,
// make sure we let through other sql files
"migrations/2.sql": false,
Expand Down
2 changes: 1 addition & 1 deletion internal/sqltest/local/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func MySQL(t *testing.T, migrations []string) string {
if err != nil {
t.Fatal(err)
}
seed = append(seed, migrate.RemoveRollbackStatements(string(blob)))
seed = append(seed, migrate.RemoveIgnoredStatements(string(blob)))
}

cfg, err := mysql.ParseDSN(dburi)
Expand Down
2 changes: 1 addition & 1 deletion internal/sqltest/local/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func postgreSQL(t *testing.T, migrations []string, rw bool) string {
t.Fatal(err)
}
h.Write(blob)
seed = append(seed, migrate.RemoveRollbackStatements(string(blob)))
seed = append(seed, migrate.RemoveIgnoredStatements(string(blob)))
}

var name string
Expand Down

0 comments on commit 1af30a3

Please sign in to comment.