From 4331f124e9a4e91208a4c391c392bedbb1dbef19 Mon Sep 17 00:00:00 2001
From: gifrerenom <lluis.gifre@cttc.es>
Date: Fri, 12 May 2023 15:17:30 +0000
Subject: [PATCH] Context component:

- Improved SQLAlchemy engine and database initialization
---
 src/context/service/__main__.py        | 12 ++++++++++--
 src/context/service/database/Engine.py |  6 ------
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/context/service/__main__.py b/src/context/service/__main__.py
index 8b39c0ded..5ac91f233 100644
--- a/src/context/service/__main__.py
+++ b/src/context/service/__main__.py
@@ -44,9 +44,17 @@ def main():
     start_http_server(metrics_port)
 
     # Get Database Engine instance and initialize database, if needed
+    LOGGER.info('Getting SQLAlchemy DB Engine...')
     db_engine = Engine.get_engine()
-    if db_engine is None: return -1
-    Engine.create_database(db_engine)
+    if db_engine is None:
+        LOGGER.error('Unable to get SQLAlchemy DB Engine...')
+        return -1
+
+    try:
+        Engine.create_database(db_engine)
+    except: # pylint: disable=bare-except # pragma: no cover
+        LOGGER.exception('Failed to check/create the database: {:s}'.format(str(db_engine.url)))
+
     rebuild_database(db_engine)
 
     # Get message broker instance
diff --git a/src/context/service/database/Engine.py b/src/context/service/database/Engine.py
index 5cfe7cd4b..592432990 100644
--- a/src/context/service/database/Engine.py
+++ b/src/context/service/database/Engine.py
@@ -42,12 +42,6 @@ class Engine:
             LOGGER.exception('Failed to connect to database: {:s}'.format(str(crdb_uri)))
             return None
 
-        try:
-            Engine.create_database(engine)
-        except: # pylint: disable=bare-except # pragma: no cover
-            LOGGER.exception('Failed to check/create to database: {:s}'.format(str(crdb_uri)))
-            return None
-
         return engine
 
     @staticmethod
-- 
GitLab