kura_db (kura v2.31.3)

View Source

Central database wrapper. Routes all SQL through a configured kura_driver impl over a configured kura_pool impl, so swapping the underlying client (minato, esqlite, ...) is one config change.

All driver calls go through this module, providing a single place for pool lookup, sandbox support, telemetry, and type loading.

Summary

Functions

The table a statement names first: the name after its first FROM, INTO, UPDATE or TABLE, without quotes and without a schema qualifier, past an IF EXISTS or IF NOT EXISTS after TABLE and an ONLY after FROM, UPDATE or TABLE. undefined when there is none.

Resolve the kura_driver implementation module for a repo.

The pool RepoMod's queries run on.

Resolve the kura_pool implementation module for a repo.

Whether this process is inside transaction/2 on RepoMod's pool. A process it spawns is not, and would not see the transaction's uncommitted work.

Load Row, as a driver returned it, into the fields of SchemaMod. A value kura_types:load/2 cannot read is kept as it is. No dialect is consulted, so a backend's stored forms are not read; kura itself loads through the repo.

Run SQL on Conn, a connection the caller checked out of RepoMod's pool itself, through the repo's driver.

Run SQL on Conn as query_on/4 does, handing Opts to the driver's kura_driver:query_on/4.

Start Repo's pool from its config, as kura_repo:config/1 resolves it.

Stop the pool Repo last started through start_pool/1 and release it.

Run Fun in a transaction on RepoMod's pool, through its driver.

Functions

build_log_event(Repo, SQL, Params, Result, DurationUs)

build_telemetry_metadata(RepoMod, SQL, Params, Result)

default_logger()

extract_source/1

-spec extract_source(iodata()) -> binary() | undefined.

The table a statement names first: the name after its first FROM, INTO, UPDATE or TABLE, without quotes and without a schema qualifier, past an IF EXISTS or IF NOT EXISTS after TABLE and an ONLY after FROM, UPDATE or TABLE. undefined when there is none.

The text of a string literal, a quoted identifier, a comment and a dollar-quoted string is not read for a keyword. Only the first 8192 bytes of the statement are read. A name that is not made of ASCII letters, digits and underscores, or that the 8192 bytes cut short, is undefined too. Never raises.

get_driver_module(RepoMod)

-spec get_driver_module(module()) -> module().

Resolve the kura_driver implementation module for a repo.

get_pool(RepoMod)

-spec get_pool(module()) -> atom().

The pool RepoMod's queries run on.

Raises {pool_owned_by, Owner} when another repo owns that pool name: a repo kura has not started, or one stopped with stop_pool/1, reads its pool name from its config, and must not reach a pool started with another repo's credentials. A repo whose last start was refused raises {pool_owned_by, Owner} too, and one whose last start moved it to a pool that did not start raises {pool_not_started, Pool}, until a start succeeds or stop_pool/1 drops that state.

get_pool_module(RepoMod)

-spec get_pool_module(module()) -> module().

Resolve the kura_pool implementation module for a repo.

in_transaction(RepoMod)

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

Whether this process is inside transaction/2 on RepoMod's pool. A process it spawns is not, and would not see the transaction's uncommitted work.

load_row/2

-spec load_row(kura_dialect:table(), map()) -> map().

Load Row, as a driver returned it, into the fields of SchemaMod. A value kura_types:load/2 cannot read is kept as it is. No dialect is consulted, so a backend's stored forms are not read; kura itself loads through the repo.

query(RepoMod, SQL, Params)

-spec query(module(), iodata(), [term()]) -> map() | {error, term()}.

query_on(RepoMod, Conn, SQL, Params)

-spec query_on(module(), kura_pool:conn(), iodata(), [term()]) -> map() | {error, term()}.

Run SQL on Conn, a connection the caller checked out of RepoMod's pool itself, through the repo's driver.

Unlike query/3 it neither looks for a sandbox connection nor routes to a transaction the process is in: the statement runs on Conn, in whatever transaction state Conn is. Telemetry is the same as for query/3. kura_migrator uses it to run a non-transactional migration and hold its session-level advisory lock on one connection.

Same as query_on/5 with no options.

query_on(RepoMod, Conn, SQL, Params, Opts)

-spec query_on(module(), kura_pool:conn(), iodata(), [term()], kura_driver:opts()) ->
                  map() | {error, term()}.

Run SQL on Conn as query_on/4 does, handing Opts to the driver's kura_driver:query_on/4.

Opts is driver-specific (see kura_driver:opts/0); the minato driver honours timeout. kura's own decode_opts replace any given here, since kura reads the rows in that shape.

start_pool(Repo)

-spec start_pool(module()) -> ok | {error, term()}.

Start Repo's pool from its config, as kura_repo:config/1 resolves it.

Repo:init/1 has run on that config, and the pool takes its name from the pool key, as get_pool/1 does, so queries reach the pool started here. The repo module is loaded first: kura_repo:config/1 only calls an init/1 it finds loaded, and at boot nothing has called into the module yet. Application boot and kura_repo_worker:start/1 both start pools through this function. The pool module applies its own connection defaults.

Deprecated: the legacy flat single-repo form ({repo, Repo} in the kura env, no repos map) on kura_dialect_pg still gets user and database set to "postgres" when it leaves them out, with a warning naming the keys filled. Kura 3.0 removes this.

An init/1 that raises starts no pool. Only the shape of its reason is kept: an atom as it is, a tuple as {Tag, redacted} when its first element is an atom, anything else as redacted. The failure is logged that way and returned as {error, {init_failed, {Class, Shape}}}.

Once the pool runs, the repo's settings are kept (see kura_repo:settings/1) and queries read them from there, so init/1 does not run per query. Starting the pool again, through kura_repo_worker:start/1, resolves the config anew and replaces them.

The repo that starts a pool owns it until stop_pool/1, or until the repo starts on another pool name: the pool it had is then stopped and released, after the new one is started. If that stop raises, the start still returns ok, the failure is logged with only the shape of its reason, and the old pool stays owned until stop_pool/1 stops it. A repo whose config names a pool another repo owns is refused with {error, {pool_owned_by, Owner}} before the pool module is called, so the answer does not depend on how the pool module reports a name that is already running. The refusal is logged as a pool that did not start, and the refused repo's queries raise {pool_owned_by, Owner} instead of running on that pool. The owner starting its pool again gets ok. A pool module whose capability_model/0 returns something this kura has no model for (see kura_capabilities) is not asked to start a pool either: the start returns {error, {invalid_capability_model, PoolModule, Value}} and is logged as a pool that did not start. A pool already running that kura did not start goes to the first repo that starts it, with a warning that names the repo, the pool and the pool module: that pool keeps the connection settings it was started with. A pool stopped outside kura stays owned; stop_pool/1 releases it.

Starts and stops of one pool name are serialised by a node-local global lock that waits without a limit: a pool module whose start_pool/2 or stop_pool/1 hangs holds up every other start or stop of that name. Queries never take the lock. The record of which pool a repo is on is changed under a second lock, per repo and held only for that change, so two starts of one repo on two pool names each find the pool the other moved it to, and the one moved from is stopped.

A start that fails leaves the stored settings as they were when the repo's pool is the one it was started on before, or when only init/1 failed. When the config now names another pool and that pool does not start, the repo's queries raise {pool_not_started, Pool} rather than run on the previous pool.

stop_pool(Repo)

-spec stop_pool(module()) -> ok.

Stop the pool Repo last started through start_pool/1 and release it.

The pool module's stop_pool/1 stops the pool, the pool name is no longer owned by Repo, so another repo can start it, and Repo's stored settings are dropped: its queries read its config on each call again, as for a repo kura has not started. Any other pool Repo still owns, one it moved away from whose stop failed or whose start died before stopping it, is stopped and released too. A repo that does not own a running pool, one refused another repo's pool among them, stops nothing and only has its stored state dropped.

A stop that raises is logged with only the shape of its reason, and that pool stays owned, with the repo's settings kept when it is the pool the repo is on, until a later stop_pool/1 stops it. The other pools are still stopped.

A stopped repo should not keep issuing queries. If another repo starts the same pool name, they raise {pool_owned_by, Owner}; otherwise they fail in the pool module, as for any pool that is not running.

transaction(RepoMod, Fun)

-spec transaction(module(), fun(() -> Result)) -> Result.

Run Fun in a transaction on RepoMod's pool, through its driver.

The driver's kura_driver:transaction/4 decides what a nested call does and what a failure raises. In a sandboxed test the sandbox connection is already inside a transaction, so Fun runs under a SAVEPOINT on it instead, the way a driver runs a nested transaction: an exception rolls back to the savepoint and is re-raised, and a savepoint the database will not release raises transaction_rolled_back after rolling back to it. A statement that ended the sandbox's transaction raises {DriverMod, transaction_ended_by_statement} here too, when the driver checks for it (see kura_sandbox). Another process opening a transaction on the same sandbox connection while this one is open raises {kura_sandbox, overlapping_transactions}.

transaction_ok(RepoMod, Fun)

-spec transaction_ok(module(), fun(() -> term())) -> {ok, term()} | {error, term()}.