Spaces:
Running
Running
Download core/db/base.py from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/db/base.py
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/core/db/base.py
-
curl -L -o base.py https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/core/db/base.py
1.34 kB
| """Declarative base and shared column mixins.""" | |
| from __future__ import annotations | |
| import uuid | |
| from datetime import datetime | |
| from sqlalchemy import DateTime, MetaData, func | |
| from sqlalchemy.dialects.postgresql import UUID | |
| from sqlalchemy.orm import DeclarativeBase, Mapped, declared_attr, mapped_column | |
| NAMING_CONVENTION = { | |
| "ix": "ix_%(column_0_label)s", | |
| "uq": "uq_%(table_name)s_%(column_0_name)s", | |
| "ck": "ck_%(table_name)s_%(constraint_name)s", | |
| "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", | |
| "pk": "pk_%(table_name)s", | |
| } | |
| class Base(DeclarativeBase): | |
| metadata = MetaData(naming_convention=NAMING_CONVENTION) | |
| class TimestampMixin: | |
| created_at: Mapped[datetime] = mapped_column( | |
| DateTime(timezone=True), server_default=func.now(), nullable=False | |
| ) | |
| class TenantMixin: | |
| """Every tenant-owned table carries ``tenant_id`` and gets RLS in its migration. | |
| ``__rls__`` is read by ``core.db.rls.tenant_tables()`` so tests can assert that | |
| every table using this mixin has RLS enabled and forced. | |
| """ | |
| __rls__ = True | |
| def tenant_id(cls) -> Mapped[uuid.UUID]: | |
| return mapped_column(UUID(as_uuid=True), nullable=False, index=True) | |
| def new_id() -> uuid.UUID: | |
| return uuid.uuid4() | |