Skip to content

Commit

Permalink
added server config option "bypass-db-version-check"
Browse files Browse the repository at this point in the history
  • Loading branch information
visualDust committed Oct 15, 2024
1 parent 4b1c795 commit 346c826
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
22 changes: 22 additions & 0 deletions neetbox/config/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
17 changes: 13 additions & 4 deletions neetbox/server/db/project/_project_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 346c826

Please sign in to comment.