mirror of https://github.com/pressly/goose.git
Fix SQL parser errors
parent
fff58a44df
commit
3836c78d69
|
@ -80,6 +80,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
|
|||
default:
|
||||
return nil, false, errors.Errorf("duplicate '-- +goose Up' annotations; stateMachine=%v, see https://github.com/pressly/goose#sql-migrations", stateMachine)
|
||||
}
|
||||
continue
|
||||
|
||||
case "+goose Down":
|
||||
switch stateMachine.Get() {
|
||||
|
@ -88,6 +89,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
|
|||
default:
|
||||
return nil, false, errors.Errorf("must start with '-- +goose Up' annotation, stateMachine=%v, see https://github.com/pressly/goose#sql-migrations", stateMachine)
|
||||
}
|
||||
continue
|
||||
|
||||
case "+goose StatementBegin":
|
||||
switch stateMachine.Get() {
|
||||
|
@ -98,6 +100,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
|
|||
default:
|
||||
return nil, false, errors.Errorf("'-- +goose StatementBegin' must be defined after '-- +goose Up' or '-- +goose Down' annotation, stateMachine=%v, see https://github.com/pressly/goose#sql-migrations", stateMachine)
|
||||
}
|
||||
continue
|
||||
|
||||
case "+goose StatementEnd":
|
||||
switch stateMachine.Get() {
|
||||
|
@ -111,10 +114,12 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
|
|||
|
||||
case "+goose NO TRANSACTION":
|
||||
useTx = false
|
||||
continue
|
||||
|
||||
default:
|
||||
// Ignore comments.
|
||||
verboseInfo("=> ignore comment")
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ func TestSemicolons(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSplitStatements(t *testing.T) {
|
||||
//SetVerbose(true)
|
||||
// SetVerbose(true)
|
||||
|
||||
type testData struct {
|
||||
sql string
|
||||
|
@ -41,24 +41,9 @@ func TestSplitStatements(t *testing.T) {
|
|||
}
|
||||
|
||||
tt := []testData{
|
||||
{sql: `-- +goose Up
|
||||
CREATE TABLE post (
|
||||
id int NOT NULL,
|
||||
title text,
|
||||
body text,
|
||||
PRIMARY KEY(id)
|
||||
); SELECT 1;
|
||||
|
||||
-- comment
|
||||
SELECT 2;
|
||||
SELECT 3; SELECT 3;
|
||||
SELECT 4;
|
||||
|
||||
-- +goose Down
|
||||
-- comment
|
||||
DROP TABLE post; SELECT 1; -- comment
|
||||
`, up: 4, down: 1},
|
||||
|
||||
{sql: multilineSQL, up: 4, down: 1},
|
||||
{sql: emptySQL, up: 0, down: 0},
|
||||
{sql: emptySQL2, up: 0, down: 0},
|
||||
{sql: functxt, up: 2, down: 2},
|
||||
}
|
||||
|
||||
|
@ -116,18 +101,35 @@ func TestParsingErrors(t *testing.T) {
|
|||
statementBeginNoStatementEnd,
|
||||
unfinishedSQL,
|
||||
noUpDownAnnotations,
|
||||
emptySQL,
|
||||
multiUpDown,
|
||||
downFirst,
|
||||
}
|
||||
for _, sql := range tt {
|
||||
for i, sql := range tt {
|
||||
_, _, err := parseSQLMigration(strings.NewReader(sql), true)
|
||||
if err == nil {
|
||||
t.Errorf("expected error on %q", sql)
|
||||
t.Errorf("expected error on tt[%v] %q", i, sql)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var multilineSQL = `-- +goose Up
|
||||
CREATE TABLE post (
|
||||
id int NOT NULL,
|
||||
title text,
|
||||
body text,
|
||||
PRIMARY KEY(id)
|
||||
); SELECT 1;
|
||||
|
||||
-- comment
|
||||
SELECT 2;
|
||||
SELECT 3; SELECT 3;
|
||||
SELECT 4;
|
||||
|
||||
-- +goose Down
|
||||
-- comment
|
||||
DROP TABLE post; SELECT 1; -- comment
|
||||
`
|
||||
|
||||
var functxt = `-- +goose Up
|
||||
CREATE TABLE IF NOT EXISTS histories (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
|
@ -232,6 +234,16 @@ ALTER TABLE post
|
|||
var emptySQL = `-- +goose Up
|
||||
-- This is just a comment`
|
||||
|
||||
var emptySQL2 = `
|
||||
|
||||
-- comment
|
||||
-- +goose Up
|
||||
|
||||
-- comment
|
||||
-- +goose Down
|
||||
|
||||
`
|
||||
|
||||
var noUpDownAnnotations = `
|
||||
CREATE TABLE post (
|
||||
id int NOT NULL,
|
||||
|
|
Loading…
Reference in New Issue