Skip to content

Commit

Permalink
Merge pull request #43 from daichirata/fix/parse-ddl
Browse files Browse the repository at this point in the history
Fix parsing failure if comments were included
  • Loading branch information
daichirata authored Aug 19, 2022
2 parents 78167e3 + 489d7d1 commit ed095e0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 8 additions & 6 deletions internal/hammer/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/hammer/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ed095e0

Please sign in to comment.