Skip to content

Commit

Permalink
database: skip invalid values when migrating namespaces
Browse files Browse the repository at this point in the history
Signed-off-by:  Eric Callahan <[email protected]>
  • Loading branch information
Arksine committed May 27, 2024
1 parent c958120 commit 5332eab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion moonraker/components/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,21 @@ 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,
password=api_user.get("api_key", uuid.uuid4().hex),
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
Expand Down
13 changes: 12 additions & 1 deletion moonraker/components/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
(
Expand Down

0 comments on commit 5332eab

Please sign in to comment.