Skip to content

Commit 2573eb2

Browse files
committed
Try to fix sync issue in python client (#119)
Overwriting geopackages is dangerous and should be avoided. If a geopackage is open in some other client (and there are wal/shm files next to it), it may loose data or even completely break the database file.
1 parent 2943893 commit 2573eb2

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

mergin/client.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ def get_pull_changes(self, server_files):
271271
if not self.geodiff:
272272
return changes
273273

274-
size_limit = int(os.environ.get('DIFFS_LIMIT_SIZE', 1024 * 1024)) # with smaller values than limit download full file instead of diffs
275274
not_updated = []
276275
for file in changes['updated']:
277276
# for small geodiff files it does not make sense to download diff and then apply it (slow)
@@ -294,8 +293,7 @@ def get_pull_changes(self, server_files):
294293
break # we found force update in history, does not make sense to download diffs
295294

296295
if is_updated:
297-
if diffs and file['size'] > size_limit and diffs_size < file['size']/2:
298-
file['diffs'] = diffs
296+
file['diffs'] = diffs
299297
else:
300298
not_updated.append(file)
301299

@@ -455,9 +453,19 @@ def apply_pull_changes(self, changes, temp_dir):
455453
if os.path.exists(f'{dest}-shm'):
456454
os.remove(f'{dest}-shm')
457455
else:
458-
# just use server version of file to update both project file and its basefile
459-
shutil.copy(src, dest)
460-
shutil.copy(src, basefile)
456+
# The local file is not modified -> no rebase needed.
457+
# We just apply the diff between our copy and server to both the local copy and its basefile
458+
try:
459+
server_diff = self.fpath(f'{path}-server_diff', temp_dir) # diff between server file and local basefile
460+
self.geodiff.create_changeset(basefile, src, server_diff)
461+
self.geodiff.apply_changeset(dest, server_diff)
462+
self.geodiff.apply_changeset(basefile, server_diff)
463+
except (pygeodiff.GeoDiffLibError, pygeodiff.GeoDiffLibConflictError):
464+
# something bad happened and we have failed to patch our local files - this should not happen if there
465+
# wasn't a schema change or something similar that geodiff can't handle.
466+
# FIXME: this is a last resort and may corrupt data! (we should warn user)
467+
shutil.copy(src, dest)
468+
shutil.copy(src, basefile)
461469
else:
462470
# backup if needed
463471
if path in modified and item['checksum'] != local_files_map[path]['checksum']:

0 commit comments

Comments
 (0)