postgres

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SQLStateRestrictViolation   = "23001"
	SQLStateForeignKeyViolation = "23503"
	SQLStateUniqueViolation     = "23505"
)

Variables

View Source
var (
	// ErrProjectDeleteConfirmationRequired protects projects with retained clients.
	ErrProjectDeleteConfirmationRequired = errors.New("project deletion requires its exact current name")
	// ErrProjectArchiveBusy means an archive build holds the client lock.
	ErrProjectArchiveBusy = errors.New("project archive is in progress")
	// ErrArchiveStoreMissing means archive rows exist but no store can remove them.
	ErrArchiveStoreMissing = errors.New("archive storage is not configured")
	// ErrArchiveStoreMismatch means an archive row belongs to another storage backend.
	ErrArchiveStoreMismatch = errors.New("archive storage backend does not match")
	// ErrArchiveContentCleanup means the project row is gone but a ZIP delete failed.
	ErrArchiveContentCleanup = errors.New("archive content cleanup failed")
)

Functions

func AbortArchive

func AbortArchive(ctx context.Context, conn *pgx.Conn, archiveID, clientID uuid.UUID) (removed bool, err error)

AbortArchive releases claimed logs and removes only a building row. A false result (including an already committed ready row) forbids file cleanup.

func BeginArchive

func BeginArchive(ctx context.Context, conn *pgx.Conn, input sqlc.InsertBuildingArchiveParams) (record sqlc.Archive, count int64, err error)

BeginArchive freezes the exact cohort in the same transaction as its row.

func CompleteArchive

func CompleteArchive(ctx context.Context, conn *pgx.Conn, archive ReadyArchive) error

func CreateProjectWithClients

func CreateProjectWithClients(ctx context.Context, pool *pgxpool.Pool, input ProjectWithClientsInput) (sqlc.Project, error)

func CreateUserWithToken

func CreateUserWithToken(ctx context.Context, pool *pgxpool.Pool, input CreateUserWithTokenInput) (sqlc.User, error)

func DeleteProjectWithHistory

func DeleteProjectWithHistory(ctx context.Context, pool *pgxpool.Pool, projectID uuid.UUID, confirmationName, backend string, cleanup func(context.Context, []StoredArchive) error) error

DeleteProjectWithHistory removes the project, every client, and their logs, archive rows and policies. Archive files are deleted only after that commit, while the per-client archive locks are still held. A busy lock leaves the project unchanged. Projects with clients require their exact current name, checked under the project lock so concurrent edits cannot bypass confirmation.

func InsertLogs

func InsertLogs(ctx context.Context, pool *pgxpool.Pool, logs []LogInsert) ([]int64, error)

func IsConstraintViolation

func IsConstraintViolation(err error, code, constraintName string) bool

func ListInstances

func ListInstances(ctx context.Context, pool *pgxpool.Pool, projectID, clientID uuid.UUID) ([]string, error)

func ReplaceLoginToken

func ReplaceLoginToken(ctx context.Context, pool *pgxpool.Pool, input ReplaceLoginTokenInput) error

func ReplaceUserProjects

func ReplaceUserProjects(ctx context.Context, pool *pgxpool.Pool, userID uuid.UUID, projectIDs []uuid.UUID) error

func UpdateProjectWithClients

func UpdateProjectWithClients(ctx context.Context, pool *pgxpool.Pool, input ProjectWithClientsInput) (sqlc.Project, error)

func UpdateUserProfileWithProjects

func UpdateUserProfileWithProjects(ctx context.Context, pool *pgxpool.Pool, input UpdateUserProfileWithProjectsInput) (sqlc.User, error)

func WithArchiveClientLock

func WithArchiveClientLock(ctx context.Context, pool *pgxpool.Pool, clientID uuid.UUID, fn func(*pgx.Conn) error) (locked bool, err error)

WithArchiveClientLock holds a session lock and a dedicated connection for the entire build, but does not keep a transaction open during file IO. All build transactions must use this connection: losing it also loses ownership.

func WithArchiveSchedulerLock

func WithArchiveSchedulerLock(ctx context.Context, pool *pgxpool.Pool, fn func(*pgx.Conn) error) (locked bool, err error)

WithArchiveSchedulerLock lets one server instance run a scheduler pass at a time.

Types

type CreateUserWithTokenInput

type CreateUserWithTokenInput struct {
	ID                 uuid.UUID
	Username           string
	FirstName          string
	LastName           string
	PasswordHash       string
	Role               string
	NeedsPasswordReset bool
	Token              LoginTokenInput
	ProjectIDs         []uuid.UUID
}

type IngestLogInsert

type IngestLogInsert struct {
	EventKey [32]byte
	Log      LogInsert
}

type InsertedIngestLog

type InsertedIngestLog struct {
	Ordinal int64 // One-based position in the input batch.
	ID      int64
}

func InsertIngestLogs

func InsertIngestLogs(ctx context.Context, pool *pgxpool.Pool, logs []IngestLogInsert) ([]InsertedIngestLog, error)

InsertIngestLogs commits new events and skips events still present in logs. Only newly inserted rows are returned, in input order. Archiving removes the event key with the log; this is intentionally not a permanent receipt ledger.

type LogInsert

type LogInsert struct {
	ClientID       uuid.UUID
	InstanceName   string
	OccurredAt     time.Time
	SequenceNumber int64
	Message        string
}

type LogReadQuery

type LogReadQuery struct {
	ProjectID       uuid.UUID
	ClientID        uuid.UUID
	Instances       []string
	FilterInstances bool
	PageSize        int32
	HasCursor       bool
	Backward        bool
	Inclusive       bool
	CursorAt        time.Time
	CursorSequence  int64
	CursorID        *int64
	HasRange        bool
	RangeFrom       time.Time
	RangeTo         time.Time
}

type LogRow

type LogRow struct {
	ID             int64
	ClientID       uuid.UUID
	ProjectID      uuid.UUID
	InstanceName   string
	OccurredAt     time.Time
	SequenceNumber int64
	Message        string
}

func GetLogAnchor

func GetLogAnchor(ctx context.Context, pool *pgxpool.Pool, projectID, clientID uuid.UUID, id int64) (LogRow, error)

func ListLogs

func ListLogs(ctx context.Context, pool *pgxpool.Pool, query LogReadQuery) ([]LogRow, error)

func SearchLogs

func SearchLogs(ctx context.Context, pool *pgxpool.Pool, query LogSearchQuery) ([]LogRow, error)

type LogSearchQuery

type LogSearchQuery struct {
	LogReadQuery
	PrimaryPattern    string
	RemainingPatterns []string
}

type LoginTokenInput

type LoginTokenInput struct {
	Hash      []byte
	ExpiresAt time.Time
}

type ProjectWithClientsInput

type ProjectWithClientsInput struct {
	ID      uuid.UUID
	Name    string
	Clients []string
}

type ReadyArchive

type ReadyArchive struct {
	ID        uuid.UUID
	ClientID  uuid.UUID
	SizeBytes int64
	SHA256    []byte
	LogCount  int64
	FromAt    time.Time
	ToAt      time.Time
}

type ReplaceLoginTokenInput

type ReplaceLoginTokenInput struct {
	UserID       uuid.UUID
	PasswordHash string
	Token        LoginTokenInput
}

type StoredArchive

type StoredArchive struct {
	Backend string
	Key     string
}

StoredArchive is a committed archive object that must be removed after the database delete commits. Keys stay internal to the configured backend.

type UpdateUserProfileWithProjectsInput

type UpdateUserProfileWithProjectsInput struct {
	ID         uuid.UUID
	Username   string
	FirstName  string
	LastName   string
	Role       string
	ProjectIDs []uuid.UUID
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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