Skip to content

Commit 76c58c3

Browse files
committed
Adding renaming method
1 parent 1e79243 commit 76c58c3

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

arangoasync/collection.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
CollectionChecksumError,
2020
CollectionConfigureError,
2121
CollectionPropertiesError,
22+
CollectionRenameError,
2223
CollectionResponsibleShardError,
2324
CollectionRevisionError,
2425
CollectionShardsError,
@@ -576,6 +577,44 @@ def response_handler(resp: Response) -> CollectionProperties:
576577

577578
return await self._executor.execute(request, response_handler)
578579

580+
async def rename(self, new_name: str) -> Result[bool]:
581+
"""Rename the collection.
582+
583+
Renames may not be reflected immediately in async execution, batch
584+
execution or transactions. It is recommended to initialize new API
585+
wrappers after a rename.
586+
587+
Note:
588+
Renaming collections is not supported in cluster deployments.
589+
590+
Args:
591+
new_name (str): New collection name.
592+
593+
Returns:
594+
bool: `True` if the collection was renamed successfully.
595+
596+
Raises:
597+
CollectionRenameError: If rename fails.
598+
599+
References:
600+
- `rename-a-collection <https://docs.arangodb.com/stable/develop/http-api/collections/#rename-a-collection>`__
601+
""" # noqa: E501
602+
data: Json = {"name": new_name}
603+
request = Request(
604+
method=Method.PUT,
605+
endpoint=f"/_api/collection/{self.name}/rename",
606+
data=self.serializer.dumps(data),
607+
)
608+
609+
def response_handler(resp: Response) -> bool:
610+
if not resp.is_success:
611+
raise CollectionRenameError(resp, request)
612+
self._name = new_name
613+
self._id_prefix = f"{new_name}/"
614+
return True
615+
616+
return await self._executor.execute(request, response_handler)
617+
579618
async def truncate(
580619
self,
581620
wait_for_sync: Optional[bool] = None,

arangoasync/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ class CollectionPropertiesError(ArangoServerError):
207207
"""Failed to retrieve collection properties."""
208208

209209

210+
class CollectionRenameError(ArangoServerError):
211+
"""Failed to rename collection."""
212+
213+
210214
class CollectionResponsibleShardError(ArangoServerError):
211215
"""Failed to retrieve responsible shard."""
212216

tests/test_collection.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
CollectionChecksumError,
88
CollectionConfigureError,
99
CollectionPropertiesError,
10+
CollectionRenameError,
1011
CollectionResponsibleShardError,
1112
CollectionRevisionError,
1213
CollectionShardsError,
@@ -19,6 +20,7 @@
1920
IndexListError,
2021
IndexLoadError,
2122
)
23+
from tests.helpers import generate_col_name
2224

2325

2426
def test_collection_attributes(db, doc_col):
@@ -77,6 +79,27 @@ async def test_collection_misc_methods(doc_col, bad_col, docs, cluster):
7779
await bad_col.checksum()
7880

7981

82+
@pytest.mark.asyncio
83+
async def test_collection_rename(cluster, db, bad_col, docs):
84+
if cluster:
85+
pytest.skip("Renaming collections is not supported in cluster deployments.")
86+
87+
with pytest.raises(CollectionRenameError):
88+
await bad_col.rename("new_name")
89+
90+
col_name = generate_col_name()
91+
new_name = generate_col_name()
92+
try:
93+
await db.create_collection(col_name)
94+
col = db.collection(col_name)
95+
await col.rename(new_name)
96+
assert col.name == new_name
97+
doc = await col.insert(docs[0])
98+
assert col.get_col_name(doc) == new_name
99+
finally:
100+
db.delete_collection(new_name, ignore_missing=True)
101+
102+
80103
@pytest.mark.asyncio
81104
async def test_collection_index(doc_col, bad_col, cluster):
82105
# Create indexes

0 commit comments

Comments
 (0)