kura_repo behaviour (kura v2.31.3)

View Source

Behaviour for defining a repository (database connection).

Implement otp_app/0 to tell Kura which application owns the repo's migrations.

-module(my_repo).
-behaviour(kura_repo).
-export([otp_app/0]).

otp_app() -> my_app.

Configure the database connection under the kura application env. Repos go in a {repos, #{Name => Cfg}} map; Kura starts the pool for each repo automatically during application startup.

[{kura, [
    {repos, #{
        my_repo => #{
            backend => kura_backend_postgres,
            host => "localhost",
            port => 5432,
            database => "my_db",
            user => "postgres",
            password => "secret",
            pool_size => 10
        }
    }}
]}].

The same form scales to multiple repos (e.g. a Postgres primary plus a SQLite analytics store):

[{kura, [
    {repos, #{
        my_repo => #{
            backend => kura_backend_postgres,
            host => "localhost",
            database => "my_db",
            pool_size => 10
        },
        analytics_repo => #{
            backend => kura_backend_sqlite,
            database => <<":memory:">>
        }
    }}
]}].

Each repo's dialect, pool, and driver are resolved from its backend key. Queries through different repos use their own dialects; the query cache is keyed per repo.

For backward compatibility, Kura also accepts the flat single-repo form ({repo, _}/{backend, _}/{host, _} at the kura env level) and the per-app form (application:get_env(OtpApp, RepoModule)). The {repos, ...} map is checked first, then the flat env, then the per-app form. New projects should prefer the map form.

Optionally implement init/1 to modify config at runtime - useful for reading secrets from files, environment variables, or external services. It runs when kura starts the repo's pool, not per query. For a repo in the {repos, ...} map or the flat form that is during kura's own application start, before the application that owns the repo has started, so it must not call that application's processes:

-module(my_repo).
-behaviour(kura_repo).
-export([otp_app/0, init/1]).

otp_app() -> my_app.

init(Config) ->
    Config#{
        password => list_to_binary(os:getenv("DB_PASSWORD", "postgres"))
    }.

Optionally implement migration_apps/0 to run migrations shipped by other applications against the same repo - an extension that owns part of the schema, for instance. The application owning the repo module is always included, so this lists only the extras:

-module(my_repo).
-behaviour(kura_repo).
-export([otp_app/0, migration_apps/0]).

otp_app() -> my_app.

migration_apps() -> [my_gdpr_extension].

kura_migrator orders them by their OTP applications lists, so a dependency's migrations run first. See kura_migrator:migration_apps/1.

Summary

Types

Kura's own settings for a repo, as configured. See settings/1.

Callbacks

Adjust the repo's config at runtime. Optional.

Functions

Read the repo configuration from application environment, with init/1 applied. Each call reads it afresh and runs init/1.

Return true if the repo is configured read_only => true.

Pick a replica repo for RepoMod from its replicas => [Repo] config.

Kura's own settings for the repo: dialect, pool, pool_module, driver_module, read_only, replicas, uuid_version and require_tenant, those it has. Connection settings and secrets are not among them.

Types

settings()

-type settings() ::
          #{dialect => module(),
            pool => atom(),
            pool_module => module(),
            driver_module => module(),
            read_only => boolean(),
            replicas => [module()],
            uuid_version => v4 | v7,
            require_tenant => boolean()}.

Kura's own settings for a repo, as configured. See settings/1.

Callbacks

init(Config)

(optional)
-callback init(Config :: map()) -> map().

Adjust the repo's config at runtime. Optional.

It runs when kura starts the repo's pool (kura_db:start_pool/1, used by application boot and kura_repo_worker:start/1), and on each call to config/1. For a repo in the {repos, ...} map or the flat form, pool start includes kura's own application start. The consuming application has not started then, so init/1 must not call processes it owns. One that raises while the pool is started does not stop kura: the pool is not started, and only the shape of the reason is logged and returned (see kura_db:start_pool/1).

Queries read the repo's settings (settings/1) as they were when the pool started, so init/1 does not run per query. For a repo whose pool kura did not start, they are read through config/1 on each lookup, init/1 included, and a raise there is neither caught nor redacted.

migration_apps()

(optional)
-callback migration_apps() -> [atom()].

otp_app()

-callback otp_app() -> atom().

Functions

config(RepoMod)

-spec config(module()) -> map().

Read the repo configuration from application environment, with init/1 applied. Each call reads it afresh and runs init/1.

read_only(RepoMod)

-spec read_only(module()) -> boolean().

Return true if the repo is configured read_only => true.

A read-only repo rejects writes (insert/update/delete and the bulk and soft-delete variants return {error, read_only}). Reads are unaffected. Intended for a replica repo pointed at a read replica.

replica(RepoMod)

-spec replica(module()) -> module().

Pick a replica repo for RepoMod from its replicas => [Repo] config.

Returns a randomly-chosen replica, or RepoMod itself when none are configured. Routing is explicit: the caller runs reads through the returned repo ((kura_repo:replica(MyRepo)):all(Q)), so the caller owns read-after-write consistency. There is no automatic write/read split.

settings(RepoMod)

-spec settings(module()) -> settings().

Kura's own settings for the repo: dialect, pool, pool_module, driver_module, read_only, replicas, uuid_version and require_tenant, those it has. Connection settings and secrets are not among them.

Once kura_db:start_pool/1 has started the repo's pool, these are the settings it was started with, read without calling init/1; starting the pool again re-reads them. A repo that sets no require_tenant is started with the one in the kura application environment, if there is one (see kura_tenant). For a repo whose pool kura has not started, they come from config/1 on each call. A repo refused a pool that another repo owns raises {pool_owned_by, Owner} until its own pool starts. A repo whose config moved it to a pool that then failed to start raises {pool_not_started, Pool} until a start succeeds.