Files
uptimesebi/internal/tenant/tenant.go
T
Sebastian UnterschützandClaude Opus 5.5 538e9358c3 feat(api): configurable retention, housekeeping and storage overview
UPTIME_RETENTION_RAW, _HOURLY and _DAILY set TimescaleDB's retention
policies, applied by "api migrate up" with owner rights and on start;
periods too short for the charts are rejected. An hourly job deletes
delivered notifications, stale invitations and expired API tokens after
UPTIME_RETENTION_HOUSEKEEPING and resolved incidents together with the
daily aggregate. The admin panel shows size, compression, retention and
background job status of each storage level.

Closes #63

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 09:50:12 +01:00

84 lines
2.5 KiB
Go

// Package tenant runs queries inside a row-level-security context. All
// database access from request handlers must go through DB.Tx.
package tenant
import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
)
// Scope is the row-level-security context of one transaction. The zero value
// sees nothing.
type Scope struct {
OrgID uuid.UUID
UserID uuid.UUID
system bool
global bool
}
// Org scopes to one organization. user may be uuid.Nil for background work.
func Org(org, user uuid.UUID) Scope { return Scope{OrgID: org, UserID: user} }
// User grants access to the user's own memberships and the orgs behind them,
// which is what login and the org switcher need before an org is chosen.
func User(user uuid.UUID) Scope { return Scope{UserID: user} }
// Global carries no tenant context: only tables without row-level security,
// such as users, are usable. Login runs here before the user is known.
func Global() Scope { return Scope{global: true} }
// System bypasses tenant isolation. Reserve it for work that is inherently
// cross-tenant, never for request handling.
func System() Scope { return Scope{system: true} }
// ErrNoScope is returned by Tx for the zero Scope.
var ErrNoScope = errors.New("tenant: empty scope")
// DB wraps a pool so that every transaction carries a Scope.
type DB struct{ pool *pgxpool.Pool }
// New returns a DB using pool.
func New(pool *pgxpool.Pool) *DB { return &DB{pool: pool} }
// Pool returns the underlying pool for queries outside any tenant table,
// such as TimescaleDB's catalog.
func (d *DB) Pool() *pgxpool.Pool { return d.pool }
// Tx runs fn in a transaction whose row-level-security settings come from s.
// The transaction commits if fn returns nil and rolls back otherwise.
func (d *DB) Tx(ctx context.Context, s Scope, fn func(*store.Queries) error) error {
if s == (Scope{}) {
return ErrNoScope
}
return pgx.BeginFunc(ctx, d.pool, func(tx pgx.Tx) error {
if _, err := tx.Exec(ctx,
`SELECT set_config('app.org_id', $1, true), set_config('app.user_id', $2, true), set_config('app.scope', $3, true)`,
idOrEmpty(s.OrgID), idOrEmpty(s.UserID), scopeName(s),
); err != nil {
return fmt.Errorf("tenant: %w", err)
}
return fn(store.New(tx))
})
}
func idOrEmpty(id uuid.UUID) string {
if id == uuid.Nil {
return ""
}
return id.String()
}
func scopeName(s Scope) string {
if s.system {
return "system"
}
return ""
}