Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract database into schema for go #1320

Merged
merged 4 commits into from
Apr 23, 2024
Merged
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
6 changes: 0 additions & 6 deletions backend/controller/cronjobs/cronjobs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ package cronjobs

import (
"context"
"sync"
"testing"
"time"

"connectrpc.com/connect"
db "github.com/TBD54566975/ftl/backend/controller/dal"
"github.com/TBD54566975/ftl/backend/controller/sql/sqltest"
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/model"
"github.com/alecthomas/assert/v2"
"github.com/alecthomas/types/optional"
"github.com/benbjohnson/clock"
)

Expand Down
37 changes: 32 additions & 5 deletions go-runtime/compile/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +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
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
ftlDatabaseFuncPath = "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 @@ -181,6 +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)
}
}

Expand Down Expand Up @@ -227,6 +230,7 @@ func parseConfigDecl(pctx *parseContext, node *ast.CallExpr, fn *types.Func) {
}
if name == "" {
pctx.errors.add(errorf(node, "config and secret declarations must have a single string literal argument"))
return
}
index := node.Fun.(*ast.IndexExpr) //nolint:forcetypeassert

Expand Down Expand Up @@ -256,6 +260,29 @@ 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) {
var name string
if len(node.Args) == 1 {
if literal, ok := node.Args[0].(*ast.BasicLit); ok && literal.Kind == token.STRING {
var err error
name, err = strconv.Unquote(literal.Value)
if err != nil {
pctx.errors.add(wrapf(node, err, ""))
return
}
}
}
if name == "" {
pctx.errors.add(errorf(node, "config and secret declarations must have a single string literal argument"))
matt2e marked this conversation as resolved.
Show resolved Hide resolved
return
}
decl := &schema.Database{
Pos: goPosToSchemaPos(node.Pos()),
Name: name,
}
pctx.module.Decls = append(pctx.module.Decls, decl)
}

func visitFile(pctx *parseContext, node *ast.File) {
if node.Doc == nil {
return
Expand Down
2 changes: 2 additions & 0 deletions go-runtime/compile/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func TestExtractModuleSchema(t *testing.T) {
config configValue one.Config
secret secretValue String

database testDb

enum Color(String) {
Red("Red")
Blue("Blue")
Expand Down
1 change: 1 addition & 0 deletions go-runtime/compile/testdata/one/one.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Config struct {

var configValue = ftl.Config[Config]("configValue")
var secretValue = ftl.Secret[string]("secretValue")
var testDb = ftl.PostgresDatabase("testDb")

//ftl:export
func Verb(ctx context.Context, req Req) (Resp, error) {
Expand Down
8 changes: 4 additions & 4 deletions integration/testdata/go/database/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ type InsertResponse struct{}

//ftl:export
func Insert(ctx context.Context, req InsertRequest) (InsertResponse, error) {
err := persistRequest(req)
err := persistRequest(ctx, req)
if err != nil {
return InsertResponse{}, err
}

return InsertResponse{}, nil
}

func persistRequest(req InsertRequest) error {
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS requests
func persistRequest(ctx context.Context, req InsertRequest) error {
_, err := db.Get(ctx).Exec(`CREATE TABLE IF NOT EXISTS requests
(
data TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT (NOW() AT TIME ZONE 'utc'),
Expand All @@ -34,7 +34,7 @@ func persistRequest(req InsertRequest) error {
if err != nil {
return err
}
_, err = db.Exec("INSERT INTO requests (data) VALUES ($1);", req.Data)
_, err = db.Get(ctx).Exec("INSERT INTO requests (data) VALUES ($1);", req.Data)
if err != nil {
return err
}
Expand Down