From 5332eab258673b4f8cc9554fc38ca2c3a42dfc0f Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Mon, 27 May 2024 10:49:34 -0400 Subject: [PATCH] database: skip invalid values when migrating namespaces Signed-off-by: Eric Callahan --- moonraker/components/authorization.py | 9 ++++++++- moonraker/components/history.py | 13 ++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/moonraker/components/authorization.py b/moonraker/components/authorization.py index 34bc4fd7d..bb3e3efe5 100644 --- a/moonraker/components/authorization.py +++ b/moonraker/components/authorization.py @@ -96,6 +96,8 @@ def migrate(self, last_version: int, db_provider: DBProviderWrapper) -> None: users: Dict[str, Dict[str, Any]] users = db_provider.get_namespace("authorized_users") api_user = users.pop(API_USER, {}) + if not isinstance(api_user, dict): + api_user = {} user_vals: List[Tuple[Any, ...]] = [ UserInfo( username=API_USER, @@ -103,7 +105,12 @@ def migrate(self, last_version: int, db_provider: DBProviderWrapper) -> None: created_on=api_user.get("created_on", time.time()) ).as_tuple() ] - for user in users.values(): + for key, user in users.items(): + if not isinstance(user, dict): + logging.info( + f"Auth migration, skipping invalid value: {key} {user}" + ) + continue user_vals.append(UserInfo(**user).as_tuple()) placeholders = ",".join("?" * len(user_vals[0])) conn = db_provider.connection diff --git a/moonraker/components/history.py b/moonraker/components/history.py index 5638281e1..c01e4060a 100644 --- a/moonraker/components/history.py +++ b/moonraker/components/history.py @@ -65,6 +65,8 @@ def _create_totals_list( maximum = value if total is None else None totals_list.append(("history", key, maximum, total, instance)) for item in aux_totals: + if not isinstance(item, dict): + continue totals_list.append( ( item["provider"], @@ -99,6 +101,10 @@ def migrate(self, last_version: int, db_provider: DBProviderWrapper) -> None: hist_ns: Dict[str, Any] = db_provider.get_item("moonraker", "history", {}) job_totals: Dict[str, Any] = hist_ns.get("job_totals", BASE_TOTALS) aux_totals: List[Dict[str, Any]] = hist_ns.get("aux_totals", []) + if not isinstance(job_totals, dict): + job_totals = dict(BASE_TOTALS) + if not isinstance(aux_totals, list): + aux_totals = [] totals_list = _create_totals_list(job_totals, aux_totals) sql_conn = db_provider.connection with sql_conn: @@ -139,7 +145,12 @@ def migrate(self, last_version: int, db_provider: DBProviderWrapper) -> None: for batch in db_provider.iter_namespace("history", 1000): conv_vals: List[Tuple[Any, ...]] = [] entry: Dict[str, Any] - for entry in batch.values(): + for key, entry in batch.items(): + if not isinstance(entry, dict): + logging.info( + f"History migration, skipping invalid value: {key} {entry}" + ) + continue try: conv_vals.append( (