From 489d7d173c520c50d1ac2b9c2c2de80230fbd709 Mon Sep 17 00:00:00 2001 From: Daichi Hirata Date: Sat, 20 Aug 2022 02:21:54 +0900 Subject: [PATCH] Fix parsing failure if comments were included --- internal/hammer/ddl.go | 14 ++++++++------ internal/hammer/ddl_test.go | 8 ++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/internal/hammer/ddl.go b/internal/hammer/ddl.go index 2ddc7df..cf5173d 100644 --- a/internal/hammer/ddl.go +++ b/internal/hammer/ddl.go @@ -24,17 +24,19 @@ func (d *DDL) AppendDDL(ddl DDL) { } func ParseDDL(uri, schema string, option *DDLOption) (DDL, error) { - trimed := strings.ReplaceAll(schema, "\n", "") - var lines []string - for _, line := range strings.Split(trimed, ";") { - if option.IgnoreChangeStreams && strings.HasPrefix(line, "CREATE CHANGE STREAM") { + for _, line := range strings.Split(schema, ";") { + trimed := strings.TrimSpace(line) + if trimed == "" { + continue + } + if option.IgnoreChangeStreams && strings.HasPrefix(trimed, "CREATE CHANGE STREAM") { continue } - lines = append(lines, line) + lines = append(lines, line+";") } - ddl, err := spansql.ParseDDL(uri, strings.Join(lines, ";")) + ddl, err := spansql.ParseDDL(uri, strings.Join(lines, "")) if err != nil { return DDL{}, fmt.Errorf("%s failed to parse ddl: %s", uri, err) } diff --git a/internal/hammer/ddl_test.go b/internal/hammer/ddl_test.go index c81b97a..7fd73dd 100644 --- a/internal/hammer/ddl_test.go +++ b/internal/hammer/ddl_test.go @@ -21,8 +21,8 @@ func TestParseDDL(t *testing.T) { { name: "Failed to parse change streams", schema: `CREATE TABLE Users ( - UserID STRING(10) NOT NULL, - Name STRING(10) NOT NULL, + UserID STRING(10) NOT NULL, -- comment + Name STRING(10) NOT NULL, -- comment ) PRIMARY KEY(UserID); CREATE CHANGE STREAM LongerDataRetention @@ -36,8 +36,8 @@ CREATE CHANGE STREAM LongerDataRetention { name: "Ignore change streams", schema: `CREATE TABLE Users ( - UserID STRING(10) NOT NULL, - Name STRING(10) NOT NULL, + UserID STRING(10) NOT NULL, -- comment + Name STRING(10) NOT NULL, -- comment ) PRIMARY KEY(UserID); CREATE CHANGE STREAM LongerDataRetention