From b8ff450f58e9562fabaa40fe6148bc4718df9c49 Mon Sep 17 00:00:00 2001 From: Al Bailey Date: Wed, 18 Jan 2023 14:19:49 +0000 Subject: [PATCH] Add support for SQLAlchemy 1.4 to NFV An internal variable was dropped in SQLALchemy 1.4. This update attempts to use the newer syntax from 1.4 and falls back to the older syntax if the newer mechanism cannot be used. This will allow StarlingX to migrate to a newer version of SQLALchemy without breaking the NFV database code. Test Plan: PASS: verify coverage using SQLAlchemy 1.2 that the exception code is executed and unit tests pass. PASS: verify coverage using SQLAlchemy 1.4 that the exception code is not executed and unit tests pass. PASS: Build/Install/Bootstrap/Unlock AIO-SX to verify existing runtime behaviour is not impacted. Story: 2010531 Task: 47237 Signed-off-by: Al Bailey Change-Id: I4063dac0b3229b4c1fdb6c5121154665ffc32903 --- .../nfv_unit_tests/tests/test_database_upgrades.py | 14 ++++++++++++++ nfv/nfv-vim/nfv_vim/database/model/_base.py | 13 ++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/nfv/nfv-tests/nfv_unit_tests/tests/test_database_upgrades.py b/nfv/nfv-tests/nfv_unit_tests/tests/test_database_upgrades.py index 95739fcf..225a0e07 100755 --- a/nfv/nfv-tests/nfv_unit_tests/tests/test_database_upgrades.py +++ b/nfv/nfv-tests/nfv_unit_tests/tests/test_database_upgrades.py @@ -26,6 +26,20 @@ class TestNFVDatabaseUpgrade(testcase.NFVTestCase): super(TestNFVDatabaseUpgrade, self).tearDown() shutil.rmtree(self.db_dir) + def test_nfv_vim_database_load_and_dump(self): + """ + Test VIM database load + """ + root_dir = os.environ['VIRTUAL_ENV'] + config = dict() + config['database_dir'] = self.db_dir + database.database_initialize(config) + data_input = "%s/nfv_vim_db_stx_19.12" % root_dir + data_output = "%s/nfv_vim_db_stx_19.12.dump" % root_dir + database.database_load_data(data_input) + database.database_dump_data(data_output) + database.database_finalize() + def test_nfv_vim_database_upgrade_from_19_12(self): """ Test VIM database upgrades from stx 19_12 diff --git a/nfv/nfv-vim/nfv_vim/database/model/_base.py b/nfv/nfv-vim/nfv_vim/database/model/_base.py index 6696cda8..81fb1f63 100755 --- a/nfv/nfv-vim/nfv_vim/database/model/_base.py +++ b/nfv/nfv-vim/nfv_vim/database/model/_base.py @@ -23,8 +23,19 @@ class AsDictMixin(object): setattr(self, column, data[column]) +def get_Base_registry(): + try: + # SQLAlchemy 1.4 got rid of _decl_class_registry + # so we need to use the new way if it exists + # otherwise fail over to the old attribute + return Base.registry._class_registry + except AttributeError: # SQLAlchemy <1.4 + return Base._decl_class_registry + + def lookup_class_by_table(table_name): - for c in list(Base._decl_class_registry.values()): + for c in list(get_Base_registry().values()): if hasattr(c, '__table__'): if table_name == str(c.__table__): return c + # TODO(abailey): add an explicit return None