feat(store): config table for instance-global app-config overrides
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
39
backend/internal/store/config.go
Normal file
39
backend/internal/store/config.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ConfigValues returns every application-configuration override row as
|
||||
// key -> value. An absent key means its default (from internal/config's
|
||||
// app-key registry) is in effect -- the table stores overrides only.
|
||||
func (db *DB) ConfigValues(ctx context.Context) (map[string]string, error) {
|
||||
rows, err := db.QueryContext(ctx, `SELECT key, value FROM config`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("config values: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
values := map[string]string{}
|
||||
for rows.Next() {
|
||||
var k, v string
|
||||
if err := rows.Scan(&k, &v); err != nil {
|
||||
return nil, fmt.Errorf("scan config row: %w", err)
|
||||
}
|
||||
values[k] = v
|
||||
}
|
||||
return values, rows.Err()
|
||||
}
|
||||
|
||||
// SetConfigValue upserts one override row. Key validation is the caller's
|
||||
// job (config.ValidateAppValue) -- the store stays a dumb K/V layer.
|
||||
func (db *DB) SetConfigValue(ctx context.Context, key, value string) error {
|
||||
if _, err := db.ExecContext(ctx, `
|
||||
INSERT INTO config (key, value, updated_at) VALUES (?, ?, datetime('now'))
|
||||
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at`,
|
||||
key, value); err != nil {
|
||||
return fmt.Errorf("set config %q: %w", key, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
35
backend/internal/store/config_test.go
Normal file
35
backend/internal/store/config_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConfigValues_EmptyThenUpsertOverwrites(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
values, err := db.ConfigValues(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigValues (empty): %v", err)
|
||||
}
|
||||
if len(values) != 0 {
|
||||
t.Fatalf("expected no overrides in a fresh DB, got %v", values)
|
||||
}
|
||||
|
||||
if err := db.SetConfigValue(ctx, "session.duration", "168"); err != nil {
|
||||
t.Fatalf("SetConfigValue: %v", err)
|
||||
}
|
||||
// Same key again: upsert must overwrite, not error or duplicate.
|
||||
if err := db.SetConfigValue(ctx, "session.duration", "24"); err != nil {
|
||||
t.Fatalf("SetConfigValue (overwrite): %v", err)
|
||||
}
|
||||
|
||||
values, err = db.ConfigValues(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigValues: %v", err)
|
||||
}
|
||||
if len(values) != 1 || values["session.duration"] != "24" {
|
||||
t.Fatalf("expected {session.duration: 24}, got %v", values)
|
||||
}
|
||||
}
|
||||
@@ -251,3 +251,14 @@ CREATE TABLE sync_runs (
|
||||
status TEXT NOT NULL CHECK(status IN ('running','success','error')),
|
||||
error_message TEXT
|
||||
);
|
||||
|
||||
-- Application configuration: instance-global key/value overrides, shared
|
||||
-- by every user -- deliberately the one table with no user_id, because
|
||||
-- application configuration is common to all users by definition. Stores
|
||||
-- overrides only; defaults live in internal/config's app-key registry.
|
||||
-- Cold: read once at startup, a change applies on the next backend restart.
|
||||
CREATE TABLE config (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user