private String database() { return getId().getDatabase(); }
@Test public void basics() { String name = "projects/test-project/instances/test-instance/databases/database-1"; DatabaseId db = DatabaseId.of(name); assertThat(db.getName()).isEqualTo(name); assertThat(db.getInstanceId().getInstance()).isEqualTo("test-instance"); assertThat(db.getDatabase()).isEqualTo("database-1"); assertThat(DatabaseId.of("test-project", "test-instance", "database-1")).isEqualTo(db); assertThat(DatabaseId.of(name)).isEqualTo(db); assertThat(DatabaseId.of(name).hashCode()).isEqualTo(db.hashCode()); assertThat(db.toString()).isEqualTo(name); }
@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(); 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(); Iterable<String> statementsInDb = db.getDdl(); assertThat(statementsInDb).containsExactly(statement1, statement2); db.drop(); dbs.clear(); expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND)); db.reload(); }
@Test public void listPagination() throws Exception { List<String> dbIds = ImmutableList.of( testHelper.getUniqueDatabaseId(), testHelper.getUniqueDatabaseId(), testHelper.getUniqueDatabaseId()); String instanceId = testHelper.getInstanceId().getInstance(); for (String dbId : dbIds) { dbs.add(dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.<String>of()).get()); } Page<Database> page = dbAdminClient.listDatabases(instanceId, Options.pageSize(1)); List<String> dbIdsGot = new ArrayList<>(); while (page != null) { Database db = Iterables.getOnlyElement(page.getValues()); dbIdsGot.add(db.getId().getDatabase()); page = page.getNextPage(); } assertThat(dbIdsGot).containsAllIn(dbIds); } }
Database db = op.get(); dbs.add(db); assertThat(db.getId().getDatabase()).isEqualTo(dbId); assertThat(db.getId().getDatabase()).isEqualTo(dbId); Iterators.toArray( dbAdminClient.listDatabases(instanceId).iterateAll().iterator(), Database.class)) { if (dbInList.getId().getDatabase().equals(dbId)) { foundDb = true; break;
static void addIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList("CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added AlbumsByAlbumTitle index"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_create_index]
static void addStoringIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList( "CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) " + "STORING (MarketingBudget)"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added AlbumsByAlbumTitle2 index"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_create_storing_index]
static void addCommitTimestamp(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList( "ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP " + "OPTIONS (allow_commit_timestamp=true)"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added LastUpdateTime as a commit timestamp column in Albums table."); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_add_timestamp_column]
static void addMarketingBudget(DatabaseAdminClient adminClient, DatabaseId dbId) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = adminClient .updateDatabaseDdl( dbId.getInstanceId().getInstance(), dbId.getDatabase(), Arrays.asList("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added MarketingBudget column"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_add_column]
static void createTableWithTimestamp(DatabaseAdminClient dbAdminClient, DatabaseId id) { OperationFuture<Void, UpdateDatabaseDdlMetadata> op = dbAdminClient.updateDatabaseDdl( id.getInstanceId().getInstance(), id.getDatabase(), Arrays.asList( "CREATE TABLE Performances (\n" + " SingerId INT64 NOT NULL,\n" + " VenueId INT64 NOT NULL,\n" + " EventDate Date,\n" + " Revenue INT64, \n" + " LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true)\n" + ") PRIMARY KEY (SingerId, VenueId, EventDate),\n" + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"), null); try { // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Created Performances table in database: [" + id + "]"); } catch (ExecutionException e) { // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { // Throw when a thread is waiting, sleeping, or otherwise occupied, // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } // [END spanner_create_table_with_timestamp_column]
dbAdminClient.createDatabase( id.getInstanceId().getInstance(), id.getDatabase(), Arrays.asList( "CREATE TABLE Singers (\n"
/** * Get the database ID used to perform database operations. * @return the database ID string. */ public String getDatabase() { return this.databaseId.getDatabase(); }
/** * Get the database ID used to perform database operations. * @return the database ID string. */ public String getDatabase() { return this.databaseId.getDatabase(); }
private String database() { return getId().getDatabase(); }
/** * Returns true if the configured database ID refers to an existing database. False * otherwise. * @return true if the database exists, and false if it does not. */ public boolean databaseExists() { for (Database db : this.databaseAdminClient.listDatabases(getInstanceId()) .getValues()) { if (getDatabase().equals(db.getId().getDatabase())) { return true; } } return false; }
/** * Returns true if the configured database ID refers to an existing database. False * otherwise. * @return true if the database exists, and false if it does not. */ public boolean databaseExists() { for (Database db : this.databaseAdminClient.listDatabases(getInstanceId()) .getValues()) { if (getDatabase().equals(db.getId().getDatabase())) { return true; } } return false; }