-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0248490
commit 515c4df
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#nullable enable | ||
Check warning on line 1 in src/Ydb.Sdk/tests/Table/TestRenameTables.cs GitHub Actions / Inspection (./src/YdbSdk.sln)
|
||
|
||
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); | ||
} | ||
} | ||
} |