Improved database connection

This commit is contained in:
Sven Heidemann 2020-11-29 21:48:17 +01:00
parent 78dcbdcbaa
commit 8551a0b7b7
2 changed files with 18 additions and 10 deletions

View File

@ -5,6 +5,7 @@ from sqlalchemy.orm import session, sessionmaker
from sh_edraft.database.base.database_connection_base import DatabaseConnectionBase from sh_edraft.database.base.database_connection_base import DatabaseConnectionBase
from sh_edraft.database.model.database_settings import DatabaseSettings from sh_edraft.database.model.database_settings import DatabaseSettings
from sh_edraft.utils.console import Console
class DatabaseConnection(DatabaseConnectionBase): class DatabaseConnection(DatabaseConnectionBase):
@ -19,6 +20,7 @@ class DatabaseConnection(DatabaseConnectionBase):
self._credentials: Optional[str] = None self._credentials: Optional[str] = None
def create(self): def create(self):
try:
self._engine = create_engine(self._db_settings.decrypted_connection_string) self._engine = create_engine(self._db_settings.decrypted_connection_string)
if self._db_settings.encoding is not None: if self._db_settings.encoding is not None:
@ -30,6 +32,12 @@ class DatabaseConnection(DatabaseConnectionBase):
if self._db_settings.echo is not None: if self._db_settings.echo is not None:
self._engine.echo = self._db_settings.echo self._engine.echo = self._db_settings.echo
self._engine.connect()
db_session = sessionmaker(bind=self._engine) db_session = sessionmaker(bind=self._engine)
self._session = db_session() self._session = db_session()
Console.write_line(f'[{__name__}] Connected to database', 'green')
except Exception as e:
Console.write_line(f'[{__name__}] Database connection failed -> {e}', 'red')
exit()

View File

@ -54,7 +54,7 @@ class Logger(LoggerBase):
# open log file, create if not exists # open log file, create if not exists
path = f'{self._path}{self._log}' path = f'{self._path}{self._log}'
f = open(path, "w+") f = open(path, "w+")
Console.write_line(f'[{__name__}]: Using log file: {path}') Console.write_line(f'[{__name__}]: Using log file: {path}', 'green')
f.close() f.close()
except Exception as e: except Exception as e:
self._fatal_console(__name__, 'Cannot open log file', ex=e) self._fatal_console(__name__, 'Cannot open log file', ex=e)