store

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package store manages the SQLite metadata database for ditto.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

func Open(path string) (*sql.DB, error)

Open opens (or creates) the SQLite database at path, applies WAL mode pragmas, and runs all pending migrations. It must be called once at startup; the returned *sql.DB is safe for concurrent use.

Types

type Copy

type Copy struct {
	ID               string     `json:"id"`
	Status           CopyStatus `json:"status"`
	Port             int        `json:"port"`
	ContainerID      string     `json:"container_id"`
	ConnectionString string     `json:"connection_string"`
	RunID            string     `json:"run_id"`
	JobName          string     `json:"job_name"`
	ErrorMessage     string     `json:"error_message"`
	CreatedAt        time.Time  `json:"created_at"`
	ReadyAt          *time.Time `json:"ready_at"`
	DestroyedAt      *time.Time `json:"destroyed_at"`
	TTLSeconds       int        `json:"ttl_seconds"`
	Warm             bool       `json:"warm"`
}

Copy is the in-memory representation of a row in the copies table.

type CopyStatus

type CopyStatus string

CopyStatus represents the lifecycle state of an ephemeral database copy.

const (
	StatusPending    CopyStatus = "pending"
	StatusCreating   CopyStatus = "creating"
	StatusReady      CopyStatus = "ready"
	StatusInUse      CopyStatus = "in_use"
	StatusDestroying CopyStatus = "destroying"
	StatusDestroyed  CopyStatus = "destroyed"
	StatusFailed     CopyStatus = "failed"
)

type CopyStore

type CopyStore struct {
	// contains filtered or unexported fields
}

CopyStore wraps a *sql.DB and exposes Copy CRUD operations.

func NewCopyStore

func NewCopyStore(db *sql.DB) *CopyStore

NewCopyStore returns a CopyStore backed by db.

func (*CopyStore) ClaimWarm

func (s *CopyStore) ClaimWarm(ttlSeconds int) (*Copy, error)

ClaimWarm atomically finds one StatusReady warm copy, resets its created_at to NOW (restarting the TTL clock from claim time), marks warm=0, and returns it. Returns sql.ErrNoRows if the pool is empty.

func (*CopyStore) CountWarm

func (s *CopyStore) CountWarm() (int, error)

CountWarm returns the number of ready warm copies currently in the pool.

func (*CopyStore) Create

func (s *CopyStore) Create(c *Copy) error

Create inserts a new copy record. c.ID must be set by the caller (ULID).

func (*CopyStore) Get

func (s *CopyStore) Get(id string) (*Copy, error)

Get returns the copy with the given id, or sql.ErrNoRows if not found.

func (*CopyStore) List

func (s *CopyStore) List(filter ListFilter) ([]*Copy, error)

List returns all copies matching filter. Ordered by created_at DESC.

func (*CopyStore) ListExpired

func (s *CopyStore) ListExpired() ([]*Copy, error)

ListExpired returns all READY or IN_USE non-warm copies whose TTL has elapsed.

func (*CopyStore) ListStuck

func (s *CopyStore) ListStuck() ([]*Copy, error)

ListStuck returns copies in CREATING or DESTROYING state (possible mid-crash remnants).

func (*CopyStore) UpdateStatus

func (s *CopyStore) UpdateStatus(id string, status CopyStatus, opts ...UpdateOption) error

UpdateStatus updates the status and optionally other fields for a copy.

type Event

type Event struct {
	ID         int64
	EntityType string
	EntityID   string
	Action     string
	Actor      string
	Metadata   map[string]any
	CreatedAt  time.Time
}

Event is a single append-only audit log entry.

type EventStore

type EventStore struct {
	// contains filtered or unexported fields
}

EventStore wraps a *sql.DB and exposes the append-only events log.

func NewEventStore

func NewEventStore(db *sql.DB) *EventStore

NewEventStore returns an EventStore backed by db.

func (*EventStore) Append

func (s *EventStore) Append(entityType, entityID, action, actor string, metadata any) error

Append adds a new event to the log. metadata may be nil or any JSON-marshallable value.

func (*EventStore) List

func (s *EventStore) List(entityID string) ([]*Event, error)

List returns all events for entityID, ordered by created_at ASC.

type ListFilter

type ListFilter struct {
	Statuses []CopyStatus
}

ListFilter controls which copies are returned by List.

type UpdateOption

type UpdateOption func(*updateArgs)

UpdateOption is a functional option for UpdateStatus.

func WithConnectionString

func WithConnectionString(cs string) UpdateOption

func WithContainerID

func WithContainerID(id string) UpdateOption

func WithDestroyedAt

func WithDestroyedAt(t time.Time) UpdateOption

func WithErrorMessage

func WithErrorMessage(msg string) UpdateOption

func WithJobName

func WithJobName(s string) UpdateOption

func WithReadyAt

func WithReadyAt(t time.Time) UpdateOption

func WithRunID

func WithRunID(s string) UpdateOption

Jump to

Keyboard shortcuts

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