Skip to content

Commit f97ca89

Browse files
dannykoppingEmyrk
authored andcommitted
Add database queries against site_configs
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 03ecbac commit f97ca89

File tree

10 files changed

+134
-45
lines changed

10 files changed

+134
-45
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,11 @@ func (q *querier) DeleteReplicasUpdatedBefore(ctx context.Context, updatedAt tim
11831183
return q.db.DeleteReplicasUpdatedBefore(ctx, updatedAt)
11841184
}
11851185

1186+
func (q *querier) DeleteRuntimeConfig(ctx context.Context, key string) error {
1187+
// TODO: auth
1188+
return q.db.DeleteRuntimeConfig(ctx, key)
1189+
}
1190+
11861191
func (q *querier) DeleteTailnetAgent(ctx context.Context, arg database.DeleteTailnetAgentParams) (database.DeleteTailnetAgentRow, error) {
11871192
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceTailnetCoordinator); err != nil {
11881193
return database.DeleteTailnetAgentRow{}, err
@@ -1856,6 +1861,11 @@ func (q *querier) GetReplicasUpdatedAfter(ctx context.Context, updatedAt time.Ti
18561861
return q.db.GetReplicasUpdatedAfter(ctx, updatedAt)
18571862
}
18581863

1864+
func (q *querier) GetRuntimeConfig(ctx context.Context, key string) (string, error) {
1865+
// TODO: auth
1866+
return q.db.GetRuntimeConfig(ctx, key)
1867+
}
1868+
18591869
func (q *querier) GetTailnetAgents(ctx context.Context, id uuid.UUID) ([]database.TailnetAgent, error) {
18601870
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceTailnetCoordinator); err != nil {
18611871
return nil, err
@@ -3906,6 +3916,11 @@ func (q *querier) UpsertProvisionerDaemon(ctx context.Context, arg database.Upse
39063916
return q.db.UpsertProvisionerDaemon(ctx, arg)
39073917
}
39083918

3919+
func (q *querier) UpsertRuntimeConfig(ctx context.Context, arg database.UpsertRuntimeConfigParams) error {
3920+
// TODO: auth
3921+
return q.db.UpsertRuntimeConfig(ctx, arg)
3922+
}
3923+
39093924
func (q *querier) UpsertTailnetAgent(ctx context.Context, arg database.UpsertTailnetAgentParams) (database.TailnetAgent, error) {
39103925
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceTailnetCoordinator); err != nil {
39113926
return database.TailnetAgent{}, err

coderd/database/dbmem/dbmem.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"golang.org/x/xerrors"
2323

2424
"github.com/coder/coder/v2/coderd/notifications/types"
25+
"github.com/coder/coder/v2/coderd/runtimeconfig"
2526

2627
"github.com/coder/coder/v2/coderd/database"
2728
"github.com/coder/coder/v2/coderd/database/dbtime"
@@ -84,6 +85,7 @@ func New() database.Store {
8485
workspaceProxies: make([]database.WorkspaceProxy, 0),
8586
customRoles: make([]database.CustomRole, 0),
8687
locks: map[int64]struct{}{},
88+
runtimeConfig: map[string]string{},
8789
},
8890
}
8991
// Always start with a default org. Matching migration 198.
@@ -194,6 +196,7 @@ type data struct {
194196
workspaces []database.Workspace
195197
workspaceProxies []database.WorkspaceProxy
196198
customRoles []database.CustomRole
199+
runtimeConfig map[string]string
197200
// Locks is a map of lock names. Any keys within the map are currently
198201
// locked.
199202
locks map[int64]struct{}
@@ -1928,6 +1931,14 @@ func (q *FakeQuerier) DeleteReplicasUpdatedBefore(_ context.Context, before time
19281931
return nil
19291932
}
19301933

1934+
func (q *FakeQuerier) DeleteRuntimeConfig(_ context.Context, key string) error {
1935+
q.mutex.Lock()
1936+
defer q.mutex.Unlock()
1937+
1938+
delete(q.runtimeConfig, key)
1939+
return nil
1940+
}
1941+
19311942
func (*FakeQuerier) DeleteTailnetAgent(context.Context, database.DeleteTailnetAgentParams) (database.DeleteTailnetAgentRow, error) {
19321943
return database.DeleteTailnetAgentRow{}, ErrUnimplemented
19331944
}
@@ -3505,6 +3516,18 @@ func (q *FakeQuerier) GetReplicasUpdatedAfter(_ context.Context, updatedAt time.
35053516
return replicas, nil
35063517
}
35073518

3519+
func (q *FakeQuerier) GetRuntimeConfig(_ context.Context, key string) (string, error) {
3520+
q.mutex.Lock()
3521+
defer q.mutex.Unlock()
3522+
3523+
val, ok := q.runtimeConfig[key]
3524+
if !ok {
3525+
return "", runtimeconfig.EntryNotFound
3526+
}
3527+
3528+
return val, nil
3529+
}
3530+
35083531
func (*FakeQuerier) GetTailnetAgents(context.Context, uuid.UUID) ([]database.TailnetAgent, error) {
35093532
return nil, ErrUnimplemented
35103533
}
@@ -9186,6 +9209,19 @@ func (q *FakeQuerier) UpsertProvisionerDaemon(_ context.Context, arg database.Up
91869209
return d, nil
91879210
}
91889211

9212+
func (q *FakeQuerier) UpsertRuntimeConfig(ctx context.Context, arg database.UpsertRuntimeConfigParams) error {
9213+
err := validateDatabaseType(arg)
9214+
if err != nil {
9215+
return err
9216+
}
9217+
9218+
q.mutex.Lock()
9219+
defer q.mutex.Unlock()
9220+
9221+
q.runtimeConfig[arg.Key] = arg.Value
9222+
return nil
9223+
}
9224+
91899225
func (*FakeQuerier) UpsertTailnetAgent(context.Context, database.UpsertTailnetAgentParams) (database.TailnetAgent, error) {
91909226
return database.TailnetAgent{}, ErrUnimplemented
91919227
}

coderd/database/dbmetrics/dbmetrics.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/siteconfig.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,14 @@ SELECT
9696
INSERT INTO site_configs (key, value) VALUES ('notifications_settings', $1)
9797
ON CONFLICT (key) DO UPDATE SET value = $1 WHERE site_configs.key = 'notifications_settings';
9898

99+
-- name: GetRuntimeConfig :one
100+
SELECT value FROM site_configs WHERE site_configs.key = $1;
101+
102+
-- name: UpsertRuntimeConfig :exec
103+
INSERT INTO site_configs (key, value) VALUES ($1, $2)
104+
ON CONFLICT (key) DO UPDATE SET value = $2 WHERE site_configs.key = $1;
105+
106+
-- name: DeleteRuntimeConfig :exec
107+
DELETE FROM site_configs
108+
WHERE site_configs.key = $1;
109+

coderd/runtimeconfig/config_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/stretchr/testify/require"
99

1010
"github.com/coder/coder/v2/coderd/coderdtest"
11+
"github.com/coder/coder/v2/coderd/database/dbmem"
1112
"github.com/coder/coder/v2/coderd/runtimeconfig"
1213
"github.com/coder/coder/v2/coderd/util/ptr"
1314
"github.com/coder/coder/v2/codersdk"
@@ -69,7 +70,7 @@ func TestConfig(t *testing.T) {
6970
t.Parallel()
7071

7172
ctx := testutil.Context(t, testutil.WaitShort)
72-
store := runtimeconfig.NewInMemoryStore()
73+
store := dbmem.New()
7374
resolver := runtimeconfig.NewOrgResolver(altOrg.ID, runtimeconfig.NewStoreResolver(store))
7475
mutator := runtimeconfig.NewOrgMutator(altOrg.ID, runtimeconfig.NewStoreMutator(store))
7576

@@ -102,7 +103,7 @@ func TestConfig(t *testing.T) {
102103
t.Parallel()
103104

104105
ctx := testutil.Context(t, testutil.WaitShort)
105-
store := runtimeconfig.NewInMemoryStore()
106+
store := dbmem.New()
106107
resolver := runtimeconfig.NewOrgResolver(altOrg.ID, runtimeconfig.NewStoreResolver(store))
107108
mutator := runtimeconfig.NewOrgMutator(altOrg.ID, runtimeconfig.NewStoreMutator(store))
108109

coderd/runtimeconfig/mutator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55

66
"github.com/google/uuid"
77
"golang.org/x/xerrors"
8+
9+
"github.com/coder/coder/v2/coderd/database"
810
)
911

1012
type StoreMutator struct {
@@ -19,7 +21,7 @@ func NewStoreMutator(store Store) *StoreMutator {
1921
}
2022

2123
func (s *StoreMutator) MutateByKey(ctx context.Context, key, val string) error {
22-
err := s.store.UpsertRuntimeSetting(ctx, key, val)
24+
err := s.store.UpsertRuntimeConfig(ctx, database.UpsertRuntimeConfigParams{Key: key, Value: val})
2325
if err != nil {
2426
return xerrors.Errorf("update %q: %w", err)
2527
}

coderd/runtimeconfig/resolver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (s StoreResolver) ResolveByKey(ctx context.Context, key string) (string, er
2222
panic("developer error: store must be set")
2323
}
2424

25-
val, err := s.store.GetRuntimeSetting(ctx, key)
25+
val, err := s.store.GetRuntimeConfig(ctx, key)
2626
if err != nil {
2727
if errors.Is(err, sql.ErrNoRows) {
2828
return "", xerrors.Errorf("%q: %w", key, EntryNotFound)

coderd/runtimeconfig/store.go

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,16 @@ package runtimeconfig
22

33
import (
44
"context"
5-
"sync"
65

76
"golang.org/x/xerrors"
7+
8+
"github.com/coder/coder/v2/coderd/database"
89
)
910

1011
var EntryNotFound = xerrors.New("entry not found")
1112

1213
type Store interface {
13-
GetRuntimeSetting(ctx context.Context, key string) (string, error)
14-
UpsertRuntimeSetting(ctx context.Context, key, value string) error
15-
DeleteRuntimeSetting(ctx context.Context, key string) error
16-
}
17-
18-
type InMemoryStore struct {
19-
mu sync.Mutex
20-
store map[string]string
21-
}
22-
23-
func NewInMemoryStore() *InMemoryStore {
24-
return &InMemoryStore{store: make(map[string]string)}
25-
}
26-
27-
func (s *InMemoryStore) GetRuntimeSetting(_ context.Context, key string) (string, error) {
28-
s.mu.Lock()
29-
defer s.mu.Unlock()
30-
31-
val, ok := s.store[key]
32-
if !ok {
33-
return "", EntryNotFound
34-
}
35-
36-
return val, nil
37-
}
38-
39-
func (s *InMemoryStore) UpsertRuntimeSetting(_ context.Context, key, value string) error {
40-
s.mu.Lock()
41-
defer s.mu.Unlock()
42-
43-
s.store[key] = value
44-
return nil
45-
}
46-
47-
func (s *InMemoryStore) DeleteRuntimeSetting(_ context.Context, key string) error {
48-
s.mu.Lock()
49-
defer s.mu.Unlock()
50-
51-
delete(s.store, key)
52-
return nil
14+
GetRuntimeConfig(ctx context.Context, key string) (string, error)
15+
UpsertRuntimeConfig(ctx context.Context, arg database.UpsertRuntimeConfigParams) error
16+
DeleteRuntimeConfig(ctx context.Context, key string) error
5317
}

0 commit comments

Comments
 (0)