db

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddOrgMember

func AddOrgMember(ctx context.Context, db *sqlx.DB, m *models.OrgMember) error

func Connect

func Connect(cfg config.DatabaseConfig) (*sqlx.DB, error)

func CountAPIKeys

func CountAPIKeys(ctx context.Context, db *sqlx.DB) (int, error)

func CountActiveSessionsByActor

func CountActiveSessionsByActor(ctx context.Context, db *sqlx.DB, actor string) (int, error)

CountActiveSessionsByActor returns the number of active (not stopped) sessions for a given actor. Used to enforce per-actor session limits.

func CountArtifacts

func CountArtifacts(ctx context.Context, db *sqlx.DB, actor string) (int, error)

func CountAuditLogs

func CountAuditLogs(ctx context.Context, db *sqlx.DB, sessionID *uuid.UUID, actor string) (int, error)

CountAuditLogs mirrors the filter logic of ListAuditLogs for accurate totals.

func CountFiles

func CountFiles(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID) (int, error)

func CountOrgSessions

func CountOrgSessions(ctx context.Context, db *sqlx.DB, orgID uuid.UUID) (int, error)

func CountOrgs

func CountOrgs(ctx context.Context, db *sqlx.DB) (int, error)

func CountRuns

func CountRuns(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID) (int, error)

func CountRunsGlobal

func CountRunsGlobal(ctx context.Context, db *sqlx.DB) (int, error)

func CountSessions

func CountSessions(ctx context.Context, db *sqlx.DB) (int, error)

func CountSessionsFiltered

func CountSessionsFiltered(ctx context.Context, db *sqlx.DB, actor, labelKey, labelValue string) (int, error)

CountSessionsFiltered returns the number of active sessions matching the same filters as ListSessions (actor + optional label key/value).

func CountSessionsForActor

func CountSessionsForActor(ctx context.Context, db *sqlx.DB, actor, labelKey, labelValue string) (int, error)

CountSessionsForActor mirrors ListSessionsForActor for pagination totals.

func CreateAPIKey

func CreateAPIKey(ctx context.Context, db *sqlx.DB, k *models.APIKey) error

func CreateArtifact

func CreateArtifact(ctx context.Context, db *sqlx.DB, a *models.SharedArtifact) error

func CreateAuditLog

func CreateAuditLog(ctx context.Context, db *sqlx.DB, a *models.AuditLog) error

func CreateOrg

func CreateOrg(ctx context.Context, db *sqlx.DB, o *models.Organization) error

func CreateRun

func CreateRun(ctx context.Context, db *sqlx.DB, r *models.Run) error

func CreateSession

func CreateSession(ctx context.Context, db *sqlx.DB, s *models.Session) error

func CreateSnapshot

func CreateSnapshot(ctx context.Context, db *sqlx.DB, s *models.Snapshot) error

func DeleteArtifact

func DeleteArtifact(ctx context.Context, db *sqlx.DB, id uuid.UUID) (string, error)

func DeleteFile

func DeleteFile(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID, path string) error

func DeleteOldAuditLogs

func DeleteOldAuditLogs(ctx context.Context, db *sqlx.DB, before time.Time) (int64, error)

DeleteOldAuditLogs removes audit log entries whose timestamp is older than the given cutoff. Returns the number of rows deleted. Called by the background cleanup goroutine when AUDIT_LOG_RETENTION_DAYS > 0.

func DeleteOrg

func DeleteOrg(ctx context.Context, db *sqlx.DB, id uuid.UUID) error

func DeleteSnapshot

func DeleteSnapshot(ctx context.Context, db *sqlx.DB, id uuid.UUID) (string, error)

func FailStalePendingRuns

func FailStalePendingRuns(ctx context.Context, db *sqlx.DB, before time.Time) (int64, error)

FailStalePendingRuns marks as "failed" any runs that are still in the "pending" state and were created before the given cutoff timestamp.

This is called at startup to clean up runs that were mid-flight when the previous server instance was killed (e.g., a crash or SIGKILL during an in-memory queue drain). Without this cleanup, such runs sit in "pending" forever and are never surfaced to callers as failures.

For the Redis Streams backend, the reaper goroutine handles re-delivery; this function is a last-resort safety net for both queue backends.

func GetAPIKeyByHash

func GetAPIKeyByHash(ctx context.Context, db *sqlx.DB, keyHash string) (*models.APIKey, error)

func GetActorOrgs

func GetActorOrgs(ctx context.Context, db *sqlx.DB, principal string) ([]*models.OrgMember, error)

GetActorOrgs returns all org memberships for the given actor.

func GetArtifact

func GetArtifact(ctx context.Context, db *sqlx.DB, id uuid.UUID) (*models.SharedArtifact, error)

func GetFile

func GetFile(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID, path string) (*models.File, error)

func GetOrg

func GetOrg(ctx context.Context, db *sqlx.DB, id uuid.UUID) (*models.Organization, error)

func GetOrgBySlug

func GetOrgBySlug(ctx context.Context, db *sqlx.DB, slug string) (*models.Organization, error)

func GetOrgMember

func GetOrgMember(ctx context.Context, db *sqlx.DB, orgID uuid.UUID, principal string) (*models.OrgMember, error)

func GetOrgMemberRole

func GetOrgMemberRole(ctx context.Context, db *sqlx.DB, orgID uuid.UUID, principal string) (string, error)

GetOrgMemberRole returns the role for the given actor in the given org, or sql.ErrNoRows if they are not a member.

func GetRun

func GetRun(ctx context.Context, db *sqlx.DB, id uuid.UUID) (*models.Run, error)

func GetSession

func GetSession(ctx context.Context, db *sqlx.DB, id uuid.UUID) (*models.Session, error)

func GetSnapshot

func GetSnapshot(ctx context.Context, db *sqlx.DB, id uuid.UUID) (*models.Snapshot, error)

func ListAPIKeys

func ListAPIKeys(ctx context.Context, db *sqlx.DB) ([]*models.APIKey, error)

func ListActiveSessions

func ListActiveSessions(ctx context.Context, db *sqlx.DB) ([]*models.Session, error)

ListActiveSessions returns sessions that are still running (for cleanup jobs).

func ListArtifacts

func ListArtifacts(ctx context.Context, db *sqlx.DB, actor string, limit, offset int) ([]*models.SharedArtifact, error)

func ListAuditLogs

func ListAuditLogs(ctx context.Context, db *sqlx.DB, sessionID *uuid.UUID, actor string, limit, offset int) ([]*models.AuditLog, error)

ListAuditLogs returns audit log entries. When actor is non-empty only that actor's log entries are returned (tenant isolation). When sessionID is set it takes precedence. Master key passes actor="". Initialises the slice to non-nil so JSON always encodes [] instead of null.

func ListAuditSince

func ListAuditSince(ctx context.Context, db *sqlx.DB, since time.Time, limit int) ([]models.AuditLog, error)

ListAuditSince returns up to limit audit entries created after since, ordered by timestamp ASC.

func ListFiles

func ListFiles(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID) ([]*models.File, error)

func ListOrgMembers

func ListOrgMembers(ctx context.Context, db *sqlx.DB, orgID uuid.UUID) ([]*models.OrgMember, error)

func ListOrgSessions

func ListOrgSessions(ctx context.Context, db *sqlx.DB, orgID uuid.UUID, limit, offset int) ([]*models.Session, error)

ListOrgSessions returns active sessions belonging to the given org.

func ListOrgs

func ListOrgs(ctx context.Context, db *sqlx.DB, limit, offset int) ([]*models.Organization, error)

func ListRuns

func ListRuns(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID, limit, offset int) ([]*models.Run, error)

func ListSessions

func ListSessions(ctx context.Context, db *sqlx.DB, actor, labelKey, labelValue string, limit, offset int) ([]*models.Session, error)

ListSessions returns active sessions. When actor is non-empty only that actor's sessions are returned (tenant isolation). Master key passes "". labelKey/labelValue optionally filter by a specific label (both must be set). Initialises the slice to non-nil so JSON always encodes [] instead of null.

func ListSessionsForActor

func ListSessionsForActor(ctx context.Context, db *sqlx.DB, actor, labelKey, labelValue string, limit, offset int) ([]*models.Session, error)

ListSessionsForActor returns all active sessions visible to the actor: own sessions UNION sessions in any org the actor belongs to. When actor is empty (master), all sessions are returned.

func ListSnapshots

func ListSnapshots(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID) ([]*models.Snapshot, error)

func RemoveOrgMember

func RemoveOrgMember(ctx context.Context, db *sqlx.DB, orgID uuid.UUID, principal string) error

func RevokeAPIKey

func RevokeAPIKey(ctx context.Context, db *sqlx.DB, id uuid.UUID) error

func RunMigrations

func RunMigrations(db *sqlx.DB, migrationsPath string) error

func StopSession

func StopSession(ctx context.Context, db *sqlx.DB, id uuid.UUID) error

func SumWorkspaceBytes

func SumWorkspaceBytes(ctx context.Context, db *sqlx.DB, sessionID uuid.UUID) (int64, error)

SumWorkspaceBytes returns the total size in bytes of all files tracked for the given session. Used by the upload handler to enforce MAX_WORKSPACE_MB.

func TotalArtifactBytes

func TotalArtifactBytes(ctx context.Context, db *sqlx.DB, actor string) (int64, error)

TotalArtifactBytes returns the sum of size_bytes for all artifacts owned by actor. Pass an empty actor to sum across all actors (master use).

func UpdateAPIKeyLastUsed

func UpdateAPIKeyLastUsed(ctx context.Context, db *sqlx.DB, id uuid.UUID) error

func UpdateRun

func UpdateRun(ctx context.Context, db *sqlx.DB, p UpdateRunParams) error

func UpdateSessionLabels

func UpdateSessionLabels(ctx context.Context, db *sqlx.DB, id uuid.UUID, labels models.JSONB) error

UpdateSessionLabels replaces the labels map for a session.

func UpdateSessionStatus

func UpdateSessionStatus(ctx context.Context, db *sqlx.DB, id uuid.UUID, status string, containerID *string) error

func UpsertFile

func UpsertFile(ctx context.Context, db *sqlx.DB, f *models.File) error

Types

type OrgWithRole added in v0.3.2

type OrgWithRole struct {
	models.Organization
	Role string `db:"role" json:"role"`
}

OrgWithRole is an organization plus the caller's membership role.

func ListOrgsForActor added in v0.3.2

func ListOrgsForActor(ctx context.Context, db *sqlx.DB, principal string) ([]OrgWithRole, error)

ListOrgsForActor returns organizations the principal belongs to, with role.

type UpdateRunParams

type UpdateRunParams struct {
	ID         uuid.UUID
	Status     string
	ExitCode   *int
	Stdout     *string
	Stderr     *string
	DurationMS *int64
	StartedAt  *time.Time
	FinishedAt *time.Time
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL