Files
archie/modules/governance/internal/store/store.go
T

99 lines
3.3 KiB
Go

// Package store holds the governance module's SQL: the organisation's model
// roles.
package store
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/jmoiron/sqlx"
)
// ModelRole is the model an organisation chose for one role.
type ModelRole struct {
OrgID string `db:"org_id"`
Role string `db:"role"`
Class string `db:"class"`
BaseURL string `db:"base_url"`
Model string `db:"model"`
}
// Store is the governance module's Postgres store.
type Store struct {
db *sqlx.DB
}
// New returns a store on db.
func New(db *sqlx.DB) *Store { return &Store{db: db} }
// ModelRole returns the organisation's model for role; ok is false when none
// was chosen.
func (s *Store) ModelRole(ctx context.Context, orgID, role string) (ModelRole, bool, error) {
var out ModelRole
err := s.db.GetContext(ctx, &out, `
SELECT org_id, role, class, base_url, model FROM governance.model_roles
WHERE org_id = $1 AND role = $2`, orgID, role)
if errors.Is(err, sql.ErrNoRows) {
return ModelRole{}, false, nil
}
if err != nil {
return ModelRole{}, false, fmt.Errorf("read model role %q: %w", role, err)
}
return out, true, nil
}
// PutModelRole records the organisation's model for a role.
func (s *Store) PutModelRole(ctx context.Context, m ModelRole) error {
if _, err := s.db.NamedExecContext(ctx, `
INSERT INTO governance.model_roles (org_id, role, class, base_url, model)
VALUES (:org_id, :role, :class, :base_url, :model)
ON CONFLICT (org_id, role) DO UPDATE
SET class = EXCLUDED.class, base_url = EXCLUDED.base_url, model = EXCLUDED.model, updated_at = now()`, m); err != nil {
return fmt.Errorf("put model role %q: %w", m.Role, err)
}
return nil
}
// Settings is an organisation's settings as stored. A duration is kept in
// milliseconds: Postgres has no duration type that scans into one.
type Settings struct {
OrgID string `db:"org_id"`
// ApprovalDeadlineMS is how long an approval request waits for an answer.
ApprovalDeadlineMS int64 `db:"approval_deadline_ms"`
// RaiseAfter is how many approvals in a row earn a proposal to raise an
// action class.
RaiseAfter int `db:"raise_after"`
// Frozen holds every action that changes something at propose and wait.
Frozen bool `db:"frozen"`
}
// Settings returns the organisation's settings; ok is false when none were
// saved.
func (s *Store) Settings(ctx context.Context, orgID string) (Settings, bool, error) {
var out Settings
err := s.db.GetContext(ctx, &out, `
SELECT org_id, approval_deadline_ms, raise_after, frozen FROM governance.settings WHERE org_id = $1`, orgID)
if errors.Is(err, sql.ErrNoRows) {
return Settings{}, false, nil
}
if err != nil {
return Settings{}, false, fmt.Errorf("read settings: %w", err)
}
return out, true, nil
}
// PutSettings records the organisation's settings.
func (s *Store) PutSettings(ctx context.Context, set Settings) error {
if _, err := s.db.NamedExecContext(ctx, `
INSERT INTO governance.settings (org_id, approval_deadline_ms, raise_after, frozen)
VALUES (:org_id, :approval_deadline_ms, :raise_after, :frozen)
ON CONFLICT (org_id) DO UPDATE
SET approval_deadline_ms = EXCLUDED.approval_deadline_ms, raise_after = EXCLUDED.raise_after,
frozen = EXCLUDED.frozen, updated_at = now()`, set); err != nil {
return fmt.Errorf("put settings: %w", err)
}
return nil
}