Bandera.Usage.Ecto (bandera v0.5.0)

Copy Markdown View Source

Durable DB backend for Bandera.Usage.

Called by Bandera.Usage to seed ETS from the DB once the Repo is up, and periodically to flush the whole ETS table back, so evaluation history survives process restarts and pod recycling.

The flush is last-writer-wins: across multiple pods the DB row for a flag ends up holding whichever pod's value was written most recently. At 30-day stale-detection granularity that ~flush-interval skew is irrelevant, and on startup every pod re-seeds from the DB and keeps the newer of (DB, in-memory), so values only ever move forward in practice.

The table is separate from the flags table — create it via Bandera.Ecto.Migrations.up_usage/0.

Only active when persistence: [adapter: Bandera.Store.Persistent.Ecto] is configured. All functions silently no-op on any DB error so ETS-only operation continues if the table is absent or the DB is unreachable.

Summary

Functions

Upserts every {flag_name, datetime} pair in ets_table into the DB, replacing last_evaluated_at (last-writer-wins).

Loads all rows from the DB usage table into ets_table, keeping whichever timestamp is newer. Called once at startup to seed in-memory history.

Functions

flush_all(ets_table)

@spec flush_all(atom()) :: :ok

Upserts every {flag_name, datetime} pair in ets_table into the DB, replacing last_evaluated_at (last-writer-wins).

load_into_ets(ets_table)

@spec load_into_ets(atom()) :: :ok

Loads all rows from the DB usage table into ets_table, keeping whichever timestamp is newer. Called once at startup to seed in-memory history.