-
Notifications
You must be signed in to change notification settings - Fork 0
/
databases.py
42 lines (29 loc) · 1.11 KB
/
databases.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flask_sqlalchemy import SQLAlchemy
from logger import setup_logger
services_log = setup_logger('services.log')
APP_NAMES = ['app_RTP', 'app_TTP']
class DB_singleton:
_instances = {}
def __new__(cls, app_name):
if app_name not in cls._instances:
instance = super(DB_singleton, cls).__new__(cls)
instance.db = cls._create_db_instance(app_name)
cls._instances[app_name] = instance
return cls._instances[app_name]
@staticmethod
def _create_db_instance(app_name):
DB_INSTANCE_CREATORS = [create_DB_instance_BA(), create_DB_instance_TTP(), create_DB_instance_RTP()]
try:
index = APP_NAMES.index(app_name)
return DB_INSTANCE_CREATORS[index]
except Exception as e:
services_log.error(
f"Ошибка при создании экземпляра SQLAlchemy для {app_name}: {str(e)}"
)
return None
def create_DB_instance_BA():
return SQLAlchemy()
def create_DB_instance_TTP():
return SQLAlchemy()
def create_DB_instance_RTP():
return SQLAlchemy()