city_retrofit/hub/persistence/db_setup.py

71 lines
2.9 KiB
Python
Raw Permalink Normal View History

2023-02-01 06:05:12 -05:00
"""
Database setup
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Concordia CERC group
Project Coder Peter Yefi peteryefi@gmail.com
"""
2023-05-18 12:29:28 -04:00
import logging
2023-05-29 11:13:49 -04:00
from hub.persistence.repository import Repository
2023-02-01 06:05:12 -05:00
from hub.persistence.models import Application
from hub.persistence.models import City
2023-02-01 06:05:12 -05:00
from hub.persistence.models import CityObject
2023-01-24 10:51:50 -05:00
from hub.persistence.models import User
from hub.persistence.models import UserRoles
2023-02-01 06:05:12 -05:00
from hub.persistence.models import SimulationResults
2023-05-19 13:15:40 -04:00
from hub.persistence.repositories.user import User as UserRepository
from hub.persistence.repositories.application import Application as ApplicationRepository
2022-12-07 19:06:17 -05:00
class DBSetup:
2023-05-18 12:29:28 -04:00
"""
Creates a Persistence database structure
"""
2022-12-07 19:06:17 -05:00
2023-01-31 13:11:39 -05:00
def __init__(self, db_name, app_env, dotenv_path, admin_password, application_uuid):
"""
2023-01-31 13:11:39 -05:00
Creates database tables a default admin user and a default admin app with the given password and uuid
2023-05-18 12:29:28 -04:00
:param db_name: database name
:param app_env: application environment type [TEST|PROD]
:param dotenv_path: .env file path
:param admin_password: administrator password for the application uuid
:application_uuid: application uuid
"""
2023-02-01 06:05:12 -05:00
repository = Repository(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
# Create the tables using the models
Application.__table__.create(bind=repository.engine, checkfirst=True)
User.__table__.create(bind=repository.engine, checkfirst=True)
2023-02-01 06:05:12 -05:00
City.__table__.create(bind=repository.engine, checkfirst=True)
CityObject.__table__.create(bind=repository.engine, checkfirst=True)
SimulationResults.__table__.create(bind=repository.engine, checkfirst=True)
self._user_repo = UserRepository(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
self._application_repo = ApplicationRepository(db_name=db_name, app_env=app_env, dotenv_path=dotenv_path)
application_id = self._create_admin_app(self._application_repo, application_uuid)
self._create_admin_user(self._user_repo, admin_password, application_id)
@staticmethod
2023-01-31 13:11:39 -05:00
def _create_admin_app(application_repo, application_uuid):
name = 'AdminTool'
description = 'Admin tool to control city persistence and to test the API v1.4'
2023-05-17 17:10:30 -04:00
logging.info('Creating default admin tool application...')
2023-01-31 13:11:39 -05:00
application = application_repo.insert(name, description, application_uuid)
2023-05-18 12:29:28 -04:00
if isinstance(application, dict):
2023-05-17 17:10:30 -04:00
logging.info(application)
2023-01-31 13:11:39 -05:00
else:
msg = f'Created Admin tool with application_uuid: {application_uuid}'
2023-05-17 17:10:30 -04:00
logging.info(msg)
2023-05-18 12:29:28 -04:00
return application.id
2023-01-31 13:11:39 -05:00
@staticmethod
def _create_admin_user(user_repo, admin_password, application_id):
password = admin_password
2023-05-17 17:10:30 -04:00
logging.info('Creating default admin user...')
2023-01-31 13:11:39 -05:00
user = user_repo.insert('Administrator', password, UserRoles.Admin, application_id)
2023-05-18 12:29:28 -04:00
if isinstance(user, dict):
2023-05-17 17:10:30 -04:00
logging.info(user)
2022-12-14 17:49:29 -05:00
else:
2023-05-18 12:29:28 -04:00
logging.info('Created Admin user')