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