Search in sources :

Example 61 with Database

use of com.google.spanner.admin.database.v1.Database in project jaxdb by jaxdb.

the class JSqlTest method loadEntitiesJaxSB.

@SuppressWarnings("unchecked")
static int loadEntitiesJaxSB(final Connection connection, final String name) throws ClassNotFoundException, IOException, SAXException, SQLException, TransformerException {
    Database.threadLocal((Class<? extends Schema>) Class.forName(Entities.class.getPackage().getName() + "." + name)).connectPrepared(() -> connection);
    final URL sqlx = ClassLoader.getSystemClassLoader().getResource("jaxdb/" + name + ".sqlx");
    assertNotNull(sqlx);
    final $Database database = ($Database) Bindings.parse(sqlx);
    final DDLx ddlx = new DDLx(ClassLoader.getSystemClassLoader().getResource(name + ".ddlx"));
    Schemas.truncate(connection, ddlx.getMergedSchema().getTable());
    final Batch batch = new Batch();
    final int expectedCount = DBVendor.valueOf(connection.getMetaData()) == DBVendor.ORACLE ? 0 : 1;
    for (final data.Table<?> table : Entities.toEntities(database)) batch.addStatement(INSERT(table), (e, c) -> assertEquals(expectedCount, c));
    return batch.execute();
}
Also used : Bindings(org.jaxsb.runtime.Bindings) Connection(java.sql.Connection) DDLx(org.jaxdb.ddlx.DDLx) InMemoryCompiler(org.libj.jci.InMemoryCompiler) TransformerException(javax.xml.transform.TransformerException) Files(java.nio.file.Files) URL(java.net.URL) org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Database(org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Database) IOException(java.io.IOException) File(java.io.File) SQLException(java.sql.SQLException) GeneratorExecutionException(org.jaxdb.ddlx.GeneratorExecutionException) DML(org.jaxdb.jsql.DML) Schemas(org.jaxdb.ddlx.Schemas) Generator(org.jaxdb.jsql.generator.Generator) SAXException(org.xml.sax.SAXException) DBVendor(org.jaxdb.vendor.DBVendor) Throwing(org.libj.util.function.Throwing) CompilationException(org.libj.jci.CompilationException) Assert(org.junit.Assert) Path(java.nio.file.Path) Assertions.assertNotNull(org.libj.lang.Assertions.assertNotNull) DDLx(org.jaxdb.ddlx.DDLx) org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Database(org.jaxdb.www.sqlx_0_5.xLygluGCXAA.$Database) URL(java.net.URL)

Example 62 with Database

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

the class GapicSpannerRpc method getDatabase.

@Override
public Database getDatabase(String databaseName) throws SpannerException {
    acquireAdministrativeRequestsRateLimiter();
    final GetDatabaseRequest request = GetDatabaseRequest.newBuilder().setName(databaseName).build();
    final GrpcCallContext context = newCallContext(null, databaseName, request, DatabaseAdminGrpc.getGetDatabaseMethod());
    return runWithRetryOnAdministrativeRequestsExceeded(() -> get(databaseAdminStub.getDatabaseCallable().futureCall(request, context)));
}
Also used : GetDatabaseRequest(com.google.spanner.admin.database.v1.GetDatabaseRequest) GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext)

Example 63 with Database

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

the class DatabaseAdminClientTest method instanceListBackupOperationsWithProgress.

@Test
public void instanceListBackupOperationsWithProgress() throws InvalidProtocolBufferException {
    Instance instance = spanner.getInstanceAdminClient().newInstanceBuilder(InstanceId.of(PROJECT_ID, INSTANCE_ID)).build();
    String database = String.format("%s/databases/%s", TEST_PARENT, DB_ID);
    String filter = String.format("(metadata.database:%s) AND " + "(metadata.@type:type.googleapis.com/" + "google.spanner.admin.database.v1.CreateBackupMetadata)", database);
    Page<Operation> operations = instance.listBackupOperations(Options.filter(filter));
    for (Operation op : operations.iterateAll()) {
        CreateBackupMetadata metadata = op.getMetadata().unpack(CreateBackupMetadata.class);
        String progress = String.format("Backup %s on database %s pending: %d%% complete", metadata.getName(), metadata.getDatabase(), metadata.getProgress().getProgressPercent());
        assertThat(progress.contains("100%"));
    }
}
Also used : CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Operation(com.google.longrunning.Operation) Test(org.junit.Test)

Example 64 with Database

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

the class DatabaseAdminClientTest method testRetryOperationOnAdminMethodQuotaPerMinutePerProjectExceeded.

@Test
public void testRetryOperationOnAdminMethodQuotaPerMinutePerProjectExceeded() {
    ErrorInfo info = ErrorInfo.newBuilder().putMetadata("quota_limit", "AdminMethodQuotaPerMinutePerProject").build();
    Metadata.Key<ErrorInfo> key = Metadata.Key.of(info.getDescriptorForType().getFullName() + Metadata.BINARY_HEADER_SUFFIX, ProtoLiteUtils.metadataMarshaller(info));
    Metadata trailers = new Metadata();
    trailers.put(key, info);
    mockDatabaseAdmin.addException(Status.RESOURCE_EXHAUSTED.withDescription("foo").asRuntimeException(trailers));
    mockDatabaseAdmin.clearRequests();
    Database database = client.getDatabase(INSTANCE_ID, DB_ID);
    assertEquals(DB_ID, database.getId().getDatabase());
    assertEquals(2, mockDatabaseAdmin.countRequestsOfType(GetDatabaseRequest.class));
}
Also used : GetDatabaseRequest(com.google.spanner.admin.database.v1.GetDatabaseRequest) ErrorInfo(com.google.rpc.ErrorInfo) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Metadata(io.grpc.Metadata) OptimizeRestoredDatabaseMetadata(com.google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) RestoreDatabaseMetadata(com.google.spanner.admin.database.v1.RestoreDatabaseMetadata) Test(org.junit.Test)

Example 65 with Database

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

the class DatabaseAdminGaxTest method listDatabasesTest.

@Test
public void listDatabasesTest() {
    Exception exception = setupException();
    String nextPageToken = "token%d";
    List<com.google.spanner.admin.database.v1.Database> databases = new ArrayList<>(2);
    for (int i = 0; i < 2; i++) {
        databases.add(com.google.spanner.admin.database.v1.Database.newBuilder().setName(String.format("projects/%s/instances/%s/databases/test%d", PROJECT, INSTANCE, i)).build());
    }
    if (exceptionAtCall == 0) {
        mockDatabaseAdmin.addException(exception);
    }
    for (int i = 0; i < 2; i++) {
        ListDatabasesResponse.Builder builder = ListDatabasesResponse.newBuilder().addAllDatabases(Collections.singletonList(databases.get(i)));
        if (i < (databases.size() - 1)) {
            builder.setNextPageToken(String.format(nextPageToken, i));
        }
        if (exceptionAtCall == (i + 1)) {
            mockDatabaseAdmin.addException(exception);
        }
        mockDatabaseAdmin.addResponse(builder.build());
    }
    InstanceName parent = InstanceName.of(PROJECT, INSTANCE);
    Page<Database> pagedListResponse = client.listDatabases(INSTANCE);
    List<Database> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(2, resources.size());
    List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
    Assert.assertEquals(2, actualRequests.size());
    ListDatabasesRequest actualRequest = (ListDatabasesRequest) actualRequests.get(0);
    Assert.assertEquals(parent, InstanceName.parse(actualRequest.getParent()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ArrayList(java.util.ArrayList) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) InstanceName(com.google.spanner.admin.instance.v1.InstanceName) ListDatabasesResponse(com.google.spanner.admin.database.v1.ListDatabasesResponse) ListDatabasesRequest(com.google.spanner.admin.database.v1.ListDatabasesRequest) 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