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
7 changes: 6 additions & 1 deletion internal/source/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ func createTable(src *model.Source, text string, pos model.Pos) {
name = name[:p]
}
schema, table := splitQualified(name)
body, rest, ok := ddl.Body(text[strings.Index(text, w[i]):])
// The column list is the first parenthesis after TABLE. Searching for
// the name's word instead panicked when the name was glued to a paren
// that opens a multi-line list ("reactions(\n userid ..."): the word
// splitter folds that list's whitespace, so the word is not in the text
// and the index is -1. Found on Mattermost's migrations.
body, rest, ok := ddl.Body(text[strings.Index(strings.ToUpper(text), "TABLE")+len("TABLE"):])
if !ok {
return
}
Expand Down
16 changes: 16 additions & 0 deletions internal/source/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,19 @@ func formatSlice(s []string) string {
}
return out + "]"
}

func TestANameGluedToAMultiLineColumnListDoesNotPanic(t *testing.T) {
// Mattermost 000016_create_reactions.up.sql, as written.
src := &model.Source{}
text := "CREATE TABLE IF NOT EXISTS reactions(\n userid VARCHAR(26) NOT NULL,\n postid VARCHAR(26) NOT NULL\n);"
if err := Apply(src, "000016_create_reactions.up.sql", text); err != nil {
t.Fatal(err)
}
r := src.Table("public", "reactions")
if r == nil || len(r.Columns) != 2 || r.Columns[0].Name != "userid" || r.Columns[1].Name != "postid" {
t.Fatalf("public.reactions = %+v", r)
}
if r.Columns[1].Pos.Line != 3 {
t.Errorf("postid line = %d, want 3", r.Columns[1].Pos.Line)
}
}