Skip to content

Commit 540970f

Browse files
sgielenkyleconroy
authored andcommitted
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. Add support for ignoring DDL statements using `// sqlc:ignore`.
1 parent 0b952b4 commit 540970f

File tree

8 files changed

+73
-21
lines changed

8 files changed

+73
-21
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
now := time.Now().UTC().UnixNano()

internal/cmd/verify.go

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

108108
var codegen plugin.GenerateRequest

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: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,48 @@ 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+
// no need to keep this line in result
39+
line = ""
40+
} else if strings.HasPrefix(line, "-- sqlc:ignore") {
41+
ignoring = true
42+
}
43+
44+
if ignoring {
45+
// make this line empty, so that errors are still reported on the
46+
// correct line
47+
line = ""
48+
}
49+
lines = append(lines, line)
3150
}
3251
return strings.Join(lines, "\n")
3352
}

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)