This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/store/postgres/policy.go
Jeffrey Smith 11fd8c1e57 step 1: rename module chat-switchboard → switchboard-core
- go.mod module name
- All 714 import references across 289 Go files
- VERSION: 0.1.0
- CI DB names: switchboard_core_{ci,dev,test}
- Docker image: gobha/switchboard-core
- Test fixtures: JWT issuer, repo names
- .env.example, docker-compose container name
- Compiles clean (go build exit 0)
2026-03-25 19:48:04 -04:00

69 lines
1.8 KiB
Go

package postgres
import (
"context"
"database/sql"
"switchboard-core/models"
)
type PolicyStore struct{}
func NewPolicyStore() *PolicyStore { return &PolicyStore{} }
// Get returns a policy value by key. Falls back to PolicyDefaults if not in DB.
func (s *PolicyStore) Get(ctx context.Context, key string) (string, error) {
var value string
err := DB.QueryRowContext(ctx,
"SELECT value FROM platform_policies WHERE key = $1", key).Scan(&value)
if err == sql.ErrNoRows {
if def, ok := models.PolicyDefaults[key]; ok {
return def, nil
}
return "", nil
}
return value, err
}
// GetBool returns a policy value as a boolean.
func (s *PolicyStore) GetBool(ctx context.Context, key string) (bool, error) {
val, err := s.Get(ctx, key)
if err != nil {
return false, err
}
return val == "true", nil
}
// Set upserts a policy value.
func (s *PolicyStore) Set(ctx context.Context, key, value, updatedBy string) error {
_, err := DB.ExecContext(ctx, `
INSERT INTO platform_policies (key, value, updated_by, updated_at)
VALUES ($1, $2, $3, NOW())
ON CONFLICT (key) DO UPDATE SET value = $2, updated_by = $3, updated_at = NOW()`,
key, value, updatedBy)
return err
}
// GetAll returns all platform policies, merged with defaults.
func (s *PolicyStore) GetAll(ctx context.Context) (map[string]string, error) {
result := make(map[string]string)
// Start with defaults
for k, v := range models.PolicyDefaults {
result[k] = v
}
// Override with DB values
rows, err := DB.QueryContext(ctx, "SELECT key, value FROM platform_policies")
if err != nil {
return result, err // return defaults on error
}
defer rows.Close()
for rows.Next() {
var k, v string
if err := rows.Scan(&k, &v); err != nil {
continue
}
result[k] = v
}
return result, rows.Err()
}