SQL Administration API
One-off administration calls for a PostgreSQL schema: create or verify the schema, and
grant or revoke tenant access when Row-Level Security (RLS) is enabled. They take an
admin_url (a connection that can create objects and grants), build their own short-lived
engine and never store the URL. They are synchronous only. For the walkthrough, including
who runs what, see the SQL graph backends guide.
All of them are importable from scinr.newton.
scinr.newton.sql.admin
sql/admin.py — Administration of a Postgres schema: setup, RLS and per-tenant grants.
An administrator runs :func:setup_sql_schema once (optionally with enable_rls=True) and
:func:grant_tenant_access for every role/tenant pair, with a connection that can create
objects and grants (admin_url). The runtime process then connects with a restricted role
and newton never needs DDL privileges again.
Newton never creates roles, changes passwords or stores them (decision P8): the runtime role
is created by hand (CREATE ROLE <r> LOGIN PASSWORD '...'). Every function here receives
admin_url, builds its own short-lived engine, closes it before returning and forgets the
URL: it is never stored in Config, never logged and never part of an error message.
These are one-off administration calls, so there are no async versions (documented
exception to decision 17 of the functional plan).
RLS semantics (see also :func:scinr.newton.sql.schema.rls_enabled): a tenant is granted to a
role in newton_tenant_access; tenant_id=None (public data, stored as __public__) is
stored as '*' there.
SchemaStatus
dataclass
State of a schema after :func:setup_sql_schema.
TenantAccess
dataclass
One grant of a role on a tenant (tenant_id is None for public data).
setup_sql_schema
setup_sql_schema(
admin_url: str,
*,
enable_rls: bool = False,
runtime_role: str | None = None,
schema: str | None = None,
fulltext_config: str | None = None,
) -> SchemaStatus
Create or migrate the schema as an administrator, optionally in RLS mode. Idempotent.
With enable_rls=True it also applies the Row-Level-Security policies and gives
runtime_role the privileges it needs (a second role: call again with the other role;
only its grants are added). Grants on tenants are separate:
:func:grant_tenant_access. A schema that is already in RLS mode stays so (turning RLS
off is not supported).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
admin_url
|
str
|
PostgreSQL URL of a user that can create objects. Used and forgotten. |
required |
enable_rls
|
bool
|
Turn on RLS mode. |
False
|
runtime_role
|
str | None
|
The restricted role newton connects with. Required with |
None
|
schema
|
str | None
|
Postgres schema. Default: |
None
|
fulltext_config
|
str | None
|
Text-search configuration used when the schema is created.
Default: |
None
|
Returns:
| Type | Description |
|---|---|
SchemaStatus
|
The state of the schema. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If admin_url is not PostgreSQL, |
GraphError
|
If the database fails (the message never contains the URL). |
grant_tenant_access
grant_tenant_access(
admin_url: str,
role: str,
tenant_id: str | None,
*,
write: bool = False,
schema: str | None = None,
) -> None
Let role read (and, with write=True, write) the data of tenant_id.
tenant_id=None grants the public data. Granting again changes the write flag.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the role or tenant is invalid, admin_url is not PostgreSQL, or the
schema is not in RLS mode ( |
GraphError
|
If the database fails. |
revoke_tenant_access
revoke_tenant_access(
admin_url: str,
role: str,
tenant_id: str | None,
*,
schema: str | None = None,
) -> None
Remove the grant of role on tenant_id (None: public). Revoking nothing is fine.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the role or tenant is invalid, admin_url is not PostgreSQL, or the schema is not in RLS mode. |
GraphError
|
If the database fails. |
list_tenant_access
list_tenant_access(
admin_url: str,
*,
role: str | None = None,
schema: str | None = None,
) -> list[TenantAccess]
The grants of role (or of every role), ordered by role and tenant.
Raises:
| Type | Description |
|---|---|
ValueError
|
If admin_url is not PostgreSQL, role is invalid, or the schema is not in RLS mode. |
GraphError
|
If the database fails. |