Search in sources :

Example 26 with Database

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

the class EmulatorUtilTest method testCreateInstanceAndDatabase_propagatesInterruptsOnDatabaseCreation.

@Test
public void testCreateInstanceAndDatabase_propagatesInterruptsOnDatabaseCreation() throws InterruptedException, ExecutionException {
    Spanner spanner = mock(Spanner.class);
    SpannerOptions options = mock(SpannerOptions.class);
    when(spanner.getOptions()).thenReturn(options);
    when(options.getCredentials()).thenReturn(NoCredentials.getInstance());
    InstanceAdminClient instanceClient = mock(InstanceAdminClient.class);
    @SuppressWarnings("unchecked") OperationFuture<Instance, CreateInstanceMetadata> instanceOperationFuture = mock(OperationFuture.class);
    when(spanner.getInstanceAdminClient()).thenReturn(instanceClient);
    when(instanceClient.createInstance(any(InstanceInfo.class))).thenReturn(instanceOperationFuture);
    when(instanceOperationFuture.get()).thenReturn(mock(Instance.class));
    DatabaseAdminClient databaseClient = mock(DatabaseAdminClient.class);
    @SuppressWarnings("unchecked") OperationFuture<Database, CreateDatabaseMetadata> databaseOperationFuture = mock(OperationFuture.class);
    when(spanner.getDatabaseAdminClient()).thenReturn(databaseClient);
    when(databaseClient.createDatabase(eq("test-instance"), eq("test-database"), eq(ImmutableList.of()))).thenReturn(databaseOperationFuture);
    when(databaseOperationFuture.get()).thenThrow(new InterruptedException());
    try {
        EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
        fail("missing expected exception");
    } catch (SpannerException e) {
        assertEquals(ErrorCode.CANCELLED, e.getErrorCode());
    }
}
Also used : Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata) InstanceInfo(com.google.cloud.spanner.InstanceInfo) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Database(com.google.cloud.spanner.Database) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) SpannerException(com.google.cloud.spanner.SpannerException) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Spanner(com.google.cloud.spanner.Spanner) Test(org.junit.Test)

Example 27 with Database

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

the class EmulatorUtilTest method testCreateInstanceAndDatabase_propagatesOtherErrorsOnDatabaseCreation.

@Test
public void testCreateInstanceAndDatabase_propagatesOtherErrorsOnDatabaseCreation() throws InterruptedException, ExecutionException {
    Spanner spanner = mock(Spanner.class);
    SpannerOptions options = mock(SpannerOptions.class);
    when(spanner.getOptions()).thenReturn(options);
    when(options.getCredentials()).thenReturn(NoCredentials.getInstance());
    InstanceAdminClient instanceClient = mock(InstanceAdminClient.class);
    @SuppressWarnings("unchecked") OperationFuture<Instance, CreateInstanceMetadata> instanceOperationFuture = mock(OperationFuture.class);
    when(spanner.getInstanceAdminClient()).thenReturn(instanceClient);
    when(instanceClient.createInstance(any(InstanceInfo.class))).thenReturn(instanceOperationFuture);
    when(instanceOperationFuture.get()).thenReturn(mock(Instance.class));
    DatabaseAdminClient databaseClient = mock(DatabaseAdminClient.class);
    @SuppressWarnings("unchecked") OperationFuture<Database, CreateDatabaseMetadata> databaseOperationFuture = mock(OperationFuture.class);
    when(spanner.getDatabaseAdminClient()).thenReturn(databaseClient);
    when(databaseClient.createDatabase(eq("test-instance"), eq("test-database"), eq(ImmutableList.of()))).thenReturn(databaseOperationFuture);
    when(databaseOperationFuture.get()).thenThrow(new ExecutionException(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Invalid database options")));
    try {
        EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
        fail("missing expected exception");
    } catch (SpannerException e) {
        assertEquals(ErrorCode.INVALID_ARGUMENT, e.getErrorCode());
    }
}
Also used : Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata) InstanceInfo(com.google.cloud.spanner.InstanceInfo) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Database(com.google.cloud.spanner.Database) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) SpannerException(com.google.cloud.spanner.SpannerException) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExecutionException(java.util.concurrent.ExecutionException) Spanner(com.google.cloud.spanner.Spanner) Test(org.junit.Test)

Example 28 with Database

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

the class ITDatabaseAdminTest method databaseOperationsViaEntity.

@Test
public void databaseOperationsViaEntity() throws Exception {
    String dbId = testHelper.getUniqueDatabaseId();
    String instanceId = testHelper.getInstanceId().getInstance();
    String statement1 = "CREATE TABLE T (\n" + "  K STRING(MAX),\n" + ") PRIMARY KEY(K)";
    OperationFuture<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
    Database db = op.get(TIMEOUT_MINUTES, TimeUnit.MINUTES);
    dbs.add(db);
    assertThat(db.getId().getDatabase()).isEqualTo(dbId);
    db = db.reload();
    assertThat(db.getId().getDatabase()).isEqualTo(dbId);
    String statement2 = "CREATE TABLE T2 (\n" + "  K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
    OperationFuture<?, ?> op2 = db.updateDdl(ImmutableList.of(statement2), null);
    op2.get(TIMEOUT_MINUTES, TimeUnit.MINUTES);
    Iterable<String> statementsInDb = db.getDdl();
    assertThat(statementsInDb).containsExactly(statement1, statement2);
    db.drop();
    dbs.clear();
    try {
        db.reload();
        fail("Expected exception");
    } catch (SpannerException ex) {
        assertThat(ex.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
    }
}
Also used : Database(com.google.cloud.spanner.Database) SpannerException(com.google.cloud.spanner.SpannerException) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Example 29 with Database

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

the class ITDatabaseTest method testNumericPrimaryKey.

@Test
public void testNumericPrimaryKey() throws InterruptedException, ExecutionException, TimeoutException {
    assumeFalse("Emulator does not support numeric primary keys", isUsingEmulator());
    final String projectId = env.getTestHelper().getInstanceId().getProject();
    final String instanceId = env.getTestHelper().getInstanceId().getInstance();
    final String databaseId = env.getTestHelper().getUniqueDatabaseId();
    final String table = "NumericTable";
    final DatabaseId id = DatabaseId.of(projectId, instanceId, databaseId);
    final DatabaseAdminClient databaseAdminClient = env.getTestHelper().getClient().getDatabaseAdminClient();
    try {
        // Creates table with numeric primary key
        final OperationFuture<Database, CreateDatabaseMetadata> operation = databaseAdminClient.createDatabase(instanceId, databaseId, Collections.singletonList("CREATE TABLE " + table + " (" + "Id NUMERIC NOT NULL" + ") PRIMARY KEY (Id)"));
        final Database database = operation.get(10, TimeUnit.MINUTES);
        assertNotNull(database);
        // Writes data into the table
        final DatabaseClient databaseClient = env.getTestHelper().getClient().getDatabaseClient(id);
        final ArrayList<Mutation> mutations = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            mutations.add(Mutation.newInsertBuilder(table).set("Id").to(new BigDecimal(i + "")).build());
        }
        databaseClient.write(mutations);
        // Reads the data to verify the writes
        try (final ResultSet resultSet = databaseClient.singleUse().read(table, KeySet.all(), Collections.singletonList("Id"))) {
            for (int i = 0; resultSet.next(); i++) {
                assertEquals(new BigDecimal(i + ""), resultSet.getBigDecimal("Id"));
            }
        }
        // Deletes data from the table, leaving only the Id = 0 row
        databaseClient.readWriteTransaction().run(new TransactionCallable<Object>() {

            @Nullable
            @Override
            public Object run(TransactionContext transaction) throws Exception {
                transaction.executeUpdate(Statement.of("DELETE FROM " + table + " WHERE Id > 0"));
                return null;
            }
        });
        // Reads the data to verify the deletes only left a single row left
        try (final ResultSet resultSet = databaseClient.singleUse().executeQuery(Statement.of("SELECT COUNT(1) as cnt FROM " + table))) {
            resultSet.next();
            assertEquals(1L, resultSet.getLong("cnt"));
        }
    } finally {
        databaseAdminClient.dropDatabase(instanceId, databaseId);
    }
}
Also used : ArrayList(java.util.ArrayList) DatabaseId(com.google.cloud.spanner.DatabaseId) BigDecimal(java.math.BigDecimal) TimeoutException(java.util.concurrent.TimeoutException) DatabaseNotFoundException(com.google.cloud.spanner.DatabaseNotFoundException) SpannerException(com.google.cloud.spanner.SpannerException) ExecutionException(java.util.concurrent.ExecutionException) InstanceNotFoundException(com.google.cloud.spanner.InstanceNotFoundException) DatabaseClient(com.google.cloud.spanner.DatabaseClient) TransactionContext(com.google.cloud.spanner.TransactionContext) Database(com.google.cloud.spanner.Database) ResultSet(com.google.cloud.spanner.ResultSet) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) Mutation(com.google.cloud.spanner.Mutation) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Nullable(javax.annotation.Nullable) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Example 30 with Database

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

the class ITBackupTest method testMetadata.

private void testMetadata(OperationFuture<Backup, CreateBackupMetadata> operation, String backupId, Database database) throws InterruptedException, ExecutionException {
    logger.info("Getting operation metadata");
    CreateBackupMetadata metadata1 = operation.getMetadata().get();
    String expectedOperationName1 = String.format(EXPECTED_OP_NAME_FORMAT, testHelper.getInstanceId().getName(), backupId);
    assertTrue(operation.getName().startsWith(expectedOperationName1));
    assertEquals(database.getId().getName(), metadata1.getDatabase());
    assertEquals(BackupId.of(testHelper.getInstanceId(), backupId).getName(), metadata1.getName());
    logger.info("Finished metadata tests");
}
Also used : CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata)

Aggregations

Test (org.junit.Test)53 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)32 Database (com.google.cloud.spanner.Database)31 ArrayList (java.util.ArrayList)25 AbstractMessage (com.google.protobuf.AbstractMessage)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)13 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 UpdateDatabaseDdlMetadata (com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata)7 StatusRuntimeException (io.grpc.StatusRuntimeException)7