From 346c8264e3a6766bf2f783ef34bab79024824a46 Mon Sep 17 00:00:00 2001 From: VisualDust Date: Mon, 14 Oct 2024 22:35:57 -0400 Subject: [PATCH] added server config option "bypass-db-version-check" --- neetbox/config/user/__init__.py | 22 ++++++++++++++++++++++ neetbox/server/db/project/_project_db.py | 17 +++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/neetbox/config/user/__init__.py b/neetbox/config/user/__init__.py index b719e1eb..78e7585e 100644 --- a/neetbox/config/user/__init__.py +++ b/neetbox/config/user/__init__.py @@ -19,11 +19,31 @@ _GLOBAL_CONFIG = { MACHINE_ID_KEY: str(uuid4()), "vault": get_create_neetbox_data_directory(), + "bypass-db-version-check": False, } _GLOBAL_CONFIG_FILE_NAME = f"neetbox.global.toml" +def update_dict_recursively_on_missing_keys(A, B): + """ + Update dictionary B with keys from dictionary A. Add missing keys from A to B, + but do not overwrite existing keys in B. Handles nested dictionaries recursively. + """ + missed_keys = [] + for key, value in A.items(): + if key not in B: + missed_keys.append(key) + B[key] = value + else: + if isinstance(value, dict) and isinstance(B[key], dict): + missed_keys += update_dict_recursively_on_missing_keys(value, B[key]) + else: + # Do not modify B[key] if it already exists + pass + return missed_keys + + def overwrite_create_local(config: dict): neetbox_config_dir = get_create_neetbox_config_directory() config_file_path = os.path.join(neetbox_config_dir, _GLOBAL_CONFIG_FILE_NAME) @@ -40,7 +60,9 @@ def read_create_local(): # read local file user_cfg = check_read_toml(config_file_path) assert user_cfg + update_dict_recursively_on_missing_keys(_GLOBAL_CONFIG, user_cfg) _GLOBAL_CONFIG.update(user_cfg) + overwrite_create_local(_GLOBAL_CONFIG) def set(key, value): diff --git a/neetbox/server/db/project/_project_db.py b/neetbox/server/db/project/_project_db.py index f05fff11..86dd0a41 100644 --- a/neetbox/server/db/project/_project_db.py +++ b/neetbox/server/db/project/_project_db.py @@ -62,9 +62,14 @@ def __new__(cls, project_id: str = None, path: str = None, **kwargs) -> "Project ) _db_file_version = new_dbc.fetch_db_version(NEETBOX_VERSION) if NEETBOX_VERSION != _db_file_version: - logger.warn( - f"History file version not match: reading from version {_db_file_version} with neetbox version {NEETBOX_VERSION}" - ) + if get_global_config("bypass-db-version-check"): + logger.warn( + f"History file version not match: reading from version {_db_file_version} with neetbox version {NEETBOX_VERSION}" + ) + else: + raise RuntimeError( + f"History file version not match: reading from version {_db_file_version} with neetbox version {NEETBOX_VERSION}. If you want to bypass this check, set 'bypass-db-version-check' to True in global config. This may cause unexpected behavior." + ) cls._path2dbc[path] = new_dbc manager.current[project_id] = new_dbc new_dbc.project_id = project_id @@ -425,7 +430,11 @@ def read_blob( def load_db_of_path(cls, path): if not os.path.isfile(path): raise RuntimeError(f"{path} is not a file") - conn = ProjectDB(path=path) + try: + conn = ProjectDB(path=path) + except Exception as e: + logger.err(f"failed to load db from {path} cause {e}") + return None return conn @classmethod