Merge "Improve DatabaseSchemaCreator"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/spannerrefdb/DatabaseSchemaCreator.java b/src/main/java/com/googlesource/gerrit/plugins/spannerrefdb/DatabaseSchemaCreator.java
index 7bed262..2990a1b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/spannerrefdb/DatabaseSchemaCreator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/spannerrefdb/DatabaseSchemaCreator.java
@@ -15,17 +15,13 @@
package com.googlesource.gerrit.plugins.spannerrefdb;
import com.gerritforge.gerrit.globalrefdb.GlobalRefDbSystemError;
-import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.DatabaseId;
-import com.google.cloud.spanner.ErrorCode;
-import com.google.cloud.spanner.SpannerException;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.inject.Inject;
import com.google.inject.Singleton;
-import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
-import java.util.Arrays;
+import java.util.List;
@Singleton
class DatabaseSchemaCreator implements LifecycleListener {
@@ -42,47 +38,38 @@
@Override
public void start() {
- createTable(
- "CREATE TABLE refs ("
- + "project STRING(MAX) NOT NULL,"
- + "ref STRING(MAX) NOT NULL,"
- + "value STRING(MAX) NOT NULL"
- + ") PRIMARY KEY (project, ref)");
- createTable(
- "CREATE TABLE locks ("
- + "project STRING(MAX) NOT NULL,"
- + "ref STRING(MAX) NOT NULL,"
- + "heartbeat TIMESTAMP OPTIONS ("
- + " allow_commit_timestamp = true"
- + "),"
- + "token TIMESTAMP OPTIONS ("
- + " allow_commit_timestamp = true"
- + "),"
- + "owner STRING(MAX) NOT NULL"
- + ") PRIMARY KEY (project, ref)");
+
+ try {
+ dbAdminClient
+ .updateDatabaseDdl(
+ dbId.getInstanceId().getInstance(),
+ dbId.getDatabase(),
+ List.of(
+ "CREATE TABLE IF NOT EXISTS refs"
+ + "("
+ + "project STRING(MAX) NOT NULL,"
+ + "ref STRING(MAX) NOT NULL,"
+ + "value STRING(MAX) NOT NULL"
+ + ")"
+ + "PRIMARY KEY (project, ref)",
+ "CREATE TABLE IF NOT EXISTS locks"
+ + "("
+ + "project STRING(MAX) NOT NULL,"
+ + "ref STRING(MAX) NOT NULL,"
+ + "heartbeat TIMESTAMP OPTIONS (allow_commit_timestamp = true),"
+ + "token TIMESTAMP OPTIONS (allow_commit_timestamp = true),"
+ + "owner STRING(MAX) NOT NULL"
+ + ")"
+ + "PRIMARY KEY (project, ref)"),
+ null)
+ .get();
+ } catch (Exception e) {
+ throw new GlobalRefDbSystemError(
+ String.format("Failed to create schema in database [%s]", dbId), e);
+ }
+ logger.atInfo().log("Created schema in database [%s]", dbId);
}
@Override
public void stop() {}
-
- private void createTable(String statement) {
- try {
- OperationFuture<Void, UpdateDatabaseDdlMetadata> op =
- dbAdminClient.updateDatabaseDdl(
- dbId.getInstanceId().getInstance(),
- dbId.getDatabase(),
- Arrays.asList(statement),
- null);
- op.get();
- logger.atInfo().log("Created table in database [%s] with statement %s", dbId, statement);
- } catch (Exception e) {
- Throwable cause = e.getCause();
- if (cause instanceof SpannerException) {
- ErrorCode code = ((SpannerException) cause).getErrorCode();
- if (code != ErrorCode.FAILED_PRECONDITION) {
- throw new GlobalRefDbSystemError("Failed to create table", e);
- }
- }
- }
- }
}