Search in sources :

Example 51 with Database

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

the class PgAdapterTestEnv method createDatabase.

// Create database.
public Database createDatabase() throws Exception {
    if (isUseExistingDb()) {
        throw new IllegalStateException("Cannot create a new test database if " + USE_EXISTING_DB + " is true.");
    }
    String databaseId = getDatabaseId();
    Spanner spanner = getSpanner();
    DatabaseAdminClient client = spanner.getDatabaseAdminClient();
    OperationFuture<Database, CreateDatabaseMetadata> op = client.createDatabase(client.newDatabaseBuilder(DatabaseId.of(projectId, instanceId, databaseId)).setDialect(Dialect.POSTGRESQL).build(), Collections.emptyList());
    Database db = op.get();
    databases.add(db);
    logger.log(Level.INFO, "Created database [" + db.getId() + "]");
    return db;
}
Also used : Database(com.google.cloud.spanner.Database) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Spanner(com.google.cloud.spanner.Spanner)

Example 52 with Database

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

the class SpannerDatabaseCreator method create.

private void create(Spanner spanner, DatabaseId databaseId) {
    DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient();
    OperationFuture<Database, CreateDatabaseMetadata> op = databaseAdminClient.createDatabase(databaseId.getInstanceId().getInstance(), databaseId.getDatabase(), Collections.emptyList());
    FutureTool.get(op);
}
Also used : Database(com.google.cloud.spanner.Database) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata)

Example 53 with Database

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

the class SpannerSingleTableSchemaUpdateService method performSchemaUpdate.

public Optional<SchemaUpdateResult> performSchemaUpdate(ClientId clientId, Supplier<List<String>> existingTableNames, PhysicalNode<?, ?, ?> physicalNode) {
    String tableName = physicalNode.getFieldInfo().getTableName();
    List<Field<?>> primaryKeyFields = physicalNode.getFieldInfo().getSamplePrimaryKey().getFields();
    List<? extends SpannerBaseFieldCodec<?, ?>> primaryKeyCodecs = fieldCodecRegistry.createCodecs(primaryKeyFields);
    for (SpannerBaseFieldCodec<?, ?> codec : primaryKeyCodecs) {
        if (codec.getSpannerColumnType().isArray()) {
            throw new RuntimeException("Invalid field type used for primary key: " + codec.getField().getKey().getName());
        }
    }
    List<SpannerIndex> indexes = new ArrayList<>();
    List<SpannerIndex> uniqueIndexes = Scanner.of(physicalNode.getFieldInfo().getUniqueIndexes().entrySet()).map(entry -> new SpannerIndex(tableName, entry.getKey(), entry.getValue(), Collections.emptyList(), true)).list();
    var statements = new SpannerUpdateStatements();
    String entityTableName = null;
    if (physicalNode instanceof IndexedStorage) {
        IndexedStorage<?, ?> indexedStorage = (IndexedStorage<?, ?>) physicalNode;
        indexes = Scanner.of(indexedStorage.getManagedNodes()).map(node -> new SpannerIndex(tableName, node.getName(), node.getIndexEntryFieldInfo().getPrimaryKeyFields(), node.getIndexEntryFieldInfo().getFields(), false)).list();
    }
    List<SpannerColumn> primaryKeyColumns = Scanner.of(primaryKeyCodecs).map(codec -> codec.getSpannerColumn(false)).list();
    List<SpannerColumn> nonKeyColumns = Scanner.of(fieldCodecRegistry.createCodecs(physicalNode.getFieldInfo().getNonKeyFields())).map(codec -> codec.getSpannerColumn(true)).list();
    if (!existingTableNames.get().contains(tableName)) {
        statements.updateFunction(tableOperationsGenerator.createTable(tableName, primaryKeyColumns, nonKeyColumns, entityTableName), updateOptions::getCreateTables, true);
        Scanner.of(indexes, uniqueIndexes).concat(Scanner::of).map(index -> createIndex(index, primaryKeyColumns)).forEach(statement -> statements.updateFunction(statement, updateOptions::getCreateTables, true));
    } else {
        DatabaseClient databaseClient = clientsHolder.getDatabaseClient(clientId);
        List<SpannerColumn> allColumns = Scanner.of(primaryKeyColumns, nonKeyColumns).concat(Scanner::of).list();
        ResultSet columnRs = databaseClient.singleUse().executeQuery(Statement.of(tableOperationsGenerator.getTableSchema(tableName)));
        ResultSet primaryKeyRs = databaseClient.singleUse().executeQuery(Statement.of(tableOperationsGenerator.getTableIndexColumnsSchema(tableName, "PRIMARY_KEY")));
        tableAlterSchemaService.generateUpdateStatementColumns(tableName, allColumns, primaryKeyColumns, columnRs, primaryKeyRs, statements);
        ResultSet indexesRs = databaseClient.singleUse().executeQuery(Statement.of(tableOperationsGenerator.getTableIndexSchema(tableName)));
        Set<String> currentIndexes = tableAlterSchemaService.getIndexes(indexesRs);
        Scanner.of(indexes, uniqueIndexes).concat(Scanner::of).forEach(index -> {
            Statement tableIndexColumnsSchema = Statement.of(tableOperationsGenerator.getTableIndexColumnsSchema(tableName, index.getIndexName()));
            ResultSet indexRs = databaseClient.singleUse().executeQuery(tableIndexColumnsSchema);
            if (!tableAlterSchemaService.indexEqual(index, indexRs)) {
                if (currentIndexes.contains(index.getIndexName())) {
                    statements.updateFunction(tableOperationsGenerator.dropIndex(index.getIndexName()), updateOptions::getDropIndexes, false);
                }
                statements.updateFunction(createIndex(index, primaryKeyColumns), updateOptions::getAddIndexes, true);
            }
            currentIndexes.remove(index.getIndexName());
        });
        currentIndexes.forEach(name -> statements.updateFunction(tableOperationsGenerator.dropIndex(name), updateOptions::getDropIndexes, false));
    }
    String errorMessage = null;
    if (!statements.getExecuteStatements().isEmpty()) {
        logger.info(SchemaUpdateTool.generateFullWidthMessage("Executing Spanner " + getClass().getSimpleName() + " SchemaUpdate"));
        logger.info(String.join("\n\n", statements.getExecuteStatements()));
        Database database = clientsHolder.getDatabase(clientId);
        OperationFuture<Void, UpdateDatabaseDdlMetadata> future = database.updateDdl(statements.getExecuteStatements(), null);
        errorMessage = FutureTool.get(future.getPollingFuture().getAttemptResult()).getErrorMessage();
        if (StringTool.notNullNorEmptyNorWhitespace(errorMessage)) {
            logger.error(errorMessage);
        }
    }
    if (statements.getPreventStartUp()) {
        errorMessage = "an alter on Spanner table " + tableName + " is required";
    }
    if (statements.getPrintStatements().isEmpty()) {
        return Optional.empty();
    }
    String printStatement = statements.getPrintStatements().stream().map(statement -> statement + ";").collect(Collectors.joining("\n"));
    SchemaUpdateTool.printSchemaUpdate(logger, printStatement);
    return Optional.of(new SchemaUpdateResult(printStatement, errorMessage, clientId));
}
Also used : Scanner(io.datarouter.scanner.Scanner) SchemaUpdateTool(io.datarouter.storage.config.schema.SchemaUpdateTool) OperationFuture(com.google.api.gax.longrunning.OperationFuture) SpannerDatabaseClientsHolder(io.datarouter.gcp.spanner.connection.SpannerDatabaseClientsHolder) LoggerFactory(org.slf4j.LoggerFactory) PhysicalNode(io.datarouter.storage.node.type.physical.PhysicalNode) DatabaseClient(com.google.cloud.spanner.DatabaseClient) Singleton(javax.inject.Singleton) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ResultSet(com.google.cloud.spanner.ResultSet) ClientId(io.datarouter.storage.client.ClientId) Logger(org.slf4j.Logger) Database(com.google.cloud.spanner.Database) Set(java.util.Set) StringTool(io.datarouter.util.string.StringTool) Collectors(java.util.stream.Collectors) SchemaUpdateOptions(io.datarouter.storage.config.schema.SchemaUpdateOptions) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) Statement(com.google.cloud.spanner.Statement) List(java.util.List) Field(io.datarouter.model.field.Field) SpannerFieldCodecRegistry(io.datarouter.gcp.spanner.field.SpannerFieldCodecRegistry) Optional(java.util.Optional) IndexedStorage(io.datarouter.storage.node.op.raw.IndexedStorage) Collections(java.util.Collections) SpannerBaseFieldCodec(io.datarouter.gcp.spanner.field.SpannerBaseFieldCodec) SchemaUpdateResult(io.datarouter.storage.config.schema.SchemaUpdateResult) FutureTool(io.datarouter.util.concurrent.FutureTool) Scanner(io.datarouter.scanner.Scanner) ArrayList(java.util.ArrayList) Field(io.datarouter.model.field.Field) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) ResultSet(com.google.cloud.spanner.ResultSet) Database(com.google.cloud.spanner.Database) IndexedStorage(io.datarouter.storage.node.op.raw.IndexedStorage) Statement(com.google.cloud.spanner.Statement) DatabaseClient(com.google.cloud.spanner.DatabaseClient) SchemaUpdateResult(io.datarouter.storage.config.schema.SchemaUpdateResult)

Example 54 with Database

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

the class DatabaseAdminClientSnippets method createDatabase.

/**
 * Example to create database.
 */
// [TARGET createDatabase(String, String, Iterable)]
// [VARIABLE my_instance_id]
// [VARIABLE my_database_id]
public Database createDatabase(String instanceId, String databaseId) {
    // [START createDatabase]
    OperationFuture<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, databaseId, Arrays.asList("CREATE TABLE Singers (\n" + "  SingerId   INT64 NOT NULL,\n" + "  FirstName  STRING(1024),\n" + "  LastName   STRING(1024),\n" + "  SingerInfo BYTES(MAX)\n" + ") PRIMARY KEY (SingerId)", "CREATE TABLE Albums (\n" + "  SingerId     INT64 NOT NULL,\n" + "  AlbumId      INT64 NOT NULL,\n" + "  AlbumTitle   STRING(MAX)\n" + ") PRIMARY KEY (SingerId, AlbumId),\n" + "  INTERLEAVE IN PARENT Singers ON DELETE CASCADE"));
    Database db;
    try {
        db = op.get();
    } catch (ExecutionException e) {
        throw (SpannerException) e.getCause();
    } catch (InterruptedException e) {
        throw SpannerExceptionFactory.propagateInterrupt(e);
    }
    // [END createDatabase]
    return db;
}
Also used : Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExecutionException(java.util.concurrent.ExecutionException)

Example 55 with Database

use of com.google.spanner.admin.database.v1.Database 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)

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