From e6ecac528f5c9cefd8d49d1591d8f65a44fcd42e Mon Sep 17 00:00:00 2001 From: Peter Yefi Date: Thu, 3 Nov 2022 12:49:39 -0400 Subject: [PATCH] Included models for persisting city and buildings --- .env | 0 db_config.py | 37 ++++++++++++++++++++++++++++ db_migration.py | 18 ++++++++++++++ persistence/__init__.py | 0 persistence/models/Building.py | 21 ++++++++++++++++ persistence/models/City.py | 21 ++++++++++++++++ persistence/models/__init__.py | 2 ++ persistence/repositories/__init__.py | 0 8 files changed, 99 insertions(+) create mode 100644 .env create mode 100644 db_config.py create mode 100644 db_migration.py create mode 100644 persistence/__init__.py create mode 100644 persistence/models/Building.py create mode 100644 persistence/models/City.py create mode 100644 persistence/models/__init__.py create mode 100644 persistence/repositories/__init__.py diff --git a/.env b/.env new file mode 100644 index 00000000..e69de29b diff --git a/db_config.py b/db_config.py new file mode 100644 index 00000000..d4fc80d2 --- /dev/null +++ b/db_config.py @@ -0,0 +1,37 @@ +""" +Persistence (Postgresql) configuration +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2022 Concordia CERC group +Project Coder Peter Yefi peteryefi@gmail.com +""" + +import os +from dotenv import load_dotenv +from sqlalchemy.ext.declarative import declarative_base + + +Base = declarative_base() + +# load environmental variables +load_dotenv() + + +class BaseConfiguration(object): + """ + Base configuration class to hold common persistence configuration + """ + def __init__(self): + self._db_name = os.getenv('DB_NAME') + self._db_host = os.getenv('DB_HOST') + self._db_user = os.getenv('DB_USER') + self._db_pass = os.getenv('DB_PASSWORD') + self._db_port = os.getenv('DB_PORT') + + def conn_string(self): + """ + Returns a connection string postgresql + :return: connection string + """ + if self._db_pass: + return f'postgresql://{self._db_user}:{self._db_pass}@{self._db_host}:{self._db_port}/{self._db_name}' + return f'postgresql://{self._db_user}@{self._db_host}:{self._db_port}/{self._db_name}' \ No newline at end of file diff --git a/db_migration.py b/db_migration.py new file mode 100644 index 00000000..336328c5 --- /dev/null +++ b/db_migration.py @@ -0,0 +1,18 @@ +""" +Migration script to create postgresql tables +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2022 Concordia CERC group +Project Coder Peter Yefi peteryefi@gmail.com +""" + +from sqlalchemy import create_engine +from db_config import BaseConfiguration +from persistence.models import Building +from persistence.models import City + +if __name__ == '__main__': + config = BaseConfiguration() + engine = create_engine(config.conn_string()) + City.__table__.create(bind=engine, checkfirst=True) + Building.__table__.create(bind=engine, checkfirst=True) + diff --git a/persistence/__init__.py b/persistence/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/persistence/models/Building.py b/persistence/models/Building.py new file mode 100644 index 00000000..ad973d78 --- /dev/null +++ b/persistence/models/Building.py @@ -0,0 +1,21 @@ +""" +Model representation of a building +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2022 Concordia CERC group +Project Coder Peter Yefi peteryefi@gmail.com +""" + +from sqlalchemy import Column, Integer, String, Sequence, Float, ForeignKey +from sqlalchemy.dialects.postgresql import JSONB +from db_config import Base + + +class Building(Base): + __tablename__ = "building" + id = Column(Integer, Sequence('building_id_seq'), primary_key=True) + city_id = Column(Integer, ForeignKey('models.id'), nullable=False) + name = Column(String, nullable=False) + construction_year = Column(Integer, nullable=False) + function = Column(String, nullable=False) + floor_area = Column(Float, nullable=False) + data = Column(JSONB, nullable=False) diff --git a/persistence/models/City.py b/persistence/models/City.py new file mode 100644 index 00000000..2e243304 --- /dev/null +++ b/persistence/models/City.py @@ -0,0 +1,21 @@ +""" +Model representation of a City +SPDX - License - Identifier: LGPL - 3.0 - or -later +Copyright © 2022 Concordia CERC group +Project Coder Peter Yefi peteryefi@gmail.com +""" + +from sqlalchemy import Column, Integer, String, Sequence, Float +from db_config import Base +from sqlalchemy.orm import relationship + + +class City(Base): + __tablename__ = "models" + id = Column(Integer, Sequence('city_id_seq'), primary_key=True) + name = Column(String, nullable=False) + location = Column(String, nullable=False) + country_code = Column(String, nullable=False) + latitude = Column(Float, nullable=False) + longitude = Column(Float, nullable=False) + building = relationship('Building', backref='models', lazy=True, cascade="all, delete-orphan") diff --git a/persistence/models/__init__.py b/persistence/models/__init__.py new file mode 100644 index 00000000..5ed1650e --- /dev/null +++ b/persistence/models/__init__.py @@ -0,0 +1,2 @@ +from .Building import Building +from .City import City diff --git a/persistence/repositories/__init__.py b/persistence/repositories/__init__.py new file mode 100644 index 00000000..e69de29b