Skip to content

Commit

Permalink
fix(sqlparser): avoid trimming on last semicolon (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman authored Aug 11, 2023
1 parent 906a5b3 commit d67f26c
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
9 changes: 2 additions & 7 deletions internal/sqlparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/sqlparser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion internal/sqlparser/testdata/valid-up/test03/01.golden.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ BEGIN
EXECUTE a_output;
END;
' LANGUAGE 'plpgsql';
' LANGUAGE 'plpgsql'; -- This comment WILL BE preserved.
-- And so will this one.
4 changes: 2 additions & 2 deletions internal/sqlparser/testdata/valid-up/test03/input.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion internal/sqlparser/testdata/valid-up/test06/04.golden.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ BEGIN
-- technology was successful
RETURN 1;
END;
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql;

-- 3 this comment WILL BE preserved
-- 4 this comment WILL BE preserved
4 changes: 2 additions & 2 deletions internal/sqlparser/testdata/valid-up/test06/input.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/sqlparser/testdata/valid-up/test09/01.golden.sql
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 8 additions & 0 deletions internal/sqlparser/testdata/valid-up/test09/input.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit d67f26c

Please sign in to comment.