Skip to content

Commit 36b57c7

Browse files
committed
feat: extract databases in Kotlin schema
1 parent 400e2db commit 36b57c7

File tree

13 files changed

+802
-321
lines changed

13 files changed

+802
-321
lines changed

backend/schema/database.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package schema
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"google.golang.org/protobuf/proto"
8+
9+
schemapb "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
10+
)
11+
12+
type Database struct {
13+
Pos Position `parser:"" protobuf:"1,optional"`
14+
15+
Comments []string `parser:"@Comment*" protobuf:"3"`
16+
Name string `parser:"'database' @Ident" protobuf:"2"`
17+
}
18+
19+
var _ Decl = (*Database)(nil)
20+
21+
// schemaDecl implements Decl
22+
func (*Database) schemaDecl() {}
23+
func (d *Database) schemaChildren() []Node { return nil }
24+
func (d *Database) String() string {
25+
w := &strings.Builder{}
26+
fmt.Fprint(w, encodeComments(d.Comments))
27+
fmt.Fprintf(w, "database %s", d.Name)
28+
return w.String()
29+
}
30+
31+
func (d *Database) ToProto() proto.Message {
32+
return &schemapb.Database{
33+
Pos: posToProto(d.Pos),
34+
Name: d.Name,
35+
Comments: d.Comments,
36+
}
37+
}
38+
func DatabaseToSchema(s *schemapb.Database) *Database {
39+
return &Database{
40+
Pos: posFromProto(s.Pos),
41+
Name: s.Name,
42+
Comments: s.Comments,
43+
}
44+
}
45+
46+
func databaseListToSchema(s []*schemapb.Database) []*Database {
47+
var out []*Database
48+
for _, n := range s {
49+
out = append(out, DatabaseToSchema(n))
50+
}
51+
return out
52+
}

backend/schema/jsonschema.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ func nodeToJSSchema(node Node, dataRefs map[DataRef]bool) *jsonschema.Schema {
122122
{TypeObject: &jsonschema.Schema{Type: &jsonschema.Type{SimpleTypes: &null}}},
123123
}}
124124

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

130130
default:

backend/schema/metadatadatabases.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package schema
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"google.golang.org/protobuf/proto"
8+
9+
schemapb "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1/schema"
10+
)
11+
12+
type MetadataDatabases struct {
13+
Pos Position `parser:"" protobuf:"1,optional"`
14+
15+
Calls []*Database `parser:"'database' 'calls' @@ (',' @@)*" protobuf:"2"`
16+
}
17+
18+
var _ Metadata = (*MetadataDatabases)(nil)
19+
20+
func (m *MetadataDatabases) String() string {
21+
out := &strings.Builder{}
22+
fmt.Fprint(out, "database calls ")
23+
w := 6
24+
for i, call := range m.Calls {
25+
if i > 0 {
26+
fmt.Fprint(out, ", ")
27+
w += 2
28+
}
29+
str := call.String()
30+
if w+len(str) > 70 {
31+
w = 6
32+
fmt.Fprint(out, "\n ")
33+
}
34+
w += len(str)
35+
fmt.Fprint(out, str)
36+
}
37+
fmt.Fprintln(out)
38+
return out.String()
39+
}
40+
41+
func (m *MetadataDatabases) schemaChildren() []Node {
42+
out := make([]Node, 0, len(m.Calls))
43+
for _, ref := range m.Calls {
44+
out = append(out, ref)
45+
}
46+
return out
47+
}
48+
func (*MetadataDatabases) schemaMetadata() {}
49+
50+
func (m *MetadataDatabases) ToProto() proto.Message {
51+
return &schemapb.MetadataDatabases{
52+
Pos: posToProto(m.Pos),
53+
Calls: nodeListToProto[*schemapb.Database](m.Calls),
54+
}
55+
}

backend/schema/normalise.go

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func Normalise[T Node](n T) T {
2929
c.Fields = normaliseSlice(c.Fields)
3030
c.Metadata = normaliseSlice(c.Metadata)
3131

32+
case *Database:
33+
c.Pos = zero
34+
3235
case *DataRef:
3336
c.Pos = zero
3437

@@ -74,6 +77,10 @@ func Normalise[T Node](n T) T {
7477
c.Pos = zero
7578
c.Calls = normaliseSlice(c.Calls)
7679

80+
case *MetadataDatabases:
81+
c.Pos = zero
82+
c.Calls = normaliseSlice(c.Calls)
83+
7784
case *MetadataIngress:
7885
c.Pos = zero
7986
c.Path = normaliseSlice(c.Path)

backend/schema/parser.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
)
1313

1414
var (
15-
declUnion = []Decl{&Data{}, &Verb{}}
15+
declUnion = []Decl{&Data{}, &Verb{}, &Database{}}
1616
nonOptionalTypeUnion = []Type{&Int{}, &Float{}, &String{}, &Bytes{}, &Bool{}, &Time{}, &Array{}, &Map{} /*&VerbRef{},*/, &DataRef{}}
1717
typeUnion = append(nonOptionalTypeUnion, &Optional{})
18-
metadataUnion = []Metadata{&MetadataCalls{}, &MetadataIngress{}}
18+
metadataUnion = []Metadata{&MetadataCalls{}, &MetadataIngress{}, &MetadataDatabases{}}
1919
ingressUnion = []IngressPathComponent{&IngressPathLiteral{}, &IngressPathParameter{}}
2020

2121
// Used by protobuf generation.

backend/schema/protobuf_dec.go

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func declListToSchema(s []*schemapb.Decl) []Decl {
2525
out = append(out, VerbToSchema(n.Verb))
2626
case *schemapb.Decl_Data:
2727
out = append(out, DataToSchema(n.Data))
28+
case *schemapb.Decl_Database:
29+
out = append(out, DatabaseToSchema(n.Database))
2830
}
2931
}
3032
return out
@@ -74,6 +76,12 @@ func metadataToSchema(s *schemapb.Metadata) Metadata {
7476
Calls: verbRefListToSchema(s.Calls.Calls),
7577
}
7678

79+
case *schemapb.Metadata_Databases:
80+
return &MetadataDatabases{
81+
Pos: posFromProto(s.Databases.Pos),
82+
Calls: databaseListToSchema(s.Databases.Calls),
83+
}
84+
7785
case *schemapb.Metadata_Ingress:
7886
return &MetadataIngress{
7987
Pos: posFromProto(s.Ingress.Pos),

backend/schema/protobuf_enc.go

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ func declListToProto(nodes []Decl) []*schemapb.Decl {
3030
v = &schemapb.Decl_Verb{Verb: n.ToProto().(*schemapb.Verb)}
3131
case *Data:
3232
v = &schemapb.Decl_Data{Data: n.ToProto().(*schemapb.Data)}
33+
case *Database:
34+
v = &schemapb.Decl_Database{Database: n.ToProto().(*schemapb.Database)}
3335
}
3436
out[i] = &schemapb.Decl{Value: v}
3537
}
@@ -44,6 +46,9 @@ func metadataListToProto(nodes []Metadata) []*schemapb.Metadata {
4446
case *MetadataCalls:
4547
v = &schemapb.Metadata_Calls{Calls: n.ToProto().(*schemapb.MetadataCalls)}
4648

49+
case *MetadataDatabases:
50+
v = &schemapb.Metadata_Databases{Databases: n.ToProto().(*schemapb.MetadataDatabases)}
51+
4752
case *MetadataIngress:
4853
v = &schemapb.Metadata_Ingress{Ingress: n.ToProto().(*schemapb.MetadataIngress)}
4954

backend/schema/validate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ func ValidateModule(module *Module) error {
211211
}
212212
}
213213

214-
case *Array, *Bool, *DataRef, *Field, *Float, *Int,
214+
case *Array, *Bool, *DataRef, *Database, *Field, *Float, *Int,
215215
*Time, *Map, *Module, *Schema, *String, *Bytes, *VerbRef,
216-
*MetadataCalls, *MetadataIngress, IngressPathComponent,
216+
*MetadataCalls, *MetadataDatabases, *MetadataIngress, IngressPathComponent,
217217
*IngressPathLiteral, *IngressPathParameter, *Optional,
218218
*SourceRef, *SinkRef:
219219
case Type, Metadata, Decl: // Union sql.

frontend/src/protos/xyz/block/ftl/v1/schema/schema_pb.ts

+106
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/integration_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ func TestIntegration(t *testing.T) {
9494
}),
9595
}},
9696
{name: "UseKotlinDbConn", assertions: assertions{
97-
setUpKotlinModuleDb(filepath.Join(modulesDir, "ftl-module-echo3")),
97+
setUpModuleDb(filepath.Join(modulesDir, "ftl-module-echo3")),
9898
run(".", "ftl", "deploy", filepath.Join(modulesDir, "ftl-module-echo3")),
9999
call("dbtest", "create", obj{"data": "Hello"}, func(t testing.TB, resp obj) {}),
100-
validateKotlinModuleDb(),
100+
validateModuleDb(),
101101
}},
102102
{name: "SchemaGenerateJS", assertions: assertions{
103103
run(".", "ftl", "schema", "generate", "integration/testdata/schema-generate", "build/schema-generate"),
@@ -230,7 +230,7 @@ func call[Resp any](module, verb string, req obj, onResponse func(t testing.TB,
230230
}
231231
}
232232

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

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

0 commit comments

Comments
 (0)