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.
GenerateUUID
Bases: FunctionElement
Platform-independent UUID default generator.
Note the actual functionality for this class is specified in the
compiles
-decorated functions below
Source code in src/prefect/server/utilities/database.py
26 27 28 29 30 31 32 33 |
|
JSON
Bases: TypeDecorator
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
Source code in src/prefect/server/utilities/database.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
process_bind_param(value, dialect)
Prepares the given value to be used as a JSON field in a parameter binding
Source code in src/prefect/server/utilities/database.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
Pydantic
Bases: TypeDecorator
A pydantic type that converts inserted parameters to json and converts read values to the pydantic type.
Source code in src/prefect/server/utilities/database.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
Timestamp
Bases: TypeDecorator
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.
Source code in src/prefect/server/utilities/database.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
UUID
Bases: TypeDecorator
Platform-independent UUID type.
Uses PostgreSQL's UUID type, otherwise uses CHAR(36), storing as stringified hex values with hyphens.
Source code in src/prefect/server/utilities/database.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
date_add
Bases: FunctionElement
Platform-independent way to add a date and an interval.
Source code in src/prefect/server/utilities/database.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
date_diff
Bases: FunctionElement
Platform-independent difference of dates. Computes d1 - d2.
Source code in src/prefect/server/utilities/database.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
interval_add
Bases: FunctionElement
Platform-independent way to add two intervals.
Source code in src/prefect/server/utilities/database.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
json_contains
Bases: FunctionElement
Platform independent json_contains operator, tests if the
left
expression contains the right
expression.
On postgres this is equivalent to the @> containment operator. https://www.postgresql.org/docs/current/functions-json.html
Source code in src/prefect/server/utilities/database.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
json_extract
Bases: FunctionElement
Platform independent json_extract operator, extracts a value from a JSON field via key.
On postgres this is equivalent to the ->> operator. https://www.postgresql.org/docs/current/functions-json.html
Source code in src/prefect/server/utilities/database.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
|
json_has_all_keys
Bases: FunctionElement
Platform independent json_has_all_keys operator.
On postgres this is equivalent to the ?& existence operator. https://www.postgresql.org/docs/current/functions-json.html
Source code in src/prefect/server/utilities/database.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
|
json_has_any_key
Bases: FunctionElement
Platform independent json_has_any_key operator.
On postgres this is equivalent to the ?| existence operator. https://www.postgresql.org/docs/current/functions-json.html
Source code in src/prefect/server/utilities/database.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
|
now
Bases: FunctionElement
Platform-independent "now" generator.
Source code in src/prefect/server/utilities/database.py
238 239 240 241 242 243 244 245 246 |
|
get_dialect(obj)
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.
Example
import prefect.settings
from prefect.server.utilities.database import get_dialect
dialect = get_dialect(PREFECT_API_DATABASE_CONNECTION_URL.value())
if dialect.name == "sqlite":
print("Using SQLite!")
else:
print("Using Postgres!")
Source code in src/prefect/server/utilities/database.py
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
|