Search in sources :

Example 6 with Database

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

the class DatabaseAdminClientTest method instanceListDatabaseOperationsWithMetadata.

@Test
public void instanceListDatabaseOperationsWithMetadata() throws Exception {
    Instance instance = spanner.getInstanceAdminClient().newInstanceBuilder(InstanceId.of(PROJECT_ID, INSTANCE_ID)).build();
    String filter = "(metadata.@type:type.googleapis.com/" + "google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata)";
    mockDatabaseAdmin.addFilterMatches(filter, restoreDatabaseOperation.getMetadata().get().getOptimizeDatabaseOperationName());
    Iterable<Operation> operations = instance.listDatabaseOperations(Options.filter(filter)).iterateAll();
    assertThat(operations).hasSize(1);
    for (Operation op : operations) {
        OptimizeRestoredDatabaseMetadata metadata = op.getMetadata().unpack(OptimizeRestoredDatabaseMetadata.class);
        String progress = String.format("Restored database %s is optimized %d%%", metadata.getName(), metadata.getProgress().getProgressPercent());
        assertThat(progress.contains("100%"));
    }
}
Also used : OptimizeRestoredDatabaseMetadata(com.google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata) Operation(com.google.longrunning.Operation) Test(org.junit.Test)

Example 7 with Database

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

the class DatabaseAdminGaxTest method updateDatabaseDdlTest.

@Test
public void updateDatabaseDdlTest() throws Exception {
    Exception exception = setupException();
    com.google.longrunning.Operation resultOperation = com.google.longrunning.Operation.newBuilder().setName("updateDatabaseDdlTest").setDone(true).setResponse(Any.pack(Empty.getDefaultInstance())).build();
    if (exceptionAtCall == 0) {
        mockDatabaseAdmin.addException(exception);
    }
    mockDatabaseAdmin.addResponse(resultOperation);
    if (exceptionAtCall == 1) {
        mockDatabaseAdmin.addException(exception);
    }
    mockDatabaseAdmin.addResponse(resultOperation);
    for (int i = 0; i < 2; i++) {
        OperationFuture<Void, UpdateDatabaseDdlMetadata> actualResponse = client.updateDatabaseDdl(INSTANCE, "DATABASE", Collections.singletonList("CREATE TABLE FOO"), "updateDatabaseDdlTest");
        try {
            actualResponse.get();
        } catch (ExecutionException e) {
            Throwables.throwIfUnchecked(e.getCause());
            throw e;
        }
    }
    List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
    Assert.assertEquals(2, actualRequests.size());
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) ExecutionException(java.util.concurrent.ExecutionException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 8 with Database

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

the class MockDatabaseAdminServiceImpl method createDatabase.

@Override
public void createDatabase(CreateDatabaseRequest request, StreamObserver<Operation> responseObserver) {
    requests.add(request);
    try {
        createDatabaseStartupExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
        String id = request.getCreateStatement().replace("CREATE DATABASE ", "");
        if (id.startsWith("`") && id.endsWith("`")) {
            id = id.substring(1, id.length() - 1);
        }
        String name = String.format("%s/databases/%s", request.getParent(), id);
        MockDatabase db = new MockDatabase(name, request.getExtraStatementsList(), null);
        if (databases.putIfAbsent(name, db) == null) {
            CreateDatabaseMetadata metadata = CreateDatabaseMetadata.newBuilder().setDatabase(name).build();
            Database database = Database.newBuilder().setName(name).setState(db.state).build();
            Operation operation = Operation.newBuilder().setMetadata(Any.pack(metadata)).setResponse(Any.pack(database)).setDone(false).setName(operations.generateOperationName(name)).build();
            operations.addOperation(operation, new CreateDatabaseCallable(operation.getName(), name));
            createDatabaseResponseExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
            responseObserver.onNext(operation);
            responseObserver.onCompleted();
        } else {
            responseObserver.onError(Status.ALREADY_EXISTS.withDescription(String.format("Database with name %s already exists", name)).asRuntimeException());
        }
    } catch (Throwable t) {
        responseObserver.onError(t);
    }
}
Also used : Database(com.google.spanner.admin.database.v1.Database) Operation(com.google.longrunning.Operation) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata)

Example 9 with Database

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

the class MockDatabaseAdminServiceImplTest method listDatabases.

@Test
public void listDatabases() {
    createTestDb();
    ListDatabasesPagedResponse response = client.listDatabases(TEST_PARENT);
    List<String> databases = new ArrayList<>();
    for (Database db : response.iterateAll()) {
        databases.add(db.getName());
    }
    assertThat(databases).containsExactly(TEST_DB_NAME);
}
Also used : ListDatabasesPagedResponse(com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListDatabasesPagedResponse) ArrayList(java.util.ArrayList) Database(com.google.spanner.admin.database.v1.Database) Test(org.junit.Test)

Example 10 with Database

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

the class MockDatabaseAdminServiceImplTest method restoreDatabaseAlreadyExists.

@Test
public void restoreDatabaseAlreadyExists() throws InterruptedException, ExecutionException {
    createTestDb();
    createTestBackup();
    RestoreDatabaseRequest request = RestoreDatabaseRequest.newBuilder().setBackup(TEST_BCK_NAME).setDatabaseId("test-db").setParent(TEST_PARENT).build();
    OperationFuture<Database, RestoreDatabaseMetadata> op = client.restoreDatabaseOperationCallable().futureCall(request);
    exception.expect(ApiExceptionMatcher.forCode(StatusCode.Code.ALREADY_EXISTS));
    op.get();
}
Also used : RestoreDatabaseRequest(com.google.spanner.admin.database.v1.RestoreDatabaseRequest) Database(com.google.spanner.admin.database.v1.Database) RestoreDatabaseMetadata(com.google.spanner.admin.database.v1.RestoreDatabaseMetadata) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)53 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)31 Database (com.google.cloud.spanner.Database)29 AbstractMessage (com.google.protobuf.AbstractMessage)24 ArrayList (java.util.ArrayList)24 ExecutionException (java.util.concurrent.ExecutionException)22 Database (com.google.spanner.admin.database.v1.Database)21 Operation (com.google.longrunning.Operation)18 ByteString (com.google.protobuf.ByteString)16 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)12 SpannerException (com.google.cloud.spanner.SpannerException)12 Database (org.molgenis.emx2.Database)12 CreateBackupMetadata (com.google.spanner.admin.database.v1.CreateBackupMetadata)11 DatabaseName (com.google.spanner.admin.database.v1.DatabaseName)10 RestoreDatabaseMetadata (com.google.spanner.admin.database.v1.RestoreDatabaseMetadata)9 Timestamp (com.google.cloud.Timestamp)8 Spanner (com.google.cloud.spanner.Spanner)8 RestoreDatabaseRequest (com.google.spanner.admin.database.v1.RestoreDatabaseRequest)7 StatusRuntimeException (io.grpc.StatusRuntimeException)7 Backup (com.google.cloud.spanner.Backup)6