diff --git a/internal/sqlparser/parser.go b/internal/sqlparser/parser.go index 5de637ccf..5e6c67503 100644 --- a/internal/sqlparser/parser.go +++ b/internal/sqlparser/parser.go @@ -258,14 +258,9 @@ func missingSemicolonError(state parserState, direction Direction, s string) err ) } -// cleanupStatement attempts to find the last semicolon and trims -// the remaining chars from the input string. This is useful for cleaning -// up a statement containing trailing comments or empty lines. +// cleanupStatement trims whitespace from the given statement. func cleanupStatement(input string) string { - if n := strings.LastIndex(input, ";"); n > 0 { - return input[:n+1] - } - return input + return strings.TrimSpace(input) } // Checks the line to see if the line has a statement-ending semicolon diff --git a/internal/sqlparser/parser_test.go b/internal/sqlparser/parser_test.go index 0a7b85a20..a98c51635 100644 --- a/internal/sqlparser/parser_test.go +++ b/internal/sqlparser/parser_test.go @@ -396,6 +396,7 @@ func TestValidUp(t *testing.T) { {Name: "test06", StatementsCount: 5}, {Name: "test07", StatementsCount: 1}, {Name: "test08", StatementsCount: 6}, + {Name: "test09", StatementsCount: 1}, } for _, tc := range tests { path := filepath.Join("testdata", "valid-up", tc.Name) diff --git a/internal/sqlparser/testdata/valid-up/test03/01.golden.sql b/internal/sqlparser/testdata/valid-up/test03/01.golden.sql index ca8f5afa9..e9fd6063a 100644 --- a/internal/sqlparser/testdata/valid-up/test03/01.golden.sql +++ b/internal/sqlparser/testdata/valid-up/test03/01.golden.sql @@ -29,4 +29,5 @@ BEGIN EXECUTE a_output; END; -' LANGUAGE 'plpgsql'; \ No newline at end of file +' LANGUAGE 'plpgsql'; -- This comment WILL BE preserved. +-- And so will this one. \ No newline at end of file diff --git a/internal/sqlparser/testdata/valid-up/test03/input.sql b/internal/sqlparser/testdata/valid-up/test03/input.sql index b4cc36af6..c325f7d8a 100644 --- a/internal/sqlparser/testdata/valid-up/test03/input.sql +++ b/internal/sqlparser/testdata/valid-up/test03/input.sql @@ -31,8 +31,8 @@ BEGIN EXECUTE a_output; END; -' LANGUAGE 'plpgsql'; -- This comment will NOT be preserved. --- And neither will this one. +' LANGUAGE 'plpgsql'; -- This comment WILL BE preserved. +-- And so will this one. -- +goose StatementEnd -- +goose Down diff --git a/internal/sqlparser/testdata/valid-up/test06/04.golden.sql b/internal/sqlparser/testdata/valid-up/test06/04.golden.sql index b72ddb06f..1e441538e 100644 --- a/internal/sqlparser/testdata/valid-up/test06/04.golden.sql +++ b/internal/sqlparser/testdata/valid-up/test06/04.golden.sql @@ -9,4 +9,7 @@ BEGIN -- technology was successful RETURN 1; END; -$$ LANGUAGE plpgsql; \ No newline at end of file +$$ LANGUAGE plpgsql; + +-- 3 this comment WILL BE preserved + -- 4 this comment WILL BE preserved \ No newline at end of file diff --git a/internal/sqlparser/testdata/valid-up/test06/input.sql b/internal/sqlparser/testdata/valid-up/test06/input.sql index 520a0e753..f671fd184 100644 --- a/internal/sqlparser/testdata/valid-up/test06/input.sql +++ b/internal/sqlparser/testdata/valid-up/test06/input.sql @@ -43,8 +43,8 @@ BEGIN END; $$ LANGUAGE plpgsql; --- 3 this comment will NOT be preserved - -- 4 this comment will NOT be preserved +-- 3 this comment WILL BE preserved + -- 4 this comment WILL BE preserved -- +goose StatementEnd diff --git a/internal/sqlparser/testdata/valid-up/test09/01.golden.sql b/internal/sqlparser/testdata/valid-up/test09/01.golden.sql new file mode 100644 index 000000000..3c1664b47 --- /dev/null +++ b/internal/sqlparser/testdata/valid-up/test09/01.golden.sql @@ -0,0 +1,2 @@ +create table t ( id int ); +update rows set value = now() -- missing semicolon. valid statement because wrapped in goose annotation, but will fail when executed. \ No newline at end of file diff --git a/internal/sqlparser/testdata/valid-up/test09/input.sql b/internal/sqlparser/testdata/valid-up/test09/input.sql new file mode 100644 index 000000000..affc8afb9 --- /dev/null +++ b/internal/sqlparser/testdata/valid-up/test09/input.sql @@ -0,0 +1,8 @@ +-- +goose Up +-- +goose StatementBegin +create table t ( id int ); +update rows set value = now() -- missing semicolon. valid statement because wrapped in goose annotation, but will fail when executed. +-- +goose StatementEnd + +-- +goose Down +DROP TABLE IF EXISTS t;