| 36 | |
| 37 | |
| 38 | class UUID7(Func): |
| 39 | function = "UUIDV7" |
| 40 | arity = 1 |
| 41 | output_field = UUIDField() |
| 42 | |
| 43 | def __init__(self, shift=None, **extra): |
| 44 | super().__init__(shift, **extra) |
| 45 | |
| 46 | def _parse_expressions(self, *expressions): |
| 47 | if expressions[0] is None: |
| 48 | expressions = expressions[1:] |
| 49 | return super()._parse_expressions(*expressions) |
| 50 | |
| 51 | def as_sql(self, compiler, connection, **extra_context): |
| 52 | if not connection.features.supports_uuid7_function: |
| 53 | raise NotSupportedError("UUID7 is not supported on this database backend.") |
| 54 | |
| 55 | if len(self.source_expressions) == 1: |
| 56 | if not connection.features.supports_uuid7_function_shift: |
| 57 | msg = ( |
| 58 | "The shift argument to UUID7 is not supported " |
| 59 | "on this database backend." |
| 60 | ) |
| 61 | raise NotSupportedError(msg) |
| 62 | |
| 63 | return super().as_sql(compiler, connection, **extra_context) |
| 64 | |
| 65 | def as_postgresql(self, compiler, connection, **extra_context): |
| 66 | if connection.features.supports_uuid7_function: |
| 67 | return self.as_sql(compiler, connection, **extra_context) |
| 68 | raise NotSupportedError("UUID7 requires PostgreSQL version 18 or later.") |
| 69 | |
| 70 | # PY314: When dropping support for 3.14, remove the entire method. |
| 71 | def as_sqlite(self, compiler, connection, **extra_context): |
| 72 | if connection.features.supports_uuid7_function: |
| 73 | return self.as_sql(compiler, connection, **extra_context) |
| 74 | raise NotSupportedError( |
| 75 | "UUID7 on SQLite requires Python version 3.14 or later." |
| 76 | ) |
| 77 | |
| 78 | def as_mysql(self, compiler, connection, **extra_context): |
| 79 | if connection.features.supports_uuid7_function: |
| 80 | return self.as_sql( |
| 81 | compiler, connection, function="UUID_V7", **extra_context |
| 82 | ) |
| 83 | if connection.mysql_is_mariadb: |
| 84 | raise NotSupportedError("UUID7 requires MariaDB version 11.7 or later.") |
| 85 | raise NotSupportedError("UUID7 is not supported on MySQL.") |