database
prefect.server.utilities.database
Utilities for interacting with Prefect REST API database and ORM layer.
Prefect supports both SQLite and Postgres. Many of these utilities allow the Prefect REST API to seamlessly switch between the two.
Functions
db_injector
generate_uuid_postgresql
Generates a random UUID in Postgres; requires the pgcrypto extension.
generate_uuid_sqlite
Generates a random UUID in other databases (SQLite) by concatenating bytes in a way that approximates a UUID hex representation. This is sufficient for our purposes of having a random client-generated ID that is compatible with a UUID spec.
bindparams_from_clause
Retrieve all non-anonymous bind parameters defined in a SQL clause
datetime_or_interval_add_postgresql
date_diff_seconds_postgresql
current_timestamp_sqlite
Generates the current timestamp for SQLite
date_add_sqlite
interval_add_sqlite
date_diff_sqlite
date_diff_seconds_sqlite
sqlite_json_operators
Intercept the PostgreSQL-only JSON / JSONB operators and translate them to SQLite
sqlite_greatest_as_max
get_dialect
Get the dialect of a session, engine, or connection url.
Primary use case is figuring out whether the Prefect REST API is communicating with SQLite or Postgres.
Classes
GenerateUUID
Platform-independent UUID default generator.
Note the actual functionality for this class is specified in the
compiles
-decorated functions below
Timestamp
TypeDecorator that ensures that timestamps have a timezone.
For SQLite, all timestamps are converted to UTC (since they are stored as naive timestamps without timezones) and recovered as UTC.
Methods:
load_dialect_impl
process_bind_param
process_result_value
UUID
Platform-independent UUID type.
Uses PostgreSQL’s UUID type, otherwise uses CHAR(36), storing as stringified hex values with hyphens.
Methods:
load_dialect_impl
process_bind_param
process_result_value
JSON
JSON type that returns SQLAlchemy’s dialect-specific JSON types, where possible. Uses generic JSON otherwise.
The “base” type is postgresql.JSONB to expose useful methods prior to SQL compilation
Methods:
load_dialect_impl
process_bind_param
Prepares the given value to be used as a JSON field in a parameter binding
Pydantic
A pydantic type that converts inserted parameters to json and converts read values to the pydantic type.
Methods:
process_bind_param
process_result_value
date_add
Platform-independent way to add a timestamp and an interval
interval_add
Platform-independent way to add two intervals.
date_diff
Platform-independent difference of two timestamps. Computes d1 - d2.
date_diff_seconds
Platform-independent calculation of the number of seconds between two timestamps or from ‘now’