diff options
| author | 2024-07-09 19:44:24 +0100 | |
|---|---|---|
| committer | 2024-07-21 13:45:30 +0100 | |
| commit | 6de1e262a478973ff3cec0ca896682c3ecdde090 (patch) | |
| tree | 18416e44e05f4314f5ffaa2b7fc3dbc0b77c8ed5 /backend | |
| parent | Move existing models to schemas namespace (diff) | |
Add alembic boiler plate for migrations
Diffstat (limited to '')
| -rw-r--r-- | backend/models/orm/__init__.py | 7 | ||||
| -rw-r--r-- | backend/models/orm/base.py | 25 | 
2 files changed, 32 insertions, 0 deletions
| diff --git a/backend/models/orm/__init__.py b/backend/models/orm/__init__.py index e69de29..4c8a6b4 100644 --- a/backend/models/orm/__init__.py +++ b/backend/models/orm/__init__.py @@ -0,0 +1,7 @@ +"""Database models.""" + +from .base import Base + +__all__ = ( +    "Base", +) diff --git a/backend/models/orm/base.py b/backend/models/orm/base.py new file mode 100644 index 0000000..adf9270 --- /dev/null +++ b/backend/models/orm/base.py @@ -0,0 +1,25 @@ +"""The base classes for ORM models.""" + +from pydantic import BaseModel +from sqlalchemy.ext.asyncio import AsyncAttrs +from sqlalchemy.orm import DeclarativeBase +from sqlalchemy.schema import MetaData + +NAMING_CONVENTIONS = { +    "ix": "%(column_0_label)s_ix", +    "uq": "%(table_name)s_%(column_0_name)s_uq", +    "ck": "%(table_name)s_%(constraint_name)s_ck", +    "fk": "%(table_name)s_%(column_0_name)s_%(referred_table_name)s_fk", +    "pk": "%(table_name)s_pk", +} + + +class Base(AsyncAttrs, DeclarativeBase): +    """Classes that inherit this class will be automatically mapped using declarative mapping.""" + +    metadata = MetaData(naming_convention=NAMING_CONVENTIONS) + +    def patch_from_pydantic(self, pydantic_model: BaseModel) -> None: +        """Patch this model using the given pydantic model, unspecified attributes remain the same.""" +        for key, value in pydantic_model.dict(exclude_unset=True).items(): +            setattr(self, key, value) | 
