Skip to content

Commit 3e97bd7

Browse files
authored
Add function to explicitly allow including only specific tables (#302)
1 parent c8f9931 commit 3e97bd7

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

mergin/merginproject.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,11 +1079,31 @@ def set_tables_to_skip(self, tables):
10791079
10801080
If empty list is passed, list will be reset.
10811081
1082+
This method is mutually exclusive with set_tables_to_include() and error
1083+
GeoDiffLibError will be raised if both are set.
1084+
10821085
:param tables: list of table names to ignore
10831086
:type tables: list[str]
10841087
"""
10851088
self.geodiff.set_tables_to_skip(tables)
10861089

1090+
def set_tables_to_include(self, tables: List[str]):
1091+
"""
1092+
Set list of tables to include in geodiff operations. Once defined, only these
1093+
tables will be included in the following operations: create changeset, apply
1094+
changeset, rebase, get database schema, dump database contents, copy database
1095+
between different drivers.
1096+
1097+
If empty list is passed, list will be reset.
1098+
1099+
This method is mutually exclusive with set_tables_to_skip() and error
1100+
GeoDiffLibError will be raised if both are set.
1101+
1102+
:param tables: list of table names to include
1103+
:type tables: list[str]
1104+
"""
1105+
self.geodiff.set_tables_to_include(tables)
1106+
10871107
def get_geodiff_changes_count(self, diff_rel_path: str):
10881108
"""
10891109
Best-effort: return number of changes in the .gpkg diff (int) or None.

mergin/test/test_mergin_project.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import tempfile
55
import pytest
66
from mergin.merginproject import MerginProject
7+
from pygeodiff import GeoDiffLibError
78
from mergin.common import DeltaChangeType, PullActionType, ClientError
89
from mergin.models import ProjectDeltaChange, ProjectDeltaItemDiff
910
from mergin.client_pull import PullAction
@@ -418,3 +419,55 @@ def username(self):
418419
assert not mp.geodiff.has_changes(os.path.join(tmp_dir, "live-server.diff"))
419420
assert not mp.geodiff.has_changes(os.path.join(tmp_dir, "live-base.diff"))
420421
assert mp.geodiff.has_changes(os.path.join(tmp_dir, "live-conflict.diff"))
422+
423+
424+
def test_set_tables_to_skip():
425+
"""Tables in set_tables_to_skip are excluded from changeset creation."""
426+
base = os.path.join(TEST_DATA_DIR, "two_tables.gpkg")
427+
modified = os.path.join(TEST_DATA_DIR, "two_tables_1_A.gpkg")
428+
429+
with tempfile.TemporaryDirectory() as tmp_dir:
430+
mp = MerginProject(tmp_dir)
431+
diff = os.path.join(tmp_dir, "diff.diff")
432+
433+
mp.geodiff.create_changeset(base, modified, diff)
434+
assert mp.geodiff.has_changes(diff)
435+
436+
mp.set_tables_to_skip(["survey"])
437+
mp.geodiff.create_changeset(base, modified, diff)
438+
assert not mp.geodiff.has_changes(diff)
439+
440+
mp.set_tables_to_skip([])
441+
mp.geodiff.create_changeset(base, modified, diff)
442+
assert mp.geodiff.has_changes(diff)
443+
444+
445+
def test_set_tables_to_include():
446+
"""Only tables in set_tables_to_include appear in the changeset."""
447+
base = os.path.join(TEST_DATA_DIR, "two_tables.gpkg")
448+
modified = os.path.join(TEST_DATA_DIR, "two_tables_1_A.gpkg")
449+
450+
with tempfile.TemporaryDirectory() as tmp_dir:
451+
mp = MerginProject(tmp_dir)
452+
diff = os.path.join(tmp_dir, "diff.diff")
453+
454+
mp.set_tables_to_include(["simple"])
455+
mp.geodiff.create_changeset(base, modified, diff)
456+
assert not mp.geodiff.has_changes(diff)
457+
458+
mp.set_tables_to_include(["survey"])
459+
mp.geodiff.create_changeset(base, modified, diff)
460+
assert mp.geodiff.has_changes(diff)
461+
462+
mp.set_tables_to_include([])
463+
mp.geodiff.create_changeset(base, modified, diff)
464+
assert mp.geodiff.has_changes(diff)
465+
466+
467+
def test_tables_to_skip_and_include_mutually_exclusive():
468+
"""Setting both skip and include lists should raise an error."""
469+
with tempfile.TemporaryDirectory() as tmp_dir:
470+
mp = MerginProject(tmp_dir)
471+
mp.set_tables_to_skip(["table_a"])
472+
with pytest.raises(GeoDiffLibError):
473+
mp.set_tables_to_include(["table_b"])

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
platforms="any",
1717
install_requires=[
1818
"python-dateutil==2.8.2",
19-
"pygeodiff==2.2.0",
19+
"pygeodiff==2.3.0",
2020
"pytz==2022.1",
2121
"click==8.1.3",
2222
],

0 commit comments

Comments
 (0)