Search in sources :

Example 1 with UpdateDatabaseDdlRequest

use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project java-spanner by googleapis.

the class MockDatabaseAdminServiceImplTest method updateDatabaseDdl.

@Test
public void updateDatabaseDdl() throws InterruptedException, ExecutionException {
    createTestDb();
    UpdateDatabaseDdlRequest request = UpdateDatabaseDdlRequest.newBuilder().setDatabase(TEST_DB_NAME).addAllStatements(Arrays.asList("CREATE TABLE BAZ", "DROP TABLE FOO")).build();
    OperationFuture<Empty, UpdateDatabaseDdlMetadata> op = client.updateDatabaseDdlOperationCallable().futureCall(request);
    op.get();
    GetDatabaseDdlResponse response = client.getDatabaseDdl(TEST_DB_NAME);
    assertThat(response.getStatementsList()).containsExactly("CREATE TABLE FOO", "CREATE TABLE BAR", "CREATE TABLE BAZ", "DROP TABLE FOO");
}
Also used : Empty(com.google.protobuf.Empty) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) GetDatabaseDdlResponse(com.google.spanner.admin.database.v1.GetDatabaseDdlResponse) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.Test)

Example 2 with UpdateDatabaseDdlRequest

use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project java-spanner by googleapis.

the class DatabaseAdminClientTest method updateDatabaseDdlTest2.

@Test
public void updateDatabaseDdlTest2() throws Exception {
    Empty expectedResponse = Empty.newBuilder().build();
    Operation resultOperation = Operation.newBuilder().setName("updateDatabaseDdlTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
    mockDatabaseAdmin.addResponse(resultOperation);
    String database = "database1789464955";
    List<String> statements = new ArrayList<>();
    client.updateDatabaseDdlAsync(database, statements).get();
    List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    UpdateDatabaseDdlRequest actualRequest = ((UpdateDatabaseDdlRequest) actualRequests.get(0));
    Assert.assertEquals(database, actualRequest.getDatabase());
    Assert.assertEquals(statements, actualRequest.getStatementsList());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Empty(com.google.protobuf.Empty) AbstractMessage(com.google.protobuf.AbstractMessage) ArrayList(java.util.ArrayList) Operation(com.google.longrunning.Operation) ByteString(com.google.protobuf.ByteString) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.Test)

Example 3 with UpdateDatabaseDdlRequest

use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project java-spanner by googleapis.

the class DatabaseAdminClientTest method updateDatabaseDdlTest.

@Test
public void updateDatabaseDdlTest() throws Exception {
    Empty expectedResponse = Empty.newBuilder().build();
    Operation resultOperation = Operation.newBuilder().setName("updateDatabaseDdlTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
    mockDatabaseAdmin.addResponse(resultOperation);
    DatabaseName database = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]");
    List<String> statements = new ArrayList<>();
    client.updateDatabaseDdlAsync(database, statements).get();
    List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    UpdateDatabaseDdlRequest actualRequest = ((UpdateDatabaseDdlRequest) actualRequests.get(0));
    Assert.assertEquals(database.toString(), actualRequest.getDatabase());
    Assert.assertEquals(statements, actualRequest.getStatementsList());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Empty(com.google.protobuf.Empty) AbstractMessage(com.google.protobuf.AbstractMessage) DatabaseName(com.google.spanner.admin.database.v1.DatabaseName) ArrayList(java.util.ArrayList) Operation(com.google.longrunning.Operation) ByteString(com.google.protobuf.ByteString) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.Test)

Example 4 with UpdateDatabaseDdlRequest

use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project java-spanner by googleapis.

the class GapicSpannerRpc method updateDatabaseDdl.

/**
 * If the update database ddl operation returns an ALREADY_EXISTS error, meaning the operation id
 * used is already in flight, this method will simply resume the original operation. The returned
 * future will be completed when the original operation finishes.
 *
 * <p>This mechanism is necessary, because the update database ddl can be retried. If a retryable
 * failure occurs, the backend has already started processing the update database ddl operation
 * with the given id and the library issues a retry, an ALREADY_EXISTS error will be returned. If
 * we were to bubble this error up, it would be confusing for the caller, who used originally
 * called the method with a new operation id.
 */
@Override
public OperationFuture<Empty, UpdateDatabaseDdlMetadata> updateDatabaseDdl(final String databaseName, final Iterable<String> updateDatabaseStatements, @Nullable final String updateId) throws SpannerException {
    acquireAdministrativeRequestsRateLimiter();
    final UpdateDatabaseDdlRequest request = UpdateDatabaseDdlRequest.newBuilder().setDatabase(databaseName).addAllStatements(updateDatabaseStatements).setOperationId(MoreObjects.firstNonNull(updateId, "")).build();
    final GrpcCallContext context = newCallContext(null, databaseName, request, DatabaseAdminGrpc.getUpdateDatabaseDdlMethod());
    final OperationCallable<UpdateDatabaseDdlRequest, Empty, UpdateDatabaseDdlMetadata> callable = databaseAdminStub.updateDatabaseDdlOperationCallable();
    return runWithRetryOnAdministrativeRequestsExceeded(() -> {
        OperationFuture<Empty, UpdateDatabaseDdlMetadata> operationFuture = callable.futureCall(request, context);
        try {
            operationFuture.getInitialFuture().get();
        } catch (InterruptedException e) {
            throw newSpannerException(e);
        } catch (ExecutionException e) {
            Throwable t = e.getCause();
            SpannerException se = SpannerExceptionFactory.asSpannerException(t);
            if (se instanceof AdminRequestsPerMinuteExceededException) {
                // Propagate this to trigger a retry.
                throw se;
            }
            if (t instanceof AlreadyExistsException) {
                String operationName = OPERATION_NAME_TEMPLATE.instantiate("database", databaseName, "operation", updateId);
                return callable.resumeFutureCall(operationName, context);
            }
        }
        return operationFuture;
    });
}
Also used : AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) AdminRequestsPerMinuteExceededException(com.google.cloud.spanner.AdminRequestsPerMinuteExceededException) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Empty(com.google.protobuf.Empty) GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) SpannerExceptionFactory.newSpannerException(com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException) SpannerException(com.google.cloud.spanner.SpannerException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with UpdateDatabaseDdlRequest

use of com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest in project liquibase-spanner by cloudspannerecosystem.

the class DropAllForeignKeysTest method testDropForeignKeyTest.

@Test
void testDropForeignKeyTest() throws Exception {
    String[] expectedSql = new String[] { "ALTER TABLE Singers DROP CONSTRAINT FK_Singers1" };
    for (String sql : expectedSql) {
        addUpdateDdlStatementsResponse(sql);
    }
    for (String file : new String[] { "drop-foreign-key.spanner.yaml" }) {
        try (Connection con = createConnection();
            Liquibase liquibase = getLiquibase(con, file)) {
            liquibase.update(new Contexts("test"));
        }
    }
    assertThat(mockAdmin.getRequests()).hasSize(expectedSql.length);
    for (int i = 0; i < expectedSql.length; i++) {
        assertThat(mockAdmin.getRequests().get(i)).isInstanceOf(UpdateDatabaseDdlRequest.class);
        UpdateDatabaseDdlRequest request = (UpdateDatabaseDdlRequest) mockAdmin.getRequests().get(i);
        assertThat(request.getStatementsList()).hasSize(1);
        assertThat(request.getStatementsList().get(0)).isEqualTo(expectedSql[i]);
    }
}
Also used : Liquibase(liquibase.Liquibase) Connection(java.sql.Connection) Contexts(liquibase.Contexts) UpdateDatabaseDdlRequest(com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest) Test(org.junit.jupiter.api.Test)

Aggregations

UpdateDatabaseDdlRequest (com.google.spanner.admin.database.v1.UpdateDatabaseDdlRequest)39 Connection (java.sql.Connection)35 Contexts (liquibase.Contexts)29 Liquibase (liquibase.Liquibase)29 Test (org.junit.jupiter.api.Test)29 Test (org.junit.Test)9 Statement (java.sql.Statement)5 AbstractMessage (com.google.protobuf.AbstractMessage)4 Empty (com.google.protobuf.Empty)4 BeginTransactionRequest (com.google.spanner.v1.BeginTransactionRequest)4 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)4 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)3 Operation (com.google.longrunning.Operation)3 UpdateDatabaseDdlMetadata (com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata)3 ResultSet (java.sql.ResultSet)3 LabelExpression (liquibase.LabelExpression)3 AbstractMockServerTest (liquibase.ext.spanner.AbstractMockServerTest)3 SpannerException (com.google.cloud.spanner.SpannerException)2 ByteString (com.google.protobuf.ByteString)2 CommitRequest (com.google.spanner.v1.CommitRequest)2