diff --git a/server/mergin/sync/files.py b/server/mergin/sync/files.py index f0ee09a1..d22358d5 100644 --- a/server/mergin/sync/files.py +++ b/server/mergin/sync/files.py @@ -286,8 +286,6 @@ def to_data_delta(self): change=self.change, version=self.version, ) - if self.diffs: - result.diff = self.diffs[0].id return result @@ -332,6 +330,9 @@ def make_object(self, data, **kwargs): @post_dump def patch_field(self, data, **kwargs): + assert data.get("change") != PushChangeType.UPDATE_DIFF.value or data.get( + "diff" + ), "Diff file must be provided for update_diff change type" # drop 'diff' key entirely if empty or None as database would expect if not data.get("diff"): data.pop("diff", None) diff --git a/server/mergin/sync/models.py b/server/mergin/sync/models.py index bc01c523..e5f5123d 100644 --- a/server/mergin/sync/models.py +++ b/server/mergin/sync/models.py @@ -1030,7 +1030,9 @@ def construct_checkpoint(self) -> bool: return False project: Project = basefile.file.project - os.makedirs(project.storage.diffs_dir, exist_ok=True) + + # create diffs directory if not exists and subfolders in case of diffs/subfolder/diff-file + os.makedirs(os.path.dirname(self.abs_path), exist_ok=True) try: if len(diffs_paths) == 1: @@ -1041,7 +1043,7 @@ def construct_checkpoint(self) -> bool: project.storage.geodiff.concat_changes(diffs_paths, self.abs_path) except (GeoDiffLibError, GeoDiffLibConflictError): logging.error( - f"Geodiff: Failed to merge diffs for file {self.file_path_id}" + f"Geodiff: Failed to merge diffs for file {self.file_path_id}." ) return False @@ -1357,6 +1359,11 @@ def create_checkpoint( # Patch the delta with the path to the new diff checkpoint item.diff = checkpoint_diff.path db.session.add(checkpoint_diff) + else: + # checkpoint already exists, just patch the delta with the path to the existing diff checkpoint + # this could happen when file diff exists but dela was missing + # this allowing us to remove rank > 0 delta checkpoints in case of inconsistencies + item.diff = existing_diff_checkpoint.path checkpoint_delta = ProjectVersionDelta( project_id=project_id,