|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +using Google.Api.Gax.Grpc; |
| 16 | +using Google.Cloud.Spanner.Admin.Database.V1; |
15 | 17 | using Google.Cloud.Spanner.Data.CommonTesting; |
| 18 | +using Google.LongRunning; |
| 19 | +using Google.Protobuf; |
| 20 | +using Google.Protobuf.WellKnownTypes; |
16 | 21 | using System; |
17 | 22 | using System.Threading.Tasks; |
18 | 23 | using Xunit; |
@@ -239,19 +244,76 @@ AlbumTitle STRING(MAX), |
239 | 244 | using (var connection = new SpannerConnection(builder.WithDatabase(dbName))) |
240 | 245 | { |
241 | 246 | var dropSingersCmd = connection.CreateDdlCommand("DROP TABLE Singers"); |
242 | | - var dropBothTablesCmd = connection.CreateDdlCommand("DROP TABLE Albums", "DROP TABLE Singers"); |
| 247 | + var dropAlbumsCmd = connection.CreateDdlCommand("DROP TABLE Albums"); |
243 | 248 |
|
244 | 249 | await Assert.ThrowsAsync<SpannerException>(() => dropSingersCmd.ExecuteNonQueryAsync()); |
245 | | - var operationName = await dropBothTablesCmd.StartDdlAsync(); |
246 | | - Assert.NotEqual("", operationName); |
| 250 | + await dropAlbumsCmd.ExecuteNonQueryAsync(); |
| 251 | + await dropSingersCmd.ExecuteNonQueryAsync(); |
247 | 252 | } |
248 | 253 |
|
249 | 254 | using (var connection = new SpannerConnection(builder)) |
250 | 255 | { |
251 | 256 | var dropCommand = connection.CreateDdlCommand($"DROP DATABASE {dbName}"); |
252 | | - var operationName = await dropCommand.StartDdlAsync(); |
253 | | - // DropDatabase does not return a long-running operation. |
254 | | - Assert.Equal("", operationName); |
| 257 | + await dropCommand.ExecuteNonQueryAsync(); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + [Fact] |
| 262 | + public async Task StartDdlReturnsOperationName() |
| 263 | + { |
| 264 | + string dbName = GenerateDatabaseName(); |
| 265 | + var builder = new SpannerConnectionStringBuilder(_fixture.Database.NoDbConnectionString); |
| 266 | + var connectionOptions = new SpannerClientCreationOptions(builder); |
| 267 | + var adminClientBuilder = connectionOptions.CreateDatabaseAdminClientBuilder(); |
| 268 | + var adminClient = await adminClientBuilder.BuildAsync(); |
| 269 | + var channel = adminClientBuilder.LastCreatedChannel; |
| 270 | + |
| 271 | + try |
| 272 | + { |
| 273 | + using (var connection = new SpannerConnection(builder)) |
| 274 | + { |
| 275 | + var createDbCommand = connection.CreateDdlCommand($"CREATE DATABASE {dbName}"); |
| 276 | + var operationName = await createDbCommand.StartDdlAsync(); |
| 277 | + Assert.False(string.IsNullOrEmpty(operationName)); |
| 278 | + |
| 279 | + await HandleLro<Database, CreateDatabaseMetadata>( |
| 280 | + adminClient.CreateDatabaseOperationsClient, operationName); |
| 281 | + } |
| 282 | + |
| 283 | + using (var connection = new SpannerConnection(builder.WithDatabase(dbName))) |
| 284 | + { |
| 285 | + var createTableCommand = connection.CreateDdlCommand( |
| 286 | + "CREATE TABLE Singers (SingerId INT64 PRIMARY KEY, Name STRING(1024))"); |
| 287 | + var operationName = await createTableCommand.StartDdlAsync(); |
| 288 | + Assert.False(string.IsNullOrEmpty(operationName)); |
| 289 | + |
| 290 | + await HandleLro<Empty, UpdateDatabaseDdlMetadata>( |
| 291 | + adminClient.UpdateDatabaseDdlOperationsClient, operationName); |
| 292 | + } |
| 293 | + |
| 294 | + using (var connection = new SpannerConnection(builder)) |
| 295 | + { |
| 296 | + var dropCommand = connection.CreateDdlCommand($"DROP DATABASE {dbName}"); |
| 297 | + var operationName = await dropCommand.StartDdlAsync(); |
| 298 | + // DropDatabase does not return a long-running operation. |
| 299 | + Assert.Null(operationName); |
| 300 | + } |
| 301 | + } |
| 302 | + finally |
| 303 | + { |
| 304 | + channel?.Shutdown(); |
| 305 | + } |
| 306 | + |
| 307 | + async Task HandleLro<TResponse, TMetadata>(OperationsClient client, string operationName) |
| 308 | + where TResponse : class, IMessage<TResponse>, new() |
| 309 | + where TMetadata : class, IMessage<TMetadata>, new() |
| 310 | + { |
| 311 | + var rawOperation = await client.GetOperationAsync(operationName); |
| 312 | + var operation = new Operation<TResponse, TMetadata>(rawOperation, client); |
| 313 | + var completedOperation = await operation.PollUntilCompletedAsync(); |
| 314 | + |
| 315 | + Assert.True(completedOperation.IsCompleted); |
| 316 | + Assert.Null(completedOperation.Exception); |
255 | 317 | } |
256 | 318 | } |
257 | 319 | } |
|
0 commit comments