Skip to content
Open
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
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ issues:
exclude:
# gosec: Duplicated errcheck checks
- G104
- bad syntax for struct tag pair
- struct literal uses unkeyed fields
- bad syntax for struct tag key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"A B;\nA;\nA (B C);\nA \"B\";\nA <B , C>;\nA B , A (B (C D)) E\n"
133 changes: 133 additions & 0 deletions database/spanner/ddl/.got/snapshots/TestBasic/ddl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"Statements": [
{
"Expression": {
"Values": [
{
"Component": ["A", "B"],
"Braced": null,
"Bracketed": null
}
]
},
"End": ";"
},
{
"Expression": {
"Values": [
{
"Component": ["A"],
"Braced": null,
"Bracketed": null
}
]
},
"End": ";"
},
{
"Expression": {
"Values": [
{
"Component": ["A"],
"Braced": null,
"Bracketed": null
},
{
"Component": null,
"Braced": {
"Values": [
{
"Component": ["B", "C"],
"Braced": null,
"Bracketed": null
}
]
},
"Bracketed": null
}
]
},
"End": ";"
},
{
"Expression": {
"Values": [
{
"Component": ["A", "\"B\""],
"Braced": null,
"Bracketed": null
}
]
},
"End": ";"
},
{
"Expression": {
"Values": [
{
"Component": ["A"],
"Braced": null,
"Bracketed": null
},
{
"Component": null,
"Braced": null,
"Bracketed": {
"Values": [
{
"Component": ["B", ",", "C"],
"Braced": null,
"Bracketed": null
}
]
}
}
]
},
"End": ";"
},
{
"Expression": {
"Values": [
{
"Component": ["A", "B", ",", "A"],
"Braced": null,
"Bracketed": null
},
{
"Component": null,
"Braced": {
"Values": [
{
"Component": ["B"],
"Braced": null,
"Bracketed": null
},
{
"Component": null,
"Braced": {
"Values": [
{
"Component": ["C", "D"],
"Braced": null,
"Bracketed": null
}
]
},
"Bracketed": null
}
]
},
"Bracketed": null
},
{
"Component": ["E"],
"Braced": null,
"Bracketed": null
}
]
},
"End": ""
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"CREATE TABLE test (id INT64)",
"CREATE PROPERTY GRAPH SocialGraph NODE TABLES (Person) EDGE TABLES (Knows)"
]
48 changes: 48 additions & 0 deletions database/spanner/ddl/ddl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ddl

import (
"testing"

"github.com/ysmood/got"
)

func TestBasic(t *testing.T) {
g := got.T(t)

ddl, err := parser.ParseString("", `
-- Comment
A B;
A;
A (B C); /*
Multi-line
comment
*/
A "B";
A <B, C>;
A -- Comment
B,
-- Comment
A (B (C D)) E
`)
g.E(err)

g.Snapshot("ddl", ddl)

g.Snapshot("ddl-string", ddl.String())
}

func TestToMigrationStatements(t *testing.T) {
g := got.T(t)

list, err := ToMigrationStatements("", `
-- Comment
CREATE TABLE test(id INT64);

CREATE PROPERTY GRAPH SocialGraph
NODE TABLES (Person)
EDGE TABLES (Knows);
`)
g.E(err)

g.Snapshot("list", list)
}
59 changes: 59 additions & 0 deletions database/spanner/ddl/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ddl

import (
"fmt"
"strings"
)

func ToMigrationStatements(path, ddl string) ([]string, error) {
parsed, err := parser.ParseString(path, ddl)
if err != nil {
return nil, fmt.Errorf("failed to parse DDL: %w", err)
}

list := []string{}

for _, statement := range parsed.Statements {
list = append(list, strings.TrimRight(statement.String(), ";"))
}

return list, nil
}

func (d *SpannerDDL) String() string {
out := ""

for _, s := range d.Statements {
out += s.String() + "\n"
}

return out
}

func (s *Statement) String() string {
return s.Expression.String() + s.End
}

func (e *Expression) String() string {
list := []string{}

for _, v := range e.Values {
list = append(list, v.String())
}

return strings.Join(list, " ")
}

func (v *Value) String() string {
out := ""

if v.Braced != nil {
out += "(" + v.Braced.String() + ")"
} else if v.Bracketed != nil {
out += "<" + v.Bracketed.String() + ">"
} else {
out += strings.Join(v.Component, " ")
}

return out
}
46 changes: 46 additions & 0 deletions database/spanner/ddl/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ddl

import (
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)

// SpannerDDL only parses the components that are relevant for the migration tool.
// Main objectives:
// - Remove comments: spanner does allow comments in DDL.
// - Split DDL into statements: spanner requires the migration request to be a list of statements, not a single DDL file.
// Also each request should not exceed 10 statements.
type SpannerDDL struct {
Statements []*Statement `@@*`
}

type Statement struct {
Expression *Expression `@@`
End string `@";"?`
}

type Expression struct {
Values []*Value `@@+`
}

type Value struct {
Component []string ` @(Ident | String | Comma)+`
Braced *Expression `| "(" @@ ")"`
Bracketed *Expression `| "<" @@ ">"`
}

var parser = participle.MustBuild[SpannerDDL](
participle.Lexer(lexer.MustSimple([]lexer.SimpleRule{
{"SingleLineComment", `--[^\n]*`},
{"MultiLineComment", `(?s)/\*.*?\*/`},
{"Whitespace", `\s+`},

{"StatementEnd", `;`},
{"Group", `[()<>]`},

{"Ident", `[a-zA-Z\d_]+`},
{"String", `"(?:\\.|[^"])*"`},
{"Comma", `,`},
})),
participle.Elide("SingleLineComment", "MultiLineComment", "Whitespace"),
)
Loading