Included models for persisting city and buildings
This commit is contained in:
parent
41c66106fa
commit
e6ecac528f
37
db_config.py
Normal file
37
db_config.py
Normal file
|
@ -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}'
|
18
db_migration.py
Normal file
18
db_migration.py
Normal file
|
@ -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)
|
||||
|
0
persistence/__init__.py
Normal file
0
persistence/__init__.py
Normal file
21
persistence/models/Building.py
Normal file
21
persistence/models/Building.py
Normal file
|
@ -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)
|
21
persistence/models/City.py
Normal file
21
persistence/models/City.py
Normal file
|
@ -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")
|
2
persistence/models/__init__.py
Normal file
2
persistence/models/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
from .Building import Building
|
||||
from .City import City
|
0
persistence/repositories/__init__.py
Normal file
0
persistence/repositories/__init__.py
Normal file
Loading…
Reference in New Issue
Block a user