Coverage for opt/mealie/lib/python3.12/site-packages/mealie/db/models/_model_utils/guid.py: 92%
36 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
1import uuid
2from typing import Any
4from sqlalchemy import Dialect
5from sqlalchemy.dialects.postgresql import UUID
6from sqlalchemy.types import CHAR, TypeDecorator
9class GUID(TypeDecorator):
10 """Platform-independent GUID type.
11 Uses PostgreSQL's UUID type, otherwise uses
12 CHAR(32), storing as stringified hex values.
13 """
15 impl = CHAR
16 cache_ok = True
18 @staticmethod
19 def generate():
20 return uuid.uuid4()
22 @staticmethod
23 def convert_value_to_guid(value: Any, dialect: Dialect) -> str | None:
24 if value is None:
25 return value
26 elif dialect.name == "postgresql": 26 ↛ 27line 26 didn't jump to line 27 because the condition on line 26 was never true
27 return str(value)
28 else:
29 if not isinstance(value, uuid.UUID):
30 return f"{uuid.UUID(value).int:032x}"
31 else:
32 # hexstring
33 return f"{value.int:032x}"
35 def load_dialect_impl(self, dialect):
36 if dialect.name == "postgresql": 36 ↛ 37line 36 didn't jump to line 37 because the condition on line 36 was never true
37 return dialect.type_descriptor(UUID())
38 else:
39 return dialect.type_descriptor(CHAR(32))
41 def process_bind_param(self, value, dialect):
42 return self.convert_value_to_guid(value, dialect)
44 def _uuid_value(self, value):
45 if value is None:
46 return value
47 else:
48 if not isinstance(value, uuid.UUID):
49 value = uuid.UUID(value)
50 return value
52 def process_result_value(self, value, dialect):
53 return self._uuid_value(value)
55 def sort_key_function(self, value):
56 return self._uuid_value(value)