Skip to content

Commit 8ca68fd

Browse files
authored
Expose overwrite parameter for saving a profile (#2277)
1 parent 0e3f244 commit 8ca68fd

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

tiledb/libtiledb/profile.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ void init_profile(py::module& m) {
3939

4040
.def("_get_param", &tiledb::Profile::get_param, py::arg("param"))
4141

42+
#if TILEDB_VERSION_MAJOR > 2 || \
43+
(TILEDB_VERSION_MAJOR == 2 && TILEDB_VERSION_MINOR >= 30)
44+
.def("_save", &tiledb::Profile::save, py::arg("overwrite"))
45+
#else
4246
.def("_save", &tiledb::Profile::save)
47+
#endif
4348

4449
.def_static(
4550
"_load",

tiledb/profile.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,20 @@ def get(self, param: str, raise_keyerror: bool = True):
7474

7575
return val
7676

77-
def save(self):
77+
def save(self, overwrite: bool = False):
7878
"""Saves the profile to storage.
7979
80+
:param overwrite: Whether to overwrite an existing profile. Defaults to False.
8081
:raises tiledb.TileDBError:
8182
"""
82-
self._save()
83+
if lt.version() >= (2, 30):
84+
self._save(overwrite)
85+
else:
86+
if overwrite:
87+
raise lt.TileDBError(
88+
"The 'overwrite' parameter is only supported in TileDB 2.30.0 and later"
89+
)
90+
self._save()
8391

8492
@classmethod
8593
def load(cls, name: str = None, dir: str = None) -> "Profile":

tiledb/tests/test_profile.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,41 @@ def test_profile_save_load_remove(self):
101101
# remove the profile
102102
tiledb.Profile.remove("profile2_name", self.path("profile2_dir"))
103103

104+
@pytest.mark.skipif(
105+
lt.version() < (2, 30),
106+
reason="Overwrite parameter is only available in TileDB 2.30.0 and later",
107+
)
108+
def test_profile_save_overwrite(self):
109+
token1 = "testing_the_token_1"
110+
token2 = "testing_the_token_2"
111+
profile_name = "overwrite_test_profile"
112+
profile_dir = self.path("overwrite_test_dir")
113+
114+
# Create and save a profile with token1
115+
profile1 = tiledb.Profile(profile_name, profile_dir)
116+
profile1["rest.token"] = token1
117+
profile1.save()
118+
119+
# Load it back to verify it was saved
120+
loaded_profile1 = tiledb.Profile.load(profile_name, profile_dir)
121+
assert loaded_profile1["rest.token"] == token1
122+
123+
# Create a new profile with the same name and try to save without overwrite
124+
profile2 = tiledb.Profile(profile_name, profile_dir)
125+
profile2["rest.token"] = token2
126+
with pytest.raises(tiledb.TileDBError):
127+
profile2.save(overwrite=False)
128+
129+
# Now save with overwrite=True
130+
profile2.save(overwrite=True)
131+
132+
# Load it back to verify it was overwritten
133+
loaded_profile2 = tiledb.Profile.load(profile_name, profile_dir)
134+
assert loaded_profile2["rest.token"] == token2
135+
136+
# Clean up
137+
tiledb.Profile.remove(profile_name, profile_dir)
138+
104139

105140
class ConfigWithProfileTest(ProfileTestCase):
106141
def test_config_with_profile(self):

0 commit comments

Comments
 (0)