diff --git a/tests/aio/test_table_client.py b/tests/aio/test_table_client.py index 89c0ba75..7316dbdf 100644 --- a/tests/aio/test_table_client.py +++ b/tests/aio/test_table_client.py @@ -89,3 +89,42 @@ async def test_copy_table(self, driver: ydb.aio.Driver): copied_description = await client.describe_table(table_name + "_copy") assert description.columns == copied_description.columns + + @pytest.mark.asyncio + async def test_rename_index(self, driver: ydb.aio.Driver): + client = driver.table_client + table_name = "/local/testtableclient" + try: + await client.drop_table(table_name) + except ydb.SchemeError: + pass + + description = ( + ydb.TableDescription() + .with_primary_keys("key1", "key2") + .with_columns( + ydb.Column("key1", ydb.OptionalType(ydb.PrimitiveType.Uint64)), + ydb.Column("key2", ydb.OptionalType(ydb.PrimitiveType.Uint64)), + ydb.Column("value", ydb.OptionalType(ydb.PrimitiveType.Utf8)), + ) + .with_index(ydb.TableIndex("index1").with_index_columns("key1")) + .with_index(ydb.TableIndex("index2").with_index_columns("key1")) + ) + + await client.create_table(table_name, description) + + await client.alter_table(table_name, rename_indexes=[ydb.RenameIndexItem("index1", "index1_1")]) + + description = await client.describe_table(table_name) + names = [index.name for index in description.indexes] + assert len(names) == 2 + for name in ["index1_1", "index2"]: + assert name in names + + await client.alter_table( + table_name, rename_indexes=[ydb.RenameIndexItem("index1_1", "index2", replace_destination=True)] + ) + + description = await client.describe_table(table_name) + assert len(description.indexes) == 1 + assert description.indexes[0].name == "index2" diff --git a/tests/table/test_table_client.py b/tests/table/test_table_client.py index ef0de906..3fc9e063 100644 --- a/tests/table/test_table_client.py +++ b/tests/table/test_table_client.py @@ -130,3 +130,41 @@ def test_describe_table_creation_time(self, driver_sync: ydb.Driver): assert desc_after.table_stats is not None assert desc_before.table_stats.creation_time == desc_after.table_stats.creation_time + + def test_rename_index(self, driver_sync: ydb.Driver): + client = driver_sync.table_client + table_name = "/local/testtableclient" + try: + client.drop_table(table_name) + except ydb.SchemeError: + pass + + description = ( + ydb.TableDescription() + .with_primary_keys("key1", "key2") + .with_columns( + ydb.Column("key1", ydb.OptionalType(ydb.PrimitiveType.Uint64)), + ydb.Column("key2", ydb.OptionalType(ydb.PrimitiveType.Uint64)), + ydb.Column("value", ydb.OptionalType(ydb.PrimitiveType.Utf8)), + ) + .with_index(ydb.TableIndex("index1").with_index_columns("key1")) + .with_index(ydb.TableIndex("index2").with_index_columns("key1")) + ) + + client.create_table(table_name, description) + + client.alter_table(table_name, rename_indexes=[ydb.RenameIndexItem("index1", "index1_1")]) + + description = client.describe_table(table_name) + names = [index.name for index in description.indexes] + assert len(names) == 2 + for name in ["index1_1", "index2"]: + assert name in names + + client.alter_table( + table_name, rename_indexes=[ydb.RenameIndexItem("index1_1", "index2", replace_destination=True)] + ) + + description = client.describe_table(table_name) + assert len(description.indexes) == 1 + assert description.indexes[0].name == "index2" diff --git a/ydb/_session_impl.py b/ydb/_session_impl.py index a61612bc..4fcd2331 100644 --- a/ydb/_session_impl.py +++ b/ydb/_session_impl.py @@ -268,6 +268,7 @@ def alter_table_request_factory( alter_partitioning_settings, set_key_bloom_filter, set_read_replicas_settings, + rename_indexes, ): request = session_state.attach_request(_apis.ydb_table.AlterTableRequest(path=path)) if add_columns is not None: @@ -316,6 +317,10 @@ def alter_table_request_factory( if set_read_replicas_settings is not None: request.set_read_replicas_settings.MergeFrom(set_read_replicas_settings.to_pb()) + if rename_indexes is not None: + for rename_index in rename_indexes: + request.rename_indexes.add().MergeFrom(rename_index.to_pb()) + return request diff --git a/ydb/aio/table.py b/ydb/aio/table.py index 538f498b..30b977f3 100644 --- a/ydb/aio/table.py +++ b/ydb/aio/table.py @@ -105,6 +105,7 @@ async def alter_table( alter_partitioning_settings=None, set_key_bloom_filter=None, set_read_replicas_settings=None, + rename_indexes=None, ): # pylint: disable=W0236,R0913,R0914 return await super().alter_table( path, @@ -123,6 +124,7 @@ async def alter_table( alter_partitioning_settings, set_key_bloom_filter, set_read_replicas_settings, + rename_indexes, ) def transaction(self, tx_mode=None, *, allow_split_transactions=None): @@ -250,6 +252,7 @@ async def alter_table( alter_partitioning_settings: Optional["ydb.PartitioningSettings"] = None, set_key_bloom_filter: Optional["ydb.FeatureFlag"] = None, set_read_replicas_settings: Optional["ydb.ReadReplicasSettings"] = None, + rename_indexes: Optional[List["ydb.RenameIndexItem"]] = None, ) -> "ydb.Operation": """ Alter a YDB table. @@ -269,6 +272,7 @@ async def alter_table( :param set_compaction_policy: Compaction policy :param alter_partitioning_settings: ydb.PartitioningSettings to alter :param set_key_bloom_filter: ydb.FeatureFlag to set key bloom filter + :param rename_indexes: List of ydb.RenameIndexItem to rename :return: Operation or YDB error otherwise. """ @@ -293,6 +297,7 @@ async def callee(session: Session): alter_partitioning_settings=alter_partitioning_settings, set_key_bloom_filter=set_key_bloom_filter, set_read_replicas_settings=set_read_replicas_settings, + rename_indexes=rename_indexes, ) return await self._pool.retry_operation(callee) diff --git a/ydb/table.py b/ydb/table.py index ac73902f..8767ca1e 100644 --- a/ydb/table.py +++ b/ydb/table.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import abc +from dataclasses import dataclass import ydb from abc import abstractmethod import logging @@ -327,6 +328,20 @@ def to_pb(self): return self._pb +@dataclass +class RenameIndexItem: + source_name: str + destination_name: str + replace_destination: bool = False + + def to_pb(self): + return _apis.ydb_table.RenameIndexItem( + source_name=self.source_name, + destination_name=self.destination_name, + replace_destination=self.replace_destination, + ) + + class ReplicationPolicy(object): def __init__(self): self._pb = _apis.ydb_table.ReplicationPolicy() @@ -1124,6 +1139,7 @@ def alter_table( alter_partitioning_settings=None, set_key_bloom_filter=None, set_read_replicas_settings=None, + rename_indexes=None, ): pass @@ -1321,6 +1337,7 @@ def alter_table( alter_partitioning_settings: Optional["ydb.PartitioningSettings"] = None, set_key_bloom_filter: Optional["ydb.FeatureFlag"] = None, set_read_replicas_settings: Optional["ydb.ReadReplicasSettings"] = None, + rename_indexes: Optional[List["ydb.RenameIndexItem"]] = None, ) -> "ydb.Operation": """ Alter a YDB table. @@ -1340,6 +1357,7 @@ def alter_table( :param set_compaction_policy: Compaction policy :param alter_partitioning_settings: ydb.PartitioningSettings to alter :param set_key_bloom_filter: ydb.FeatureFlag to set key bloom filter + :param rename_indexes: List of ydb.RenameIndexItem to rename :return: Operation or YDB error otherwise. """ @@ -1364,6 +1382,7 @@ def callee(session: Session): alter_partitioning_settings=alter_partitioning_settings, set_key_bloom_filter=set_key_bloom_filter, set_read_replicas_settings=set_read_replicas_settings, + rename_indexes=rename_indexes, ) return self._pool.retry_operation_sync(callee) @@ -1857,6 +1876,7 @@ def alter_table( alter_partitioning_settings=None, set_key_bloom_filter=None, set_read_replicas_settings=None, + rename_indexes=None, ): return self._driver( _session_impl.alter_table_request_factory( @@ -1876,6 +1896,7 @@ def alter_table( alter_partitioning_settings, set_key_bloom_filter, set_read_replicas_settings, + rename_indexes, ), _apis.TableService.Stub, _apis.TableService.AlterTable, @@ -2088,6 +2109,7 @@ def async_alter_table( alter_partitioning_settings=None, set_key_bloom_filter=None, set_read_replicas_settings=None, + rename_indexes=None, ): return self._driver.future( _session_impl.alter_table_request_factory( @@ -2107,6 +2129,7 @@ def async_alter_table( alter_partitioning_settings, set_key_bloom_filter, set_read_replicas_settings, + rename_indexes, ), _apis.TableService.Stub, _apis.TableService.AlterTable,