Accrue.Repo (accrue v0.3.0)

Copy Markdown View Source

Thin facade over the host-configured Ecto.Repo.

Accrue does not ship a Repo of its own (D-10 — the host application owns the Repo lifecycle). Every Accrue context module that needs to talk to Postgres routes through this facade, which resolves the real Repo via Application.get_env(:accrue, :repo) at call time.

Runtime resolution (not compile-time) is deliberate: tests inject Accrue.TestRepo through config/test.exs, and host apps inject their own Repo through config :accrue, :repo, MyApp.Repo without ever recompiling Accrue.

This module intentionally only re-exports the Repo callbacks Accrue itself uses. If you need a lower-level Repo operation, call the host Repo directly.

Summary

Functions

Delegates to Ecto.Repo.aggregate/4. Used by concurrency tests and the billing query layer for count/sum over queryables.

Delegates to Ecto.Repo.all/2. Pass-through.

Delegates to Ecto.Repo.delete/2. Returns {:ok, struct} or {:error, changeset}.

Delegates to Ecto.Repo.get/3. Returns the row or nil.

Delegates to Ecto.Repo.get_by/3. Returns the row or nil.

Delegates to Ecto.Repo.get_by!/3. Raises when the row is missing.

Delegates to Ecto.Repo.insert!/2. Raising variant used inside transact/2 blocks where the happy path is mandatory and failures must abort the whole transaction via exception.

Delegates to Ecto.Repo.one/2. Pass-through.

Delegates to Ecto.Repo.preload/3. Used by Phase 3 Billing context to hydrate associations after processor mutations.

Returns the configured host Repo module. Raises Accrue.ConfigError when :repo is not configured (prevents runtime UndefinedFunctionError with confusing stacktraces).

Delegates to Ecto.Repo.transact/2. Accepts either a zero-arity function (preferred) or a one-arity function that receives the Repo module.

Delegates to Ecto.Repo.transaction/2. Accepts an Ecto.Multi or a function. Used by Accrue.Billing for multi-step atomic writes.

Delegates to Ecto.Repo.update/2 with Postgrex SQLSTATE 45A01 translation. Attempts to update an Accrue.Events.Event will raise Accrue.EventLedgerImmutableError rather than the raw Postgrex.Error.

Delegates to Ecto.Repo.update!/2. Raising variant. Does NOT apply the 45A01 SQLSTATE translation — reserved for the non-raising update/2 path since only test/boot code updates events directly.

Functions

aggregate(queryable, aggregate, field \\ nil, opts \\ [])

@spec aggregate(Ecto.Queryable.t(), atom(), atom() | nil, keyword()) :: term()

Delegates to Ecto.Repo.aggregate/4. Used by concurrency tests and the billing query layer for count/sum over queryables.

all(queryable, opts \\ [])

@spec all(
  Ecto.Queryable.t(),
  keyword()
) :: [any()]

Delegates to Ecto.Repo.all/2. Pass-through.

delete(schema_or_changeset, opts \\ [])

@spec delete(
  Ecto.Schema.t() | Ecto.Changeset.t(),
  keyword()
) :: {:ok, struct()} | {:error, Ecto.Changeset.t()}

Delegates to Ecto.Repo.delete/2. Returns {:ok, struct} or {:error, changeset}.

get(queryable, id, opts \\ [])

@spec get(Ecto.Queryable.t(), term(), keyword()) :: struct() | nil

Delegates to Ecto.Repo.get/3. Returns the row or nil.

get_by(queryable, clauses, opts \\ [])

@spec get_by(Ecto.Queryable.t(), keyword() | map(), keyword()) :: struct() | nil

Delegates to Ecto.Repo.get_by/3. Returns the row or nil.

get_by!(queryable, clauses, opts \\ [])

@spec get_by!(Ecto.Queryable.t(), keyword() | map(), keyword()) :: struct()

Delegates to Ecto.Repo.get_by!/3. Raises when the row is missing.

insert(changeset_or_struct, opts \\ [])

@spec insert(
  Ecto.Changeset.t() | struct(),
  keyword()
) :: {:ok, struct()} | {:error, Ecto.Changeset.t()}

Delegates to Ecto.Repo.insert/2. Pass-through.

insert!(changeset_or_struct, opts \\ [])

@spec insert!(
  Ecto.Changeset.t() | struct(),
  keyword()
) :: struct()

Delegates to Ecto.Repo.insert!/2. Raising variant used inside transact/2 blocks where the happy path is mandatory and failures must abort the whole transaction via exception.

one(queryable, opts \\ [])

@spec one(
  Ecto.Queryable.t(),
  keyword()
) :: any() | nil

Delegates to Ecto.Repo.one/2. Pass-through.

preload(struct_or_list, preloads, opts \\ [])

@spec preload(struct() | [struct()], atom() | list(), keyword()) ::
  struct() | [struct()]

Delegates to Ecto.Repo.preload/3. Used by Phase 3 Billing context to hydrate associations after processor mutations.

repo()

@spec repo() :: module()

Returns the configured host Repo module. Raises Accrue.ConfigError when :repo is not configured (prevents runtime UndefinedFunctionError with confusing stacktraces).

transact(fun, opts \\ [])

@spec transact(
  (-> any()) | (module() -> any()),
  keyword()
) :: {:ok, any()} | {:error, any()}

Delegates to Ecto.Repo.transact/2. Accepts either a zero-arity function (preferred) or a one-arity function that receives the Repo module.

transaction(multi_or_fun, opts \\ [])

@spec transaction(
  Ecto.Multi.t() | (-> any()),
  keyword()
) :: {:ok, any()} | {:error, any()} | {:error, Ecto.Multi.name(), any(), map()}

Delegates to Ecto.Repo.transaction/2. Accepts an Ecto.Multi or a function. Used by Accrue.Billing for multi-step atomic writes.

update(changeset, opts \\ [])

@spec update(
  Ecto.Changeset.t(),
  keyword()
) :: {:ok, struct()} | {:error, Ecto.Changeset.t()}

Delegates to Ecto.Repo.update/2 with Postgrex SQLSTATE 45A01 translation. Attempts to update an Accrue.Events.Event will raise Accrue.EventLedgerImmutableError rather than the raw Postgrex.Error.

Used only by tests and Plan 06 boot-check paths; normal Accrue code MUST NOT call update on an Event — the trigger will reject it.

update!(changeset, opts \\ [])

@spec update!(
  Ecto.Changeset.t(),
  keyword()
) :: struct()

Delegates to Ecto.Repo.update!/2. Raising variant. Does NOT apply the 45A01 SQLSTATE translation — reserved for the non-raising update/2 path since only test/boot code updates events directly.