diff --git a/backend/app/db/functions/condition_functions.py b/backend/app/db/functions/condition_functions.py new file mode 100644 index 00000000..d281b9a4 --- /dev/null +++ b/backend/app/db/functions/condition_functions.py @@ -0,0 +1,12 @@ +from typing import List +from sqlalchemy import insert, delete +from sqlalchemy.orm import Session + +from backend.app.db.connection import db_session, db_read_session +from backend.app.db.models.condition import PropertyConditionSurveyModel + + +def bulk_insert_property_surveys( + session: Session, surveys: List[PropertyConditionSurveyModel] +) -> None: + raise NotImplementedError diff --git a/backend/app/db/models/condition.py b/backend/app/db/models/condition.py new file mode 100644 index 00000000..65e85058 --- /dev/null +++ b/backend/app/db/models/condition.py @@ -0,0 +1,95 @@ +from sqlalchemy import ( + BigInteger, + Column, + Date, + ForeignKey, + Integer, + String, + Enum as SqlEnum, +) +from sqlalchemy.orm import declarative_base, relationship + +from backend.condition.domain.aspect_type import AspectType +from backend.condition.domain.element_type import ElementType + +Base = declarative_base() + +ElementTypeDb = SqlEnum( + ElementType, + name="element_type", + native_enum=True, +) + +AspectTypeDb = SqlEnum( + AspectType, + name="aspect_type", + native_enum=True, +) + + +class PropertyConditionSurveyModel(Base): + __tablename__ = "property_condition_survey" + + id = Column(BigInteger, primary_key=True, autoincrement=True) + uprn = Column(BigInteger, nullable=False) + + date = Column(Date, nullable=False) + source = Column(String, nullable=False) + + elements = relationship( + "ElementModel", + back_populates="survey", + cascade="all, delete-orphan", + ) + + +class ElementModel(Base): + __tablename__ = "element" + + id = Column(BigInteger, primary_key=True, autoincrement=True) + + survey_id = Column( + BigInteger, + ForeignKey("property_condition_survey.id"), + nullable=False, + ) + + element_type = Column(ElementTypeDb, nullable=False) + element_instance = Column(BigInteger, nullable=False) + + survey = relationship( + "PropertyConditionSurveyModel", + back_populates="elements", + ) + + aspect_conditions = relationship( + "AspectConditionModel", + back_populates="element", + cascade="all, delete-orphan", + ) + + +class AspectConditionModel(Base): + __tablename__ = "aspect_condition" + + id = Column(BigInteger, primary_key=True, autoincrement=True) + + element_id = Column( + BigInteger, + ForeignKey("element.id"), + nullable=False, + ) + + aspect_type = Column(AspectTypeDb, nullable=False) + aspect_instance = Column(BigInteger, nullable=False) + + value = Column(String) + quantity = Column(Integer) + install_date = Column(Date) + renewal_year = Column(Integer) + comments = Column(String) + + element = relationship( + "ElementModel", + back_populates="aspect_conditions", + )