From: Benjamin Diaz Date: Fri, 8 Feb 2019 16:41:12 +0000 (-0300) Subject: Opens and closes db connections manually to avoid errors when using MySQL backend X-Git-Tag: v6.0.0~20 X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FMON.git;a=commitdiff_plain;h=0c5ec8cea177aae846451601bd62e0c99676c30a Opens and closes db connections manually to avoid errors when using MySQL backend Change-Id: I39f7d17069afa6aaceb643f9ecbfeb5595c4ed9a Signed-off-by: Benjamin Diaz --- diff --git a/osm_mon/core/database.py b/osm_mon/core/database.py index 2a17f9b..3b601b2 100644 --- a/osm_mon/core/database.py +++ b/osm_mon/core/database.py @@ -75,34 +75,44 @@ class DatabaseManager: db.initialize(connect(config.get('sql', 'database_uri'))) def create_tables(self) -> None: + db.connect() with db.atomic(): router = Router(db, os.path.dirname(migrations.__file__)) router.run() + db.close() def get_credentials(self, vim_uuid: str = None) -> VimCredentials: + db.connect() with db.atomic(): - return VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid) + vim_credentials = VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid) + db.close() + return vim_credentials def save_credentials(self, vim_credentials) -> VimCredentials: """Saves vim credentials. If a record with same uuid exists, overwrite it.""" + db.connect() with db.atomic(): exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid) if exists: vim_credentials.id = exists.id vim_credentials.save() - return vim_credentials + db.close() + return vim_credentials def get_alarm(self, alarm_id) -> Alarm: + db.connect() with db.atomic(): alarm = (Alarm.select() .where(Alarm.alarm_id == alarm_id) .get()) - return alarm + db.close() + return alarm def save_alarm(self, name, threshold, operation, severity, statistic, metric_name, vdur_name, vnf_member_index, nsr_id) -> Alarm: """Saves alarm.""" # TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials) + db.connect() with db.atomic(): alarm = Alarm() alarm.uuid = str(uuid.uuid4()) @@ -116,14 +126,17 @@ class DatabaseManager: alarm.vnf_member_index = vnf_member_index alarm.nsr_id = nsr_id alarm.save() - return alarm + db.close() + return alarm def delete_alarm(self, alarm_uuid) -> None: + db.connect() with db.atomic(): alarm = (Alarm.select() .where(Alarm.uuid == alarm_uuid) .get()) alarm.delete_instance() + db.close() def get_vim_type(self, vim_account_id) -> str: """Get the vim type that is required by the message.""" diff --git a/osm_mon/tests/core/test_database.py b/osm_mon/tests/core/test_database.py index 0329c74..3f8eb7e 100644 --- a/osm_mon/tests/core/test_database.py +++ b/osm_mon/tests/core/test_database.py @@ -28,7 +28,7 @@ from osm_mon.core.config import Config from osm_mon.core.database import VimCredentials, DatabaseManager -class DatbaseManagerTest(unittest.TestCase): +class DatabaseManagerTest(unittest.TestCase): def setUp(self): super().setUp() self.config = Config() @@ -42,6 +42,7 @@ class DatbaseManagerTest(unittest.TestCase): mock_creds.password = 'password' mock_creds.tenant_name = 'tenant_name' mock_creds.type = 'openstack' + mock_creds.config = '{}' get_credentials.return_value = mock_creds database_manager = DatabaseManager(self.config)