Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/dbutil/dbutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TrimLeadingSQLComments(data []byte) ([]byte, error) {
// we read bytes directly for premature performance optimization
line := scanner.Bytes()

if preamble && (len(line) == 0 || bytes.Equal(line[0:2], []byte("--"))) {
if preamble && (len(line) == 0 || bytes.HasPrefix(line, []byte("--"))) {
// header section, skip this line in output buffer
continue
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/dbutil/dbutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dbutil_test

import (
"database/sql"
"strings"
"testing"

"github.com/amacneil/dbmate/v2/pkg/dbtest"
Expand Down Expand Up @@ -36,6 +37,24 @@ func TestTrimLeadingSQLComments(t *testing.T) {
require.Equal(t, "real stuff\n-- end\n", string(out))
}

func TestTrimLeadingSQLCommentsShortLineAtEOF(t *testing.T) {
// >4KB of leading comments forces bufio.Scanner to slide its buffer,
// leaving stale bytes just past the final token; a one-byte last line
// must not be misread as a comment marker.
var b strings.Builder
for b.Len() < 4090 {
b.WriteString("--\n")
}
for b.Len() < 4095 {
b.WriteString("-")
}
b.WriteString("\n-") // final line: one byte, no trailing newline

out, err := dbutil.TrimLeadingSQLComments([]byte(b.String()))
require.NoError(t, err)
require.Equal(t, "-\n", string(out))
}

func TestStripPsqlMetaCommands(t *testing.T) {
t.Run("strips restrict and unrestrict", func(t *testing.T) {
in := "\\restrict dbmate\n" +
Expand Down