Skip to content

Commit

Permalink
feat: extract databases in Kotlin schema
Browse files Browse the repository at this point in the history
  • Loading branch information
worstell committed Jan 12, 2024
1 parent 8f52e07 commit 9a345a7
Show file tree
Hide file tree
Showing 13 changed files with 816 additions and 329 deletions.
52 changes: 52 additions & 0 deletions backend/schema/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package schema

import (
"fmt"
"strings"

"google.golang.org/protobuf/proto"

schemapb "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
)

type Database struct {
Pos Position `parser:"" protobuf:"1,optional"`

Comments []string `parser:"@Comment*" protobuf:"3"`
Name string `parser:"'database' @Ident" protobuf:"2"`
}

var _ Decl = (*Database)(nil)

// schemaDecl implements Decl
func (*Database) schemaDecl() {}
func (d *Database) schemaChildren() []Node { return nil }
func (d *Database) String() string {
w := &strings.Builder{}
fmt.Fprint(w, encodeComments(d.Comments))
fmt.Fprintf(w, "database %s", d.Name)
return w.String()
}

func (d *Database) ToProto() proto.Message {
return &schemapb.Database{
Pos: posToProto(d.Pos),
Name: d.Name,
Comments: d.Comments,
}
}
func DatabaseToSchema(s *schemapb.Database) *Database {
return &Database{
Pos: posFromProto(s.Pos),
Name: s.Name,
Comments: s.Comments,
}
}

func databaseListToSchema(s []*schemapb.Database) []*Database {
var out []*Database
for _, n := range s {
out = append(out, DatabaseToSchema(n))
}
return out
}
4 changes: 2 additions & 2 deletions backend/schema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ func nodeToJSSchema(node Node, dataRefs map[DataRef]bool) *jsonschema.Schema {
{TypeObject: &jsonschema.Schema{Type: &jsonschema.Type{SimpleTypes: &null}}},
}}

case Decl, *Field, Metadata, *MetadataCalls, *MetadataIngress,
case Decl, *Field, Metadata, *MetadataCalls, *MetadataDatabases, *MetadataIngress,
IngressPathComponent, *IngressPathLiteral, *IngressPathParameter, *Module,
*Schema, Type, *Verb, *VerbRef, *SourceRef, *SinkRef:
*Schema, Type, *Database, *Verb, *VerbRef, *SourceRef, *SinkRef:
panic(fmt.Sprintf("unsupported node type %T", node))

default:
Expand Down
55 changes: 55 additions & 0 deletions backend/schema/metadatadatabases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package schema

import (
"fmt"
"strings"

"google.golang.org/protobuf/proto"

schemapb "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
)

type MetadataDatabases struct {
Pos Position `parser:"" protobuf:"1,optional"`

Calls []*Database `parser:"'database' 'calls' @@ (',' @@)*" protobuf:"2"`
}

var _ Metadata = (*MetadataDatabases)(nil)

func (m *MetadataDatabases) String() string {
out := &strings.Builder{}
fmt.Fprint(out, "database calls ")
w := 6
for i, call := range m.Calls {
if i > 0 {
fmt.Fprint(out, ", ")
w += 2
}
str := call.String()
if w+len(str) > 70 {
w = 6
fmt.Fprint(out, "\n ")
}
w += len(str)
fmt.Fprint(out, str)
}
fmt.Fprintln(out)
return out.String()
}

func (m *MetadataDatabases) schemaChildren() []Node {
out := make([]Node, 0, len(m.Calls))
for _, ref := range m.Calls {
out = append(out, ref)
}
return out
}
func (*MetadataDatabases) schemaMetadata() {}

func (m *MetadataDatabases) ToProto() proto.Message {
return &schemapb.MetadataDatabases{
Pos: posToProto(m.Pos),
Calls: nodeListToProto[*schemapb.Database](m.Calls),
}
}
7 changes: 7 additions & 0 deletions backend/schema/normalise.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func Normalise[T Node](n T) T {
c.Fields = normaliseSlice(c.Fields)
c.Metadata = normaliseSlice(c.Metadata)

case *Database:
c.Pos = zero

case *DataRef:
c.Pos = zero

Expand Down Expand Up @@ -77,6 +80,10 @@ func Normalise[T Node](n T) T {
c.Pos = zero
c.Calls = normaliseSlice(c.Calls)

case *MetadataDatabases:
c.Pos = zero
c.Calls = normaliseSlice(c.Calls)

case *MetadataIngress:
c.Pos = zero
c.Path = normaliseSlice(c.Path)
Expand Down
4 changes: 2 additions & 2 deletions backend/schema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
)

var (
declUnion = []Decl{&Data{}, &Verb{}}
declUnion = []Decl{&Data{}, &Verb{}, &Database{}}
nonOptionalTypeUnion = []Type{
&Int{}, &Float{}, &String{}, &Bytes{}, &Bool{}, &Time{}, &Array{},
&Map{}, &DataRef{}, &Unit{},
}
typeUnion = append(nonOptionalTypeUnion, &Optional{})
metadataUnion = []Metadata{&MetadataCalls{}, &MetadataIngress{}}
metadataUnion = []Metadata{&MetadataCalls{}, &MetadataIngress{}, &MetadataDatabases{}}
ingressUnion = []IngressPathComponent{&IngressPathLiteral{}, &IngressPathParameter{}}

// Used by protobuf generation.
Expand Down
8 changes: 8 additions & 0 deletions backend/schema/protobuf_dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func declListToSchema(s []*schemapb.Decl) []Decl {
out = append(out, VerbToSchema(n.Verb))
case *schemapb.Decl_Data:
out = append(out, DataToSchema(n.Data))
case *schemapb.Decl_Database:
out = append(out, DatabaseToSchema(n.Database))
}
}
return out
Expand Down Expand Up @@ -74,6 +76,12 @@ func metadataToSchema(s *schemapb.Metadata) Metadata {
Calls: verbRefListToSchema(s.Calls.Calls),
}

case *schemapb.Metadata_Databases:
return &MetadataDatabases{
Pos: posFromProto(s.Databases.Pos),
Calls: databaseListToSchema(s.Databases.Calls),
}

case *schemapb.Metadata_Ingress:
return &MetadataIngress{
Pos: posFromProto(s.Ingress.Pos),
Expand Down
5 changes: 5 additions & 0 deletions backend/schema/protobuf_enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func declListToProto(nodes []Decl) []*schemapb.Decl {
v = &schemapb.Decl_Verb{Verb: n.ToProto().(*schemapb.Verb)}
case *Data:
v = &schemapb.Decl_Data{Data: n.ToProto().(*schemapb.Data)}
case *Database:
v = &schemapb.Decl_Database{Database: n.ToProto().(*schemapb.Database)}
}
out[i] = &schemapb.Decl{Value: v}
}
Expand All @@ -44,6 +46,9 @@ func metadataListToProto(nodes []Metadata) []*schemapb.Metadata {
case *MetadataCalls:
v = &schemapb.Metadata_Calls{Calls: n.ToProto().(*schemapb.MetadataCalls)}

case *MetadataDatabases:
v = &schemapb.Metadata_Databases{Databases: n.ToProto().(*schemapb.MetadataDatabases)}

case *MetadataIngress:
v = &schemapb.Metadata_Ingress{Ingress: n.ToProto().(*schemapb.MetadataIngress)}

Expand Down
4 changes: 2 additions & 2 deletions backend/schema/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ func ValidateModule(module *Module) error {
}
}

case *Array, *Bool, *DataRef, *Field, *Float, *Int,
case *Array, *Bool, *DataRef, *Database, *Field, *Float, *Int,
*Time, *Map, *Module, *Schema, *String, *Bytes, *VerbRef,
*MetadataCalls, *MetadataIngress, IngressPathComponent,
*MetadataCalls, *MetadataDatabases, *MetadataIngress, IngressPathComponent,
*IngressPathLiteral, *IngressPathParameter, *Optional,
*SourceRef, *SinkRef, *Unit:
case Type, Metadata, Decl: // Union sql.
Expand Down
106 changes: 106 additions & 0 deletions frontend/src/protos/xyz/block/ftl/v1/schema/schema_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ func TestIntegration(t *testing.T) {
}),
}},
{name: "UseKotlinDbConn", assertions: assertions{
setUpKotlinModuleDb(filepath.Join(modulesDir, "ftl-module-echo3")),
setUpModuleDb(filepath.Join(modulesDir, "ftl-module-echo3")),
run(".", "ftl", "deploy", filepath.Join(modulesDir, "ftl-module-echo3")),
call("dbtest", "create", obj{"data": "Hello"}, func(t testing.TB, resp obj) {}),
validateKotlinModuleDb(),
validateModuleDb(),
}},
{name: "SchemaGenerateJS", assertions: assertions{
run(".", "ftl", "schema", "generate", "integration/testdata/schema-generate", "build/schema-generate"),
Expand Down Expand Up @@ -230,7 +230,7 @@ func call[Resp any](module, verb string, req obj, onResponse func(t testing.TB,
}
}

func setUpKotlinModuleDb(dir string) assertion {
func setUpModuleDb(dir string) assertion {
os.Setenv("FTL_POSTGRES_DSN_dbtest_testdb", "postgres://postgres:secret@localhost:54320/testdb?sslmode=disable")
return func(t testing.TB, ic itContext) error {
db, err := sql.Open("pgx", "postgres://postgres:secret@localhost:54320/ftl?sslmode=disable")
Expand Down Expand Up @@ -265,7 +265,7 @@ func setUpKotlinModuleDb(dir string) assertion {
}
}

func validateKotlinModuleDb() assertion {
func validateModuleDb() assertion {
return func(t testing.TB, ic itContext) error {
db, err := sql.Open("pgx", "postgres://postgres:secret@localhost:54320/testdb?sslmode=disable")
assert.NoError(t, err)
Expand Down
Loading

0 comments on commit 9a345a7

Please sign in to comment.