Skip to content

Commit 2e5cea8

Browse files
committed
add ToMigrationStatements helper
1 parent bc9520c commit 2e5cea8

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
"CREATE TABLE test (id INT64)",
3+
"CREATE PROPERTY GRAPH SocialGraph NODE TABLES (Person) EDGE TABLES (Knows)"
4+
]

database/spanner/ddl/ddl_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,19 @@ func TestBasic(t *testing.T) {
3030

3131
g.Snapshot("ddl-string", ddl.String())
3232
}
33+
34+
func TestToMigrationStatements(t *testing.T) {
35+
g := got.T(t)
36+
37+
list, err := ToMigrationStatements("", `
38+
-- Comment
39+
CREATE TABLE test(id INT64);
40+
41+
CREATE PROPERTY GRAPH SocialGraph
42+
NODE TABLES (Person)
43+
EDGE TABLES (Knows);
44+
`)
45+
g.E(err)
46+
47+
g.Snapshot("list", list)
48+
}

database/spanner/ddl/format.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ import (
66
)
77

88
func ToMigrationStatements(path, ddl string) ([]string, error) {
9-
_, err := parser.ParseString(path, ddl)
9+
parsed, err := parser.ParseString(path, ddl)
1010
if err != nil {
1111
return nil, fmt.Errorf("failed to parse DDL: %w", err)
1212
}
1313

14-
return nil, nil
14+
list := []string{}
15+
16+
for _, statement := range parsed.Statements {
17+
list = append(list, strings.TrimRight(statement.String(), ";"))
18+
}
19+
20+
return list, nil
1521
}
1622

1723
func (d *SpannerDDL) String() string {

0 commit comments

Comments
 (0)