kura_tenant (kura v2.31.3)
View SourceSummary
Types
A process's tenant and whether it is inside without_tenant/1.
What with_tenant/2 and without_tenant/1 run: a fun of no arguments, or
{Module, Function, Args}, applied as Module:Function(Args...).
Functions
Take on a scope read with current_scope/0 in another process, for the
rest of this process's life. Nothing puts the process back: the tenant
stays, and so does the mark of a scope read inside without_tenant/1,
under which require_tenant refuses nothing.
Remove the current tenant strategy from the process dictionary.
The calling process's scope: its tenant and whether it is inside
without_tenant/1. Hand it to adopt/1 in a process that does part of
this one's work, as kura's parallel preloads do. A process that is not
handed the scope has no tenant.
Get the current tenant strategy from the process dictionary.
Set the current tenant strategy in the process dictionary.
Confine Query to the tenant Strategy names.
Run Scoped under Tenant and put the process back as it was, whether
Scoped returns, raises, throws or exits: the tenant it had before, or
none. A tenant set inside Scoped with put_tenant/1 ends with it too.
Run Scoped with no tenant, on purpose: queries and writes inside it are
not scoped, and require_tenant does not refuse them. The process is put
back as it was afterwards, as with with_tenant/2. A with_tenant/2
inside Scoped scopes as usual.
Types
-opaque scope()
A process's tenant and whether it is inside without_tenant/1.
What with_tenant/2 and without_tenant/1 run: a fun of no arguments, or
{Module, Function, Args}, applied as Module:Function(Args...).
Functions
-spec adopt(scope()) -> ok.
Take on a scope read with current_scope/0 in another process, for the
rest of this process's life. Nothing puts the process back: the tenant
stays, and so does the mark of a scope read inside without_tenant/1,
under which require_tenant refuses nothing.
So call it only in a process that ends with the work it was started for,
as a spawned preload or a Task-like worker does. A process that goes on
to other work, such as a pool worker, a job runner or a gen_server,
would do that work under the adopted scope: there, hand the process the
tenant itself (get_tenant/0) and run the work in with_tenant/2, which
ends with it.
Tenant = kura_tenant:get_tenant(),
gen_server:cast(Worker, {run, Tenant, Job}),
handle_cast({run, Tenant, Job}, State) ->
kura_tenant:with_tenant(Tenant, {my_jobs, run, [Job]}),
{noreply, State}.Raises {invalid_tenant, Tenant} for a scope whose tenant put_tenant/1
would refuse.
-spec clear_tenant() -> undefined | strategy().
Remove the current tenant strategy from the process dictionary.
-spec current_scope() -> scope().
The calling process's scope: its tenant and whether it is inside
without_tenant/1. Hand it to adopt/1 in a process that does part of
this one's work, as kura's parallel preloads do. A process that is not
handed the scope has no tenant.
Scope = kura_tenant:current_scope(),
spawn_link(fun() ->
ok = kura_tenant:adopt(Scope),
my_repo:all(kura_query:from(invoice))
end).
-spec get_tenant() -> undefined | strategy().
Get the current tenant strategy from the process dictionary.
Set the current tenant strategy in the process dictionary.
The tenant stays for the life of the process, or until clear_tenant/0 or
another put_tenant/1. That suits a process that serves one request and
ends. A process that outlives the work it was given, such as a pool worker,
a job runner or a gen_server, would carry the tenant into the next piece
of work: there, set the tenant with with_tenant/2, which ends with the
work.
Raises {invalid_tenant, Tenant} on anything that is not a strategy, rather
than storing something that would scope nothing. The attribute value cannot be
is_nil or is_not_nil, which a condition reads as a test rather than a value,
nor undefined, null or nil: a tenant with no value, such as one read from
a claim that was missing, would count as a tenant, match no row and stamp
inserts with no owner. The attribute field is the name of a field and cannot
be and, or, not, exists, not_exists or fragment: a condition reads
those as its own words, so the tenant would scope by whatever stands in place
of the value. with_tenant/2 and adopt/1 refuse the same. undefined in
place of the strategy is accepted and leaves the process without a tenant.
Name the field in your own code. A field taken from a request is not a
tenant: kura checks it against the schema only where it can, and refuses a
field the schema does not declare only on a repo that requires a tenant
(see scope_query/2).
-spec scope_query(undefined | strategy(), #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}.
Confine Query to the tenant Strategy names.
Under {attribute, {Field, Value}} the condition {Field, Value} is added
to the query, to each CTE and combination, and to every in and exists
subquery in its WHERE and HAVING conditions, however deeply nested. The
condition names Field unqualified. A top-level query, CTE or union branch over
a table without it fails. An in or exists subquery over such a table does
not: the database resolves Field to the outer query's column, and the inner
table is not scoped. Any other strategy leaves Query as it is.
The condition goes through the guard of kura_query:where/2, so a Field
that is encrypted in a schema of the query raises instead of binding the
tenant value, also when it is deterministic: writes refuse an encrypted
tenant field, so reads do too.
A Field that put_tenant/1 refuses raises {invalid_tenant, Strategy}.
A query, or a query nested in Query, whose from and joins are all
shared schemas gets no condition: no tenant owns its rows. A shared schema
exports tenant_scoped() -> false and does not declare Field as a
stored field. One other source among them keeps the condition: a schema
that declares Field, whatever its tenant_scoped/0 returns, a schema
without that callback, or a table named by a binary or an atom.
A query from a schema that does not declare Field as a stored field
and is not shared is scoped by Field all the same: the condition is
added and the database decides, so
the query runs when the table has the column and fails there when it has
not. kura logs one warning for each such schema, naming the schema and the
field, where it can tell: the query, or a query combined with it, is from a
schema and has no join and no CTE, either of which can bring the column in.
A query kura compiles for a repo whose require_tenant is true raises
{invalid_tenant, Strategy} there instead, before anything is sent. kura
3.0 raises for every repo.
Run Scoped under Tenant and put the process back as it was, whether
Scoped returns, raises, throws or exits: the tenant it had before, or
none. A tenant set inside Scoped with put_tenant/1 ends with it too.
Scoped is a fun of no arguments or {Module, Function, Args}, which is
applied as it stands and so needs no fun handed across modules. An invalid
Tenant raises before anything changes.
kura_tenant:with_tenant({prefix, ~"tenant_abc"}, fun() -> my_repo:all(Q) end),
kura_tenant:with_tenant({prefix, ~"tenant_abc"}, {my_repo, all, [Q]}).
-spec without_tenant(scoped(Result)) -> Result.
Run Scoped with no tenant, on purpose: queries and writes inside it are
not scoped, and require_tenant does not refuse them. The process is put
back as it was afterwards, as with with_tenant/2. A with_tenant/2
inside Scoped scopes as usual.
Use it for the few paths that must cross tenants. It is the only way to
run unscoped once require_tenant is set.
Scoped is a fun of no arguments or {Module, Function, Args}, as for
with_tenant/2:
kura_tenant:without_tenant(fun() -> my_repo:all(Q) end),
kura_tenant:without_tenant({my_reports, totals, [Month]}).