db

package
v0.0.0-...-f911ff3 Latest Latest
Warning

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

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

Documentation

Overview

Package db opens the ent client over Postgres (pgx) in production or a pure-Go SQLite database for local dev / tests, and installs the client-level hooks and interceptors that enforce audit logging, append-only ledgers, and per-user tenant isolation.

Tenant protection is intentionally dual-layered: native Ent privacy rejects missing viewers, while client interceptors and hooks add the exact row-level predicates for reads and writes.

Index

Constants

This section is empty.

Variables

View Source
var ErrAccountNotFound = errors.New("db: account not found")

ErrAccountNotFound means that the user row was already gone.

View Source
var ErrNoViewer = errors.New("db: query on per-user entity without a viewer in context")

ErrNoViewer is returned when a per-user entity is queried with neither an authenticated user nor the internal-caller flag in context. It is the ORM-layer guarantee that untrusted code cannot read across tenants.

View Source
var ErrTenantMutation = errors.New("db: tenant mutation ownership mismatch")

ErrTenantMutation is returned when a caller attempts to create or re-parent a tenant-owned row for another user.

View Source
var ErrWorkspaceTransferConflict = errors.New("db: workspace transfer target changed")

ErrWorkspaceTransferConflict means that a workspace owner or the chosen successor changed between planning and deleting the account.

Functions

This section is empty.

Types

type DB

type DB struct {
	Client  *ent.Client
	Dialect string
	// contains filtered or unexported fields
}

DB bundles the ent client with the raw *sql.DB (for health pings) and the dialect in use.

func Open

func Open(ctx context.Context, cfg appconfig.Config, log *zap.Logger) (*DB, error)

Open connects, installs hooks + interceptors, and (optionally) runs auto-migration. An empty DATABASE_URL falls back to a local sqlite file so the service is fully functional in local development with no external DB.

func (*DB) Cached

func (d *DB) Cached(ctx context.Context, ttl time.Duration) context.Context

Cached returns a context that opts this request's queries into the shared cache for the given TTL. Use only on staleness-tolerant reads (e.g. the /v1/me subscription join). Without it, queries hit the database directly.

func (*DB) Close

func (d *DB) Close() error

Close releases the connection pool.

func (*DB) DeleteAccount

func (d *DB) DeleteAccount(ctx context.Context, userID uuid.UUID, transfers []WorkspaceTransfer) error

DeleteAccount removes the user and everything the user owns in one transaction. It runs as raw SQL: user edges are immutable in Ent, the ORM rejects deletes of append-only records, and ON DELETE CASCADE does the rest.

The order is important:

  1. Shared workspaces move to their successors.
  2. In every workspace that survives, the user's authored rows move to the workspace owner, so the cascade does not delete shared data.
  3. History rows go while their source rows still identify the user.
  4. Subscription facts the privacy policy keeps for tax and disputes are copied into billing_retentions. That table has no user foreign key.
  5. The user row goes, and the database cascades the delete.
  6. The user's own history rows go (they hold the email).
  7. A tombstone records the deleted identity, so a token issued before the deletion cannot create the account again.

func (*DB) Ping

func (d *DB) Ping(ctx context.Context) error

Ping verifies connectivity (used by the /readyz probe).

func (*DB) SQLDB

func (d *DB) SQLDB() *sql.DB

SQLDB exposes the shared database/sql pool for small, security-critical primitives that rely on PostgreSQL atomicity rather than tenant Ent models.

type WorkspaceTransfer

type WorkspaceTransfer struct {
	WorkspaceID uuid.UUID
	SuccessorID uuid.UUID
}

WorkspaceTransfer gives a shared workspace to a new owner during account deletion, so the other members keep their data.

Jump to

Keyboard shortcuts

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