Skip to content

Commit 409eb5e

Browse files
committed
Fix issue 343 - renaming within a namespace/db now allowed
1 parent 16d479e commit 409eb5e

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

arctic/arctic.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,21 @@ def rename_library(self, from_lib, to_lib):
322322
to_lib: str
323323
The new name of the library
324324
"""
325+
to_colname = to_lib
326+
if '.' in from_lib and '.' in to_lib:
327+
if from_lib.split('.')[0] != to_lib.split('.')[0]:
328+
raise ValueError("Collection can only be renamed in the same database")
329+
to_colname = to_lib.split('.')[1]
330+
325331
l = ArcticLibraryBinding(self, from_lib)
326332
colname = l.get_top_level_collection().name
327-
logger.info('Dropping collection: %s' % colname)
328-
l._db[colname].rename(to_lib)
333+
334+
logger.info('Renaming collection: %s' % colname)
335+
l._db[colname].rename(to_colname)
329336
for coll in l._db.collection_names():
330337
if coll.startswith(colname + '.'):
331-
l._db[coll].rename(coll.replace(from_lib, to_lib))
338+
l._db[coll].rename(coll.replace(colname, to_colname))
339+
332340
if from_lib in self._library_cache:
333341
del self._library_cache[from_lib]
334342
del self._library_cache[l.get_name()]

tests/integration/test_arctic.py

+19
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ def test_lib_rename(arctic):
168168
assert('test' not in arctic.list_libraries())
169169

170170

171+
def test_lib_rename_namespace(arctic):
172+
arctic.initialize_library('namespace.test')
173+
l = arctic['namespace.test']
174+
l.write('test_data', 'abc')
175+
176+
with pytest.raises(ValueError) as e:
177+
arctic.rename_library('namespace.test', 'new_namespace.test')
178+
assert('Collection can only be renamed in the same database' in str(e))
179+
180+
arctic.rename_library('namespace.test', 'namespace.newlib')
181+
l = arctic['namespace.newlib']
182+
assert(l.read('test_data').data == 'abc')
183+
184+
with pytest.raises(LibraryNotFoundException) as e:
185+
l = arctic['namespace.test']
186+
assert('Library namespace.test' in str(e))
187+
assert('namespace.test' not in arctic.list_libraries())
188+
189+
171190
def test_lib_type(arctic):
172191
arctic.initialize_library('test')
173192
assert(arctic.get_library_type('test') == VERSION_STORE)

0 commit comments

Comments
 (0)