Skip to content

Commit d28e9c7

Browse files
committed
Add apply
1 parent d3faa1b commit d28e9c7

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

terminusdb_client/client/Client.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,40 @@ def _convert_diff_document(self, document):
20122012
new_doc = self._conv_to_dict(document)
20132013
return new_doc
20142014

2015+
def apply(self,
2016+
before_version,
2017+
after_version,
2018+
branch=None,
2019+
message=None,
2020+
author=None):
2021+
"""Diff two different commits and apply changes on branch
2022+
2023+
Parameters
2024+
----------
2025+
before_version : string
2026+
Before branch/commit to compare
2027+
after_object : string
2028+
After branch/commit to compare
2029+
branch : string
2030+
Branch to apply to. Optional.
2031+
"""
2032+
self._check_connection()
2033+
branch = branch if branch else self.branch
2034+
return json.loads(
2035+
_finish_response(
2036+
requests.post(
2037+
self._apply_url(branch=branch),
2038+
headers=self._default_headers,
2039+
json={
2040+
"commit_info": self._generate_commit(message, author),
2041+
"before_commit": before_version,
2042+
"after_commit": after_version,
2043+
},
2044+
auth=self._auth(),
2045+
)
2046+
)
2047+
)
2048+
20152049
def diff_object(self, before_object, after_object):
20162050
"""Diff two different objects.
20172051
@@ -2836,14 +2870,16 @@ def _branch_url(self, branch_id: str):
28362870
def _repo_base(self, action: str):
28372871
return self._db_base(action) + f"/{self._repo}"
28382872

2839-
def _branch_base(self, action: str):
2873+
def _branch_base(self, action: str, branch: Optional[str] = None):
28402874
base = self._repo_base(action)
28412875
if self._repo == "_meta":
28422876
return base
28432877
if self._branch == "_commits":
28442878
return base + f"/{self._branch}"
28452879
elif self.ref:
28462880
return base + f"/commit/{self._ref}"
2881+
elif branch:
2882+
return base + f"/branch/{branch}"
28472883
else:
28482884
return base + f"/branch/{self._branch}"
28492885
return base
@@ -2916,6 +2952,9 @@ def _squash_url(self):
29162952
def _diff_url(self):
29172953
return self._branch_base("diff")
29182954

2955+
def _apply_url(self, branch: Optional[str] = None):
2956+
return self._branch_base("apply", branch)
2957+
29192958
def _patch_url(self):
29202959
return self._branch_base("patch")
29212960

terminusdb_client/tests/integration_tests/test_client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_diff_object(docker_url):
119119
assert diff == {'test': {'@before': 'wew', '@after': 'wow', '@op': 'SwapValue'}}
120120

121121

122-
def test_diff_version(docker_url):
122+
def test_diff_apply_version(docker_url):
123123
client = Client(docker_url, user_agent=test_user_agent)
124124
client.connect()
125125
db_name = "philosophers" + str(random())
@@ -147,6 +147,13 @@ def test_diff_version(docker_url):
147147
assert diff[0]['@insert']['name'] == 'Plato'
148148
assert diff[1]['@insert']['name'] == 'Aristotle'
149149

150+
# Apply the differences to main with apply
151+
client.apply("main", "changes", branch='main')
152+
153+
# Diff again
154+
diff_again = client.diff_version("main", "changes")
155+
assert len(diff_again) == 0
156+
150157

151158
def test_log(docker_url):
152159
# create client

0 commit comments

Comments
 (0)