Skip to content

Commit c172fbc

Browse files
committed
Add assertions to make more defensive protection
1 parent 7af59d9 commit c172fbc

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

server/mergin/sync/models.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,8 +1332,9 @@ def create_checkpoint(
13321332

13331333
for item in versioned_delta_items:
13341334
file_path_id = file_path_map.get(item.path)
1335-
if not file_path_id:
1336-
continue
1335+
assert (
1336+
file_path_id
1337+
), f"ProjectFilePath not found for UPDATE_DIFF item '{item.path}' — data inconsistency"
13371338

13381339
# Check if a FileDiff checkpoint already exists
13391340
existing_diff_checkpoint = FileDiff.query.filter_by(
@@ -1344,11 +1345,13 @@ def create_checkpoint(
13441345
# If does not exists, let's create diff with higher rank and some generated path (name of diff file)
13451346
if not existing_diff_checkpoint:
13461347
base_file = FileHistory.get_basefile(file_path_id, checkpoint.end)
1347-
if not base_file:
1348-
continue
1348+
assert (
1349+
base_file
1350+
), f"Basefile not found for file_path_id={file_path_id} — data inconsistency"
13491351

1350-
if not FileDiff.can_create_checkpoint(file_path_id, checkpoint):
1351-
continue
1352+
assert FileDiff.can_create_checkpoint(
1353+
file_path_id, checkpoint
1354+
), f"Cannot create FileDiff checkpoint for file_path_id={file_path_id}"
13521355

13531356
checkpoint_diff = FileDiff(
13541357
basefile=base_file,

server/mergin/sync/public_api_v2_controller.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,11 @@ def get_project_delta(id: str, since: str, to: Optional[str] = None):
465465
f"Failed to get delta changes for project {project.id} between versions {since_version} and {to_version}"
466466
)
467467
abort(422)
468+
except AssertionError as err:
469+
logging.exception(
470+
f"Data inconsistency when getting delta changes for project {project.id}: {str(err)}"
471+
)
472+
abort(422)
468473

469474
return (
470475
DeltaChangeRespSchema().dump(

0 commit comments

Comments
 (0)