Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
deniseli committed May 25, 2024
1 parent 86b4738 commit 3741ac5
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 70 deletions.
60 changes: 23 additions & 37 deletions backend/controller/dal/database_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/TBD54566975/ftl/backend/controller/sql"
"github.com/TBD54566975/ftl/common/configuration"
"github.com/alecthomas/types/optional"
"github.com/TBD54566975/ftl/internal/slices"
)

// DatabaseResolver loads values a project's configuration from the given database.
Expand All @@ -26,56 +26,42 @@ func (d *DAL) NewConfigResolver() DatabaseResolver {
}

func (d DatabaseResolver) Get(ctx context.Context, ref configuration.Ref) (*url.URL, error) {
module, moduleOk := ref.Module.Get()
if !moduleOk {
module = "" // global
}
accessor, err := d.db.GetConfig(ctx, module, ref.Name)
value, err := d.db.GetConfig(ctx, ref.Module, ref.Name)
if err != nil {
// If we could not find this config within the module, then try getting
// again with scope set to global
if moduleOk {
return d.Get(ctx, configuration.Ref{
Module: optional.None[string](),
Name: ref.Name,
})
}
return nil, configuration.ErrNotFound
}
return url.Parse(accessor)
p := configuration.DBProvider{true}
return p.Store(ctx, ref, []byte(value))
}

func (d DatabaseResolver) List(ctx context.Context) ([]configuration.Entry, error) {
configs, err := d.db.ListConfigs(ctx)
if err != nil {
return nil, err
return nil, translatePGError(err)
}
entries := []configuration.Entry{}
for _, c := range configs {
u, err := url.Parse(c.Accessor)
if err != nil {
return nil, err
}
entries = append(entries, configuration.Entry{
Ref: configuration.NewRef(c.Module, c.Name),
p := configuration.DBProvider{true}
return slices.Map(configs, func(c sql.Config) configuration.Entry {
ref := configuration.Ref{c.Module, c.Name}
// err can be ignored because Store always returns a nil error
u, _ := p.Store(ctx, ref, c.Value)
return configuration.Entry{
Ref: ref,
Accessor: u,
})
}
return entries, nil
}
}), nil
}

func (d DatabaseResolver) Set(ctx context.Context, ref configuration.Ref, key *url.URL) error {
return d.db.SetConfig(ctx, moduleAsString(ref), ref.Name, key.String())
p := configuration.DBProvider{true}
value, err := p.Load(ctx, ref, key)
if err != nil {
return err
}
err = d.db.SetConfig(ctx, ref.Module, ref.Name, value)
return translatePGError(err)
}

func (d DatabaseResolver) Unset(ctx context.Context, ref configuration.Ref) error {
return d.db.UnsetConfig(ctx, moduleAsString(ref), ref.Name)
}

func moduleAsString(ref configuration.Ref) string {
module, ok := ref.Module.Get()
if !ok {
return ""
}
return module
err := d.db.UnsetConfig(ctx, ref.Module, ref.Name)
return translatePGError(err)
}
4 changes: 2 additions & 2 deletions backend/controller/dal/database_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func TestDBResolverRoundTrip(t *testing.T) {
}

ctx, cr := setup(t)
u := URL("db://asdfasdf")
u := URL("db://ImFzZGYi") // asdf
for _, test := range tests {
t.Run(test.TestName, func(t *testing.T) {
if test.PresetGlobal {
err := cr.Set(ctx, configuration.Ref{
Module: optional.None[string](),
Name: "configname",
}, URL("db://qwerty"))
}, URL("db://InF3ZXJ0eSI")) // qwerty
assert.NoError(t, err)
}
err := cr.Set(ctx, configuration.Ref{
Expand Down
8 changes: 4 additions & 4 deletions backend/controller/sql/models.go

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

6 changes: 3 additions & 3 deletions backend/controller/sql/querier.go

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

11 changes: 7 additions & 4 deletions backend/controller/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -547,18 +547,21 @@ VALUES ((SELECT id FROM execution), (SELECT id FROM call))
RETURNING id;

-- name: GetConfig :one
SELECT accessor
SELECT value
FROM configs
WHERE module = @module
AND name = @name;
WHERE
(module IS NULL OR module = @module)
AND name = @name
ORDER BY module NULLS LAST
LIMIT 1;

-- name: ListConfigs :many
SELECT *
FROM configs
ORDER BY module, name;

-- name: SetConfig :exec
INSERT INTO configs (module, name, accessor)
INSERT INTO configs (module, name, value)
VALUES ($1, $2, $3);

-- name: UnsetConfig :exec
Expand Down
27 changes: 15 additions & 12 deletions backend/controller/sql/queries.sql.go

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

8 changes: 4 additions & 4 deletions backend/controller/sql/schema/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,10 @@ CREATE TABLE fsm_transitions (

CREATE TABLE configs
(
id BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
module TEXT NOT NULL,
name TEXT NOT NULL,
accessor TEXT NOT NULL,
id BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
module TEXT,
name TEXT NOT NULL,
value JSONB NOT NULL,
UNIQUE (module, name)
);

Expand Down
11 changes: 9 additions & 2 deletions common/configuration/db_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package configuration

import (
"context"
"encoding/base64"
"fmt"
"net/url"
)

Expand All @@ -18,11 +20,16 @@ func (DBProvider) Key() string { return "db" }
func (d DBProvider) Writer() bool { return d.DB }

func (DBProvider) Load(ctx context.Context, ref Ref, key *url.URL) ([]byte, error) {
return []byte(key.Host), nil
data, err := base64.RawURLEncoding.DecodeString(key.Host)
if err != nil {
return nil, fmt.Errorf("invalid base64 data in db configuration: %w", err)
}
return data, nil
}

func (DBProvider) Store(ctx context.Context, ref Ref, value []byte) (*url.URL, error) {
return &url.URL{Scheme: "db", Host: string(value)}, nil
b64 := base64.RawURLEncoding.EncodeToString(value)
return &url.URL{Scheme: "db", Host: b64}, nil
}

func (DBProvider) Delete(ctx context.Context, ref Ref) error {
Expand Down
4 changes: 2 additions & 2 deletions common/configuration/db_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

func TestDBProvider(t *testing.T) {
ctx := context.Background()
u := URL("db://asdf")
b := []byte("asdf")
u := URL("db://ImFzZGYi")
b := []byte(`"asdf"`)
ref := Ref{
Module: optional.None[string](),
Name: "name",
Expand Down

0 comments on commit 3741ac5

Please sign in to comment.