Skip to content

Commit

Permalink
Merge pull request #60 Add rename operation from Quantulion/rename_op…
Browse files Browse the repository at this point in the history
…eration_add
  • Loading branch information
rekby authored Feb 9, 2024
2 parents 0248490 + 44e4479 commit cb04f07
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Ydb.Sdk/src/Services/Table/RenameTables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Ydb.Sdk.Client;
using Ydb.Table;
using Ydb.Table.V1;

namespace Ydb.Sdk.Services.Table;

public class RenameTableItem
{
public string SourcePath { get; }
public string DestinationPath { get; }
public bool ReplaceDestination { get; }

public RenameTableItem(string sourcePath, string destinationPath, bool replaceDestination)
{
SourcePath = sourcePath;
DestinationPath = destinationPath;
ReplaceDestination = replaceDestination;
}

public Ydb.Table.RenameTableItem GetProto(TableClient tableClient)
{
return new Ydb.Table.RenameTableItem
{
SourcePath = tableClient.MakeTablePath(SourcePath),
DestinationPath = tableClient.MakeTablePath(DestinationPath),
ReplaceDestination = ReplaceDestination
};
}
}

public class RenameTablesSettings : OperationRequestSettings
{
}

public class RenameTablesResponse : ResponseBase
{
internal RenameTablesResponse(Status status) : base(status)
{
}
}

public partial class TableClient
{
public async Task<RenameTablesResponse> RenameTables(IEnumerable<RenameTableItem> tableItems,
RenameTablesSettings? settings = null)
{
settings ??= new RenameTablesSettings();
var request = new RenameTablesRequest
{
OperationParams = MakeOperationParams(settings)
};
request.Tables.AddRange(tableItems.Select(item => item.GetProto(this)));

try
{
var response = await Driver.UnaryCall(
method: TableService.RenameTablesMethod,
request: request,
settings: settings);

var status = UnpackOperation(response.Data.Operation);
return new RenameTablesResponse(status);
}
catch (Driver.TransportException e)
{
return new RenameTablesResponse(e.Status);
}
}
}
63 changes: 63 additions & 0 deletions src/Ydb.Sdk/tests/Table/TestRenameTables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
using Ydb.Sdk.Services.Table;

namespace Ydb.Sdk.Tests.Table;

[Trait("Category", "Integration")]
public sealed class TestRenameTables
{
private readonly ILoggerFactory _loggerFactory;

private readonly DriverConfig _driverConfig = new(
endpoint: "grpc://localhost:2136",
database: "/local"
);

public TestRenameTables()
{
_loggerFactory = Utils.GetLoggerFactory() ?? NullLoggerFactory.Instance;
}

[Fact]
public async Task RenameNotExisting()
{
await using var driver = await Driver.CreateInitialized(_driverConfig, _loggerFactory);
using var tableClient = new TableClient(driver);

var renameTableItem = new RenameTableItem("source", "dest", false);

var response = await tableClient.RenameTables(new[] { renameTableItem });
Assert.Equal(StatusCode.SchemeError, response.Status.StatusCode);
}

[Fact]
public async Task RenameTables()
{
await using var driver = await Driver.CreateInitialized(_driverConfig, _loggerFactory);
using var tableClient = new TableClient(driver);

var pairs = new List<(string, string)>();
for (var i = 0; i < 5; i++)
{
pairs.Add(($"t{Guid.NewGuid():n}", $"t{Guid.NewGuid():n}"));
}

var items = new List<RenameTableItem>();
foreach (var (source, dest) in pairs)
{
await Utils.CreateSimpleTable(tableClient, source);
items.Add(new RenameTableItem(source, dest, false));
}

var response = await tableClient.RenameTables(items);
response.EnsureSuccess();


foreach (var (_, dest) in pairs)
{
await Utils.DropTable(tableClient, dest);
}
}
}

0 comments on commit cb04f07

Please sign in to comment.