fix(sqlparser): avoid trimming on last semicolon (#581)

pull/584/head
Michael Fridman 2023-08-11 08:31:33 -04:00 committed by GitHub
parent 906a5b38bc
commit d67f26cb2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 23 additions and 13 deletions

View File

@ -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

View File

@ -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)

View File

@ -29,4 +29,5 @@ BEGIN
EXECUTE a_output;
END;
' LANGUAGE 'plpgsql';
' LANGUAGE 'plpgsql'; -- This comment WILL BE preserved.
-- And so will this one.

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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;