Search in sources :

Example 1 with CreateDatabaseMetadata

use of com.google.spanner.admin.database.v1.CreateDatabaseMetadata in project google-cloud-java by GoogleCloudPlatform.

the class ITDatabaseAdminTest method databaseOperations.

@Test
public void databaseOperations() throws Exception {
    String dbId = testHelper.getUniqueDatabaseId();
    String instanceId = testHelper.getInstanceId().getInstance();
    String statement1 = "CREATE TABLE T (\n" + "  K STRING(MAX),\n" + ") PRIMARY KEY(K)";
    Operation<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
    op = op.waitFor();
    Database db = op.getResult();
    dbs.add(db);
    assertThat(db.getId().getDatabase()).isEqualTo(dbId);
    db = dbAdminClient.getDatabase(instanceId, dbId);
    assertThat(db.getId().getDatabase()).isEqualTo(dbId);
    boolean foundDb = false;
    for (Database dbInList : Iterators.toArray(dbAdminClient.listDatabases(instanceId).iterateAll().iterator(), Database.class)) {
        if (dbInList.getId().getDatabase().equals(dbId)) {
            foundDb = true;
            break;
        }
    }
    assertThat(foundDb).isTrue();
    String statement2 = "CREATE TABLE T2 (\n" + "  K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
    Operation<?, ?> op2 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), null);
    op2.waitFor();
    List<String> statementsInDb = dbAdminClient.getDatabaseDdl(instanceId, dbId);
    assertThat(statementsInDb).containsExactly(statement1, statement2);
    dbAdminClient.dropDatabase(instanceId, dbId);
    dbs.clear();
    expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND));
    db = dbAdminClient.getDatabase(testHelper.getInstanceId().getInstance(), dbId);
}
Also used : Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 2 with CreateDatabaseMetadata

use of com.google.spanner.admin.database.v1.CreateDatabaseMetadata in project google-cloud-java by GoogleCloudPlatform.

the class RemoteSpannerHelper method createTestDatabase.

/**
   * Creates a test database defined by {@code statements} in the test instance. A {@code CREATE
   * DATABASE ...} statement should not be included; an appropriate name will be chosen and the
   * statement generated accordingly.
   */
public Database createTestDatabase(Iterable<String> statements) throws SpannerException {
    String dbId = getUniqueDatabaseId();
    Operation<Database, CreateDatabaseMetadata> op = client.getDatabaseAdminClient().createDatabase(instanceId.getInstance(), dbId, statements);
    op = op.waitFor();
    Database db = op.getResult();
    logger.log(Level.FINE, "Created test database {0}", db.getId());
    dbs.add(db);
    return db;
}
Also used : Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata)

Example 3 with CreateDatabaseMetadata

use of com.google.spanner.admin.database.v1.CreateDatabaseMetadata in project google-cloud-java by GoogleCloudPlatform.

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)";
    Operation<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
    op = op.waitFor();
    Database db = op.getResult();
    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)";
    Operation<?, ?> op2 = db.updateDdl(ImmutableList.of(statement2), null);
    op2.waitFor();
    Iterable<String> statementsInDb = db.getDdl();
    assertThat(statementsInDb).containsExactly(statement1, statement2);
    db.drop();
    dbs.clear();
    expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND));
    db.reload();
}
Also used : Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 4 with CreateDatabaseMetadata

use of com.google.spanner.admin.database.v1.CreateDatabaseMetadata in project google-cloud-java by GoogleCloudPlatform.

the class ITDatabaseAdminTest method updateDdlRetry.

@Test
public void updateDdlRetry() throws Exception {
    String dbId = testHelper.getUniqueDatabaseId();
    String instanceId = testHelper.getInstanceId().getInstance();
    String statement1 = "CREATE TABLE T (\n" + "  K STRING(MAX),\n" + ") PRIMARY KEY(K)";
    Operation<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
    op = op.waitFor();
    Database db = op.getResult();
    dbs.add(db);
    String statement2 = "CREATE TABLE T2 (\n" + "  K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
    Operation<Void, UpdateDatabaseDdlMetadata> op1 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
    Operation<Void, UpdateDatabaseDdlMetadata> op2 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
    op1 = op1.waitFor();
    op2 = op2.waitFor();
    assertThat(op1.getMetadata()).isEqualTo(op2.getMetadata());
}
Also used : UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Aggregations

Database (com.google.cloud.spanner.Database)4 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)4 IntegrationTest (com.google.cloud.spanner.IntegrationTest)3 Test (org.junit.Test)3 UpdateDatabaseDdlMetadata (com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata)1