Upgrading
View SourceWhat a release changes for an application that already runs on kura:
behaviour, settings and their defaults, and new API. CHANGELOG.md lists
a release's changes by commit subject, as git-cliff groups them; this
guide is written by hand and says what to do about them. For the move
from 1.x see MIGRATION-2.0.
After 2.31.1
Behaviour that changes
- The operations of a migration run in the order they are written. Before,
kura worked out an order for the
create_tableoperations from their references and their names and put each in the place of another, so an index written between two tables could run before its table existed. A table that an earlier operation needs is still created first; see Create Table. executestatements are not read. A migration whoseexecuterelied on the tables being created in that worked-out order fails when it is run on an empty database. Put its operations in the order they have to run. kura records a migration by its version, so a database that has already run it is not affected by the edit.
After 2.30.0
Nothing here needs a code change to keep a 2.30 application running, with the exceptions under Behaviour that changes. Several checks are new and off until asked for; see Settings.
Sibling packages
kura_postgres, kura_sqlite and rebar3_kura are released on their own. The releases that exist today keep working with this kura; three things wait for their next release:
- the text-match operators on SQLite (
contains,starts_with,ends_withand their case-insensitive forms) need the next kura_sqlite; - so do the optional backend callbacks under For a backend, which no released kura_sqlite implements yet;
- until the next rebar3_kura, a migration that writes a column's name as a binary makes the generator propose dropping that column and adding it again. Keep column names atoms in migrations for now.
Settings
All are optional and live in the kura application environment.
| Setting | Left unset | Values | kura 3.0 |
|---|---|---|---|
max_page_size | not capped; warns once | a positive integer, or infinity | 1000 |
strict_assoc_cast | a nested cast with no allow-list is allowed; warns once | true, false | true |
require_tenant | a missing tenant is not an error; warns once | true, false | planned: true on a repo that declares a tenancy strategy |
strict_locks | lock text kura cannot read is written as it is; warns once | true, false | true |
encryption_write_format | writes v1; warns once | v1, v2 | v2 |
telemetry_error_detail | as false; no warning | true, false | false |
Unset is not the same as false. An unset setting keeps what 2.30 did and
logs one warning per boot, the first time what it governs happens: a page
of more than 1000 rows, a nested cast with no allow-list, a statement under
a tenant, lock text kura cannot read, an encrypted write. A setting given
any value, the permissive one included, is a decision and logs nothing.
telemetry_error_detail never warns.
require_tenantis also a repo setting, in the repo's own config, which wins over the application environment. See Multitenancy. kura 2.x has nothing a repo declares a tenancy strategy with, so the 3.0 default is a plan. Set totrueit also refuses a query from, and a write to, a schema that neither declares the attribute tenant's field nor is shared, which is otherwise a warning; see Tenancy.max_page_sizeis also an option ofkura_paginator:paginate/3andkura_paginator:cursor_paginate/3, which wins.- kura does not start with a value it cannot read:
{invalid_setting, Setting, Value}. A repo's pool does not start with arequire_tenantthat is not a boolean.
Behaviour that changes
Queries
- A join without
selectreturns thefromschema's columns only. It used to return every joined table's columns, and a joined column of the same name replaced thefromrow's value. - A field of a joined schema that has a
columnof its own is read and filtered by that column. A field two joined schemas store under different columns raises{kura, {ambiguous_field, Field, Columns}}. - A combined query (
union,intersect,except) puts itsORDER BY,LIMITandOFFSETafter the last branch, and parenthesises a branch that has its own. The statement was a syntax error on PostgreSQL before. {Field, in, []}compiles to1 = 0and{Field, not_in, []}to1 = 1, where it was invalid SQL.get,get_byandreloadbind every value: the atomsis_nilandis_not_nilas values are no longer read as operators.- A
?inside a string, a quoted identifier, a dollar-quoted string or a comment of a fragment is left alone. A fragment whose placeholders do not match its parameters raises{kura, {fragment_placeholder_mismatch, Placeholders, Params}}. - A
{fragment, SQL, Params}condition is compiled inside parentheses:WHERE (SQL) AND ..., with the closing parenthesis on a line of its own. The SQL text of every query with a fragment condition changes. Results change only for a fragment with anORoutside any parentheses of its own, one that ends in a--comment, or one whose text is not a single condition. A fragment is combined with the other conditions, kura's own included (the tenant's, the soft-delete filter), as one unit: anORin it stays inside the fragment, a--comment ends with the fragment, and text that is not a single condition, such asa = 1 ORDER BY b, is a syntax error. Check your fragments for those three. To match on a fragment or on another condition, write{'or', [{fragment, SQL, Params}, Condition]}. kura_query:lock/2reads a lock given as text into a lock term:FORwith a strength,OFtables,NOWAITorSKIP LOCKED, one clause or several. Text it cannot read is written into the statement as before;strict_locksdecides whether that warns, passes or raises{kura, {invalid_lock, Text}}. A term that is not a lock raises{kura, {invalid_lock, Lock}}.- A query that uses full-text search, a window function, a vector
distance, a lock or
DISTINCT ONraises{kura, {unsupported_capability, Capability, PoolModule}}when the repo's pool module declares a capability model and not that capability.kura_stream:stream/4raises it forcursors. A pool module withoutcapability_model/0, as every backend released for 2.30 is, is not checked.
Schemas and rows
#kura_field.columnis honoured in every statement and in loaded rows.- The
timestamps/0callback is read; 2.30 declared it and never called it. The fields it lists are stamped. The fields namedinserted_atandupdated_atare stamped as well, as 2.30 stamped them, until kura 3.0: where that differs from the declaration (a field of that name the declaration leaves out, a declaration kura cannot use, or[]on a schema that has them) the schema logs one warning each time it is loaded. Seekura_schema. kura_types:load(decimal, Text)returns the text, exact, for a decimal a driver hands over as text. A loaded row is as it was.- A schema module loaded after kura was first asked about its name is a schema from then on, whichever function is asked first. kura used to remember for the life of the VM that the name was not a schema, which left it a bare table: no redaction and no encrypted-field guard.
- A compiled query and a schema's load plan are kept with a format
version, and one kept by another release of kura, or one kura cannot
read, is dropped and worked out again where it used to be served or to
raise on every use. That covers the format, not a module whose code
changed: after a hot code upgrade of kura or of a schema module, call
kura_query_cache:flush/0, andkura_schema:forget/1for each schema that changed.
Changesets and writes
validate_confirmation/2,3reports a mismatch on<Field>_confirmation, as 2.30 did, wherever an atom of that name exists: in any application whose loaded code names the confirmation, in a schema, a view or the params. kura no longer creates the atom, so where there is none the mismatch is reported onField. A confirmation declared as a field, virtual or not, is always reported on. The confirmation is read from an atom or a binary key of the params.- A database constraint error the changeset does not declare is reported
on a field only when its name is one the database generates for a
single column of the changeset's table, or
<table>_pkey. Any other name is reported onbase; the field used to be guessed from the second word of the name. A failed many-to-many join row is reported on the association. - A changeset of a schema with redacted fields no longer carries the
database's message on
base, which can quote a value: the error isdatabase error <code>. insert_allof[]returns{ok, 0}; entries that do not set the same fields return{error, {entries_differ, Fields, EntryFields}}; a statement over PostgreSQL's bind limit raises{kura, {too_many_parameters, Count, 65535}}.kura_paginatorreturns{error, {invalid_page, Page}},{error, {invalid_page_size, Size}}or{error, {invalid_limit, Limit}}for one that is not a positive integer.
Tenancy
- An attribute tenant whose value is
undefined,nullornilraises{invalid_tenant, Tenant}. It used to count as a tenant, compare the field withNULLand stamp inserts with no owner. - The field of an attribute tenant has to be a field. A name
kura_queryreads as the head of a condition (and,or,not,exists,not_exists,fragment) raises{invalid_tenant, Tenant}where the tenant is set, as a field that is not an atom already did. - Under an attribute tenant a schema is scoped, shared or undeclared, for
its reads and its writes alike; see
The three states of a schema.
Scoped: it declares the tenant's field as a stored field. It is matched
on the field and its inserts are stamped with the tenant, also when it
exports
tenant_scoped() -> false; all tenants' rows are read insidekura_tenant:without_tenant/1. Shared: it exportstenant_scoped() -> falseand does not declare the field. Nothing of it names the tenant. Undeclared: neither. - A statement on an undeclared schema runs as it did in 2.30: the tenant
condition is added to a query,
update_all,delete_all,update,deleteand an upsert and the database decides, so the statement is scoped when the table has the column and refused there when it has not, and aninsertorinsert_allwrites the row without the tenant. New is one warning per boot for each such schema, naming the schema and the field. On a repo whoserequire_tenantistrueeach of them, an insert included, raises{invalid_tenant, Tenant}instead, before anything is sent and, for a write of a changeset, before itsprepare_changesfunctions andbefore_*hooks run. What sends no statement returns as before: anupdatewith nothing to change, a changeset that is not valid, aninsert_allof no entries. kura 3.0 raises on every repo: declare the tenant's field in each schema used under the tenant, or exporttenant_scoped() -> falsefrom a schema every tenant shares. kura judges a query only where it can tell: the query, or a query combined with it, is from a schema and has no join and no CTE. A write is always judged. kura_tenant:with_tenant/2checks the tenant before changing anything and restores the previous one however the function ends. Parallel preloads run in the caller's tenant scope.
Telemetry
- The
error_reasonof the[kura, repo, query]event keeps the error's code and the names in it, and drops its message and detail, for every statement.{telemetry_error_detail, true}restores the whole error for statements on schemas without redacted fields. - The same event's
sourceis the table kura compiled the statement for, where kura built the statement: for a query with a CTE, the table the query is from, not the first one in the CTE's body. For raw SQL it is still read from the text, where it is now the table, not the schema, of a schema-qualified table, a keyword inside a longer word is not taken for one, andIF EXISTSorIF NOT EXISTSafterTABLEis passed over: the source ofDROP TABLE IF EXISTS twasIF.ONLYafterFROM,UPDATEorTABLEis passed over too. - For raw SQL, a keyword inside a string literal, a quoted identifier, a
comment or a dollar-quoted string is not read, so a table named only
there is never the
source. Only the first 8192 bytes of the statement are read. A table name that is not made of ASCII letters, digits and underscores, or that those bytes cut short, givesundefined; such a name used to be reported as it was written. - The same event's
num_rowsis the number of rows a write affected. It was0for a write that returns no rows:insert_all,update_allanddelete_allwithoutreturning, and raw SQL. - The same event's
paramshold~"[redacted]"in place of a redacted field's value. Only a schema that implementsredact/0or has encrypted fields is affected; seekura_redact.
New API
whereandhavingoperatorscontains,starts_with,ends_with,icontains,istarts_with,iends_with;kura_query:escape_like/1. On SQLite they need the next kura_sqlite release.- Lock terms
{Strength, #{tables => Tables, wait => Wait}}and a list of locks;kura_query:lock_clause/1, the lock a term or text is stored as. kura_query:sources/1: every schema and table a query reads, through joins, CTEs, combinations, subqueries and preloads, each with its role (from,join,cte,preload). A later release may add a role.cast_assoc/3,cast_embed/3andkura_changeset:put_assoc/4takecast, the child fields params may set, andwithas{Module, Function}. See Nested Changesets.- Schema callbacks
redact/0andtenant_scoped/0;kura_schema:redacted_fields/1andkura_schema:renamed_columns/1. An embed whose schema has aredact/0that lists a field, or embeds a schema that does at any depth, is redacted whole with its parent. kura_redact:record/2andkura_redact:changeset/1. Both are for logging and never raise. For a schema with redacted fields or with associations, what they cannot read (an error that is not{Field, Message}, a message that is not text, a value under a key that is not a field name) is replaced by~"[redacted]", not passed through, and a message that is a string or an atom comes back as a binary. In theparamsof a changeset whose schema has redacted fields or associations, a value is shown only under a key that names a field, an embed or an association of the schema: the value under a confirmation or a mistyped key is replaced, the key is kept, and an error message that quotes the value is replaced with it. A schema with neither redacted fields nor associations has nothing to hide: its record or changeset is returned as it is. A term that is not a changeset, and a module that is not a schema, give~"[redacted]"underbase.kura_repo_worker:query/4with#{sensitive => true}redacts a raw statement's parameters in telemetry and the query log.kura_tenant:without_tenant/1,kura_tenant:current_scope/0andkura_tenant:adopt/1;with_tenant/2andwithout_tenant/1also take{Module, Function, Args}.require_tenant: a query or changeset write in a process with no tenant raises{kura, {tenant_required, Source}}. It judges a query by everything it reads, joins, CTEs and subqueries included.- A schema whose
tenant_scoped/0returnsfalseis shared by every tenant:require_tenantdoes not ask a tenant for it, and under an attribute tenant a query,update_allordelete_allthat reads only such schemas gets no tenant condition, and a write of its records (insert,update,delete, an upsert,insert_all, the records of an association) is neither matched on the tenant nor stamped with it. Under an attribute tenant a schema without the callback is scoped when it declares the tenant's field and undeclared when it does not, and one that declares the field is scoped whatever the callback returns: it is matched on the field, and all tenants' rows are read insidekura_tenant:without_tenant/1. An undeclared schema is refused on a repo whoserequire_tenantistrue; see Tenancy. A process without a tenant names no field to judge by, so thererequire_tenantexempts a schema that returnsfalsewhatever fields it declares. See Multitenancy. kura_migration:column_name/0: a column in a#kura_column{}, a table constraint, analter_tableoperation, an index or asafe/0entry is an atom or a binary.kura_migration:column_text/1gives either as text.
For a backend
- Capabilities
row_locks,distinct_onandcursors, the optional callbackcapability_model/0, andkura_capabilities:check/2,kura_capabilities:check_query/2andkura_capabilities:model/1. A backend's queries are checked once it declares model1; a model kura does not know keeps the repo's pool from starting. Seekura_capabilities. - Optional
kura_dialectcallbackload_stored/2: a backend without native types turns what it stores (1/0for a boolean, ISO 8601 text for a date or time) into the value kura loads. Core reads no backend's stored forms itself. - Optional
kura_dialectcallbackplaceholder/1, andkura_dialect:placeholder/2, which calls it or writes$N. The migrator writes its version statements with it. #kura_query.lockis a term for every lockkura_query:lock/2takes, text included:~"FOR UPDATE"is stored asfor_update, and text kura cannot read as{fragment, Text}. 2.30 stored the binary. A dialect that writes the field into the statement as it is has to render the term instead: callkura_query:lock_clause/1on it, which also reads a binary in a record built by hand, and write what comes back. See Locks.- The text-match operators (
contains,starts_with,ends_with,icontains,istarts_with,iends_with) are compiled bykura_dialect_pgwithLIKEandILIKE. A backend whose database has noILIKE, or whoseLIKEignores case, has to compile them itself. On SQLite they need the next kura_sqlite release: with kura_sqlite 1.3 the case-insensitive three are written asILIKE, which SQLite refuses, and the other three ignore the case of ASCII letters.