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,17 +20,24 @@ class DatabaseConnection(DatabaseConnectionBase):
self._credentials: Optional[str] = None self._credentials: Optional[str] = None
def create(self): def create(self):
self._engine = create_engine(self._db_settings.decrypted_connection_string) try:
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:
self._engine.encoding = self._db_settings.encoding self._engine.encoding = self._db_settings.encoding
if self._db_settings.case_sensitive is not None: if self._db_settings.case_sensitive is not None:
self._engine.case_sensitive = self._db_settings.case_sensitive self._engine.case_sensitive = self._db_settings.case_sensitive
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
db_session = sessionmaker(bind=self._engine) self._engine.connect()
self._session = db_session()
db_session = sessionmaker(bind=self._engine)
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)