Skip to content

Commit f1a683c

Browse files
committed
Add support for ignoring DDL statements using -- sqlc:ignore
This fix adds tests and some renamings RemoveRollbackStatements -> RemoveIgnoredStatements as now not only rollbacks are ignored.
1 parent a33afeb commit f1a683c

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

internal/cmd/verify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func Verify(ctx context.Context, dir, filename string, opts *Options) error {
101101
if err != nil {
102102
return fmt.Errorf("read file: %w", err)
103103
}
104-
ddl = append(ddl, migrations.RemoveRollbackStatements(string(contents)))
104+
ddl = append(ddl, migrations.RemoveIgnoredStatements(string(contents)))
105105
}
106106

107107
var codegen plugin.GenerateRequest

internal/migrations/migrations.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ func RemoveIgnoredStatements(contents string) string {
3535

3636
if strings.HasPrefix(line, "-- sqlc:ignore end") {
3737
ignoring = false
38+
// no need to keep this line in result
39+
line = ""
3840
} else if strings.HasPrefix(line, "-- sqlc:ignore") {
3941
ignoring = true
40-
} else if ignoring {
42+
}
43+
44+
if ignoring {
4145
// make this line empty, so that errors are still reported on the
4246
// correct line
4347
line = ""

internal/migrations/migrations_test.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,34 @@ const inputGoose = `
1010
-- +goose Up
1111
ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
1212
13+
-- sqlc:ignore
14+
CREATE TABLE countries (id int);
15+
CREATE TABLE people (id int);
16+
-- sqlc:ignore
17+
1318
-- +goose Down
1419
ALTER TABLE archived_jobs DROP COLUMN expires_at;
1520
`
1621

1722
const outputGoose = `
1823
-- +goose Up
1924
ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
25+
26+
27+
28+
29+
2030
`
2131

2232
const inputMigrate = `
2333
-- +migrate Up
2434
-- SQL in section 'Up' is executed when this migration is applied
2535
CREATE TABLE people (id int);
2636
37+
-- sqlc:ignore
38+
INVALID SYNTAX HERE IS OK, WE SHOULD IGNORE THIS
39+
-- sqlc:ignore end
40+
2741
-- +migrate Down
2842
-- SQL section 'Down' is executed when this migration is rolled back
2943
DROP TABLE people;
@@ -33,47 +47,66 @@ const outputMigrate = `
3347
-- +migrate Up
3448
-- SQL in section 'Up' is executed when this migration is applied
3549
CREATE TABLE people (id int);
50+
51+
52+
53+
3654
`
3755

3856
const inputTern = `
57+
-- sqlc:ignore
58+
As first row also ok, all contents after should be processed
59+
-- sqlc:ignore end
3960
-- Write your migrate up statements here
4061
ALTER TABLE todo RENAME COLUMN done TO is_done;
4162
---- create above / drop below ----
4263
ALTER TABLE todo RENAME COLUMN is_done TO done;
4364
`
4465

4566
const outputTern = `
67+
68+
69+
4670
-- Write your migrate up statements here
4771
ALTER TABLE todo RENAME COLUMN done TO is_done;`
4872

4973
const inputDbmate = `
5074
-- migrate:up
5175
CREATE TABLE foo (bar int);
76+
-- sqlc:ignore
77+
In up section
78+
-- sqlc:ignore end
5279
-- migrate:down
53-
DROP TABLE foo;`
80+
DROP TABLE foo;
81+
-- sqlc:ignore
82+
In down section
83+
-- sqlc:ignore end`
5484

5585
const outputDbmate = `
5686
-- migrate:up
57-
CREATE TABLE foo (bar int);`
87+
CREATE TABLE foo (bar int);
88+
89+
90+
`
5891

59-
func TestRemoveRollback(t *testing.T) {
60-
if diff := cmp.Diff(outputGoose, RemoveRollbackStatements(inputGoose)); diff != "" {
92+
func TestRemoveIgnored(t *testing.T) {
93+
if diff := cmp.Diff(outputGoose, RemoveIgnoredStatements(inputGoose)); diff != "" {
6194
t.Errorf("goose migration mismatch:\n%s", diff)
6295
}
63-
if diff := cmp.Diff(outputMigrate, RemoveRollbackStatements(inputMigrate)); diff != "" {
96+
if diff := cmp.Diff(outputMigrate, RemoveIgnoredStatements(inputMigrate)); diff != "" {
6497
t.Errorf("sql-migrate migration mismatch:\n%s", diff)
6598
}
66-
if diff := cmp.Diff(outputTern, RemoveRollbackStatements(inputTern)); diff != "" {
99+
if diff := cmp.Diff(outputTern, RemoveIgnoredStatements(inputTern)); diff != "" {
67100
t.Errorf("tern migration mismatch:\n%s", diff)
68101
}
69-
if diff := cmp.Diff(outputDbmate, RemoveRollbackStatements(inputDbmate)); diff != "" {
102+
if diff := cmp.Diff(outputDbmate, RemoveIgnoredStatements(inputDbmate)); diff != "" {
70103
t.Errorf("dbmate migration mismatch:\n%s", diff)
71104
}
72105
}
73106

74107
func TestRemoveGolangMigrateRollback(t *testing.T) {
75108
filenames := map[string]bool{
76-
// make sure we let through golang-migrate files that aren't rollbacks
109+
// make sure we let through golang-migrate files that aren't ignored
77110
"migrations/1.up.sql": false,
78111
// make sure we let through other sql files
79112
"migrations/2.sql": false,

internal/sqltest/local/mysql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func MySQL(t *testing.T, migrations []string) string {
5353
if err != nil {
5454
t.Fatal(err)
5555
}
56-
seed = append(seed, migrate.RemoveRollbackStatements(string(blob)))
56+
seed = append(seed, migrate.RemoveIgnoredStatements(string(blob)))
5757
}
5858

5959
cfg, err := mysql.ParseDSN(dburi)

internal/sqltest/local/postgres.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func postgreSQL(t *testing.T, migrations []string, rw bool) string {
5959
t.Fatal(err)
6060
}
6161
h.Write(blob)
62-
seed = append(seed, migrate.RemoveRollbackStatements(string(blob)))
62+
seed = append(seed, migrate.RemoveIgnoredStatements(string(blob)))
6363
}
6464

6565
var name string

0 commit comments

Comments
 (0)