Threadline.Retention.Policy (Threadline v0.2.0)

Copy Markdown View Source

Validates config :threadline, :retention before purge runs.

v1.3 exposes a single global retention window (:keep_days or :max_age_seconds, mutually exclusive) plus an enabled flag that must be true for destructive purge. delete_empty_transactions defaults to true (remove parent audit_transactions rows with no remaining children after change deletes).

Per-table / per-tenant overrides are not in v1.3; callers should treat this module as the guardrail for the global policy shape only.

Summary

Types

t()

Normalized retention options as returned by resolve/1.

Functions

Returns UTC DateTime strictly before which AuditChange.captured_at values are considered expired for purge (i.e. delete rows with captured_at < cutoff).

Resolves config into a struct or raises like validate_config!/1.

Validates retention config from Application.get_env(:threadline, :retention).

Types

t()

@type t() :: %Threadline.Retention.Policy{
  delete_empty_transactions: boolean(),
  enabled: boolean(),
  window_seconds: pos_integer()
}

Normalized retention options as returned by resolve/1.

Functions

cutoff_utc_datetime_usec!(opts \\ [])

@spec cutoff_utc_datetime_usec!(keyword()) :: DateTime.t()

Returns UTC DateTime strictly before which AuditChange.captured_at values are considered expired for purge (i.e. delete rows with captured_at < cutoff).

Uses DateTime.add/3 in microsecond mode for consistency with :utc_datetime_usec.

resolve!(opts)

@spec resolve!(keyword() | map()) :: t()

Resolves config into a struct or raises like validate_config!/1.

validate_config!(opts)

@spec validate_config!(keyword() | map()) :: :ok

Validates retention config from Application.get_env(:threadline, :retention).

Raises ArgumentError with a message containing "retention" when the shape is invalid, keys conflict, or the window is not positive.

In :test, missing :keep_days / :max_age_seconds is allowed only when the caller passes a non-empty map/list that still fails other checks — for empty config in test, hosts should set explicit values in config/test.exs.