aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Chris Lovering <[email protected]>2024-08-24 23:37:14 +0100
committerGravatar Chris Lovering <[email protected]>2024-08-24 23:37:45 +0100
commit6adc7e46d064a2012059d600e7eff345d5cbcc67 (patch)
tree0ff5625ac8989b64204d47be6cf8329dfef8f334
parentMount db migrations as a volume in dev (diff)
Move UUIDBase to its own mixin
-rw-r--r--thallium-backend/src/orm/base.py7
-rw-r--r--thallium-backend/src/orm/users.py4
-rw-r--r--thallium-backend/src/orm/vouchers.py4
3 files changed, 10 insertions, 5 deletions
diff --git a/thallium-backend/src/orm/base.py b/thallium-backend/src/orm/base.py
index ec79d99..5fd70d6 100644
--- a/thallium-backend/src/orm/base.py
+++ b/thallium-backend/src/orm/base.py
@@ -32,10 +32,15 @@ class Base(AsyncAttrs, DeclarativeBase):
setattr(self, key, value)
+class UUIDBase:
+ """Adds a pre-populated UUID as the tables PK."""
+
+ id: Mapped[UUID] = mapped_column(server_default=text("gen_random_uuid()"), primary_key=True)
+
+
class AuditBase:
"""Common columns for a table with UUID PK and datetime audit columns."""
- id: Mapped[UUID] = mapped_column(server_default=text("gen_random_uuid()"), primary_key=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
diff --git a/thallium-backend/src/orm/users.py b/thallium-backend/src/orm/users.py
index 8f78387..82c46c0 100644
--- a/thallium-backend/src/orm/users.py
+++ b/thallium-backend/src/orm/users.py
@@ -1,9 +1,9 @@
from sqlalchemy.orm import Mapped
-from .base import AuditBase, Base
+from .base import AuditBase, Base, UUIDBase
-class User(AuditBase, Base):
+class User(UUIDBase, AuditBase, Base):
"""An authenticated user of the service."""
__tablename__ = "users"
diff --git a/thallium-backend/src/orm/vouchers.py b/thallium-backend/src/orm/vouchers.py
index 2b3af77..ec0f0df 100644
--- a/thallium-backend/src/orm/vouchers.py
+++ b/thallium-backend/src/orm/vouchers.py
@@ -3,10 +3,10 @@ from decimal import Decimal
from sqlalchemy import Index, text
from sqlalchemy.orm import Mapped, mapped_column
-from .base import AuditBase, Base
+from .base import AuditBase, Base, UUIDBase
-class Voucher(AuditBase, Base):
+class Voucher(UUIDBase, AuditBase, Base):
"""A valid voucher in the database."""
__tablename__ = "vouchers"