Skip to content

Commit

Permalink
feat: add database type to the schema (#1374)
Browse files Browse the repository at this point in the history
fixes #1306
  • Loading branch information
worstell authored May 1, 2024
1 parent 8107c4a commit 223151a
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 368 deletions.
703 changes: 356 additions & 347 deletions backend/protos/xyz/block/ftl/v1/schema/schema.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions backend/protos/xyz/block/ftl/v1/schema/schema.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ message Database {
optional Position pos = 1;
string name = 2;
repeated string comments = 3;
string type = 4;
}

message Decl {
Expand Down
7 changes: 6 additions & 1 deletion backend/schema/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
)

const PostgresDatabaseType = "postgres"

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

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

Expand All @@ -26,14 +29,15 @@ 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)
fmt.Fprintf(w, "%s database %s", d.Type, d.Name)
return w.String()
}

func (d *Database) ToProto() proto.Message {
return &schemapb.Database{
Pos: posToProto(d.Pos),
Name: d.Name,
Type: d.Type,
Comments: d.Comments,
}
}
Expand All @@ -45,5 +49,6 @@ func DatabaseFromProto(s *schemapb.Database) *Database {
Pos: posFromProto(s.Pos),
Name: s.Name,
Comments: s.Comments,
Type: s.Type,
}
}
5 changes: 3 additions & 2 deletions backend/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module todo {
config configValue String
secret secretValue String
database testdb
postgres database testdb
data CreateRequest {
name {String: String}? +alias json "rqn"
Expand Down Expand Up @@ -387,7 +387,7 @@ func TestParseModule(t *testing.T) {
module todo {
config configValue String
secret secretValue String
database testdb
postgres database testdb
data CreateRequest {
name {String: String}? +alias json "rqn"
Expand Down Expand Up @@ -467,6 +467,7 @@ var testSchema = MustValidate(&Schema{
},
&Database{
Name: "testdb",
Type: "postgres",
},
&Data{
Name: "CreateRequest",
Expand Down
4 changes: 2 additions & 2 deletions backend/schema/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ func TestValidate(t *testing.T) {
{name: "DuplicateDatabases",
schema: `
module one {
database MY_DB
database MY_DB
postgres database MY_DB
postgres database MY_DB
}
`,
errs: []string{
Expand Down
4 changes: 2 additions & 2 deletions buildengine/testdata/projects/alpha/go.sum

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

6 changes: 6 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.

19 changes: 10 additions & 9 deletions go-runtime/compile/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ var (
return mustLoadRef("builtin", "error").Type().Underlying().(*types.Interface) //nolint:forcetypeassert
})

ftlCallFuncPath = "github.com/TBD54566975/ftl/go-runtime/ftl.Call"
ftlConfigFuncPath = "github.com/TBD54566975/ftl/go-runtime/ftl.Config"
ftlSecretFuncPath = "github.com/TBD54566975/ftl/go-runtime/ftl.Secret" //nolint:gosec
ftlDatabaseFuncPath = "github.com/TBD54566975/ftl/go-runtime/ftl.PostgresDatabase"
ftlUnitTypePath = "github.com/TBD54566975/ftl/go-runtime/ftl.Unit"
aliasFieldTag = "json"
ftlCallFuncPath = "github.com/TBD54566975/ftl/go-runtime/ftl.Call"
ftlConfigFuncPath = "github.com/TBD54566975/ftl/go-runtime/ftl.Config"
ftlSecretFuncPath = "github.com/TBD54566975/ftl/go-runtime/ftl.Secret" //nolint:gosec
ftlPostgresDBFuncPath = "github.com/TBD54566975/ftl/go-runtime/ftl.PostgresDatabase"
ftlUnitTypePath = "github.com/TBD54566975/ftl/go-runtime/ftl.Unit"
aliasFieldTag = "json"
)

// NativeNames is a map of top-level declarations to their native Go names.
Expand Down Expand Up @@ -182,8 +182,8 @@ func visitCallExpr(pctx *parseContext, node *ast.CallExpr) {
case ftlConfigFuncPath, ftlSecretFuncPath:
// Secret/config declaration: ftl.Config[<type>](<name>)
parseConfigDecl(pctx, node, fn)
case ftlDatabaseFuncPath:
parseDatabaseDecl(pctx, node)
case ftlPostgresDBFuncPath:
parseDatabaseDecl(pctx, node, schema.PostgresDatabaseType)
}
}

Expand Down Expand Up @@ -281,7 +281,7 @@ func parseConfigDecl(pctx *parseContext, node *ast.CallExpr, fn *types.Func) {
pctx.module.Decls = append(pctx.module.Decls, decl)
}

func parseDatabaseDecl(pctx *parseContext, node *ast.CallExpr) {
func parseDatabaseDecl(pctx *parseContext, node *ast.CallExpr, dbType string) {
var name string
if len(node.Args) == 1 {
if literal, ok := node.Args[0].(*ast.BasicLit); ok && literal.Kind == token.STRING {
Expand Down Expand Up @@ -311,6 +311,7 @@ func parseDatabaseDecl(pctx *parseContext, node *ast.CallExpr) {
decl := &schema.Database{
Pos: goPosToSchemaPos(node.Pos()),
Name: name,
Type: dbType,
}
pctx.module.Decls = append(pctx.module.Decls, decl)
}
Expand Down
2 changes: 1 addition & 1 deletion go-runtime/compile/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestExtractModuleSchema(t *testing.T) {
config configValue one.Config
secret secretValue String
database testDb
postgres database testDb
enum Color: String {
Red = "Red"
Expand Down
4 changes: 2 additions & 2 deletions go-runtime/compile/testdata/one/go.sum

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

4 changes: 2 additions & 2 deletions go-runtime/compile/testdata/two/go.sum

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

0 comments on commit 223151a

Please sign in to comment.