flyrain commented on code in PR #2762:
URL: https://github.com/apache/polaris/pull/2762#discussion_r2412009737


##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java:
##########
@@ -748,14 +748,28 @@ static int loadSchemaVersion(
       }
       return schemaVersion.getFirst().getValue();
     } catch (SQLException e) {
-      LOGGER.error("Failed to load schema version due to {}", e.getMessage(), 
e);
       if (fallbackOnDoesNotExist && 
datasourceOperations.isRelationDoesNotExist(e)) {
         return SchemaVersion.MINIMUM.getValue();
       }
+      LOGGER.error("Failed to load schema version due to {}", e.getMessage(), 
e);

Review Comment:
   Minor: Do we need this error message given that we throw right after it?



##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBootstrapUtils.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.polaris.persistence.relational.jdbc;
+
+import java.util.Optional;
+import org.apache.polaris.core.persistence.bootstrap.BootstrapOptions;
+import org.apache.polaris.core.persistence.bootstrap.SchemaOptions;
+
+public class JdbcBootstrapUtils {
+
+  private JdbcBootstrapUtils() {}
+
+  /**
+   * Determines the correct schema version to use for bootstrapping a realm.
+   *
+   * @param currentSchemaVersion The current version of the database schema.
+   * @param requiredSchemaVersion The requested schema version (-1 for 
auto-detection).
+   * @param hasAlreadyBootstrappedRealms Flag indicating if any realms already 
exist.
+   * @return The calculated bootstrap schema version.
+   * @throws IllegalStateException if the combination of parameters represents 
an invalid state.
+   */
+  public static int getRealmBootstrapSchemaVersion(
+      int currentSchemaVersion, int requiredSchemaVersion, boolean 
hasAlreadyBootstrappedRealms) {
+
+    // If versions already match, no change is needed.
+    if (currentSchemaVersion == requiredSchemaVersion) {
+      return requiredSchemaVersion;
+    }
+
+    // Handle fresh installations where no schema version is recorded (version 
0).
+    if (currentSchemaVersion == 0) {
+      if (hasAlreadyBootstrappedRealms) {
+        // System was bootstrapped with v1 before schema versioning was 
introduced.
+        if (requiredSchemaVersion == -1 || requiredSchemaVersion == 1) {
+          return 1;
+        }
+      } else {
+        // A truly fresh start. Default to v3 for auto-detection, otherwise 
use the specified
+        // version.
+        return requiredSchemaVersion == -1 ? 3 : requiredSchemaVersion;

Review Comment:
   LGTM, we could improve it by having a variable like `latestSchemaVersion`, 
so that we don't have to change this method every time we update the schema 
version.



##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBootstrapUtils.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.polaris.persistence.relational.jdbc;
+
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.polaris.core.persistence.bootstrap.BootstrapOptions;
+import org.apache.polaris.core.persistence.bootstrap.SchemaOptions;
+
+public class JdbcBootstrapUtils {
+
+  // Define a pattern to find 'v' followed by one or more digits (\d+)
+  private static final Pattern pattern = Pattern.compile("(v\\d+)");
+
+  private JdbcBootstrapUtils() {}
+
+  /**
+   * Determines the correct schema version to use for bootstrapping a realm.
+   *
+   * @param currentSchemaVersion The current version of the database schema.
+   * @param requiredSchemaVersion The requested schema version (-1 for 
auto-detection).
+   * @param hasAlreadyBootstrappedRealms Flag indicating if any realms already 
exist.
+   * @return The calculated bootstrap schema version.
+   * @throws IllegalStateException if the combination of parameters represents 
an invalid state.
+   */
+  public static int getRealmBootstrapSchemaVersion(
+      int currentSchemaVersion, int requiredSchemaVersion, boolean 
hasAlreadyBootstrappedRealms) {

Review Comment:
   I'd prefer to have `hasAlreadyBootstrappedRealms` as an input other than 
embedded within this method. It make the tests much easier by passing a 
boolean. 



##########
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/DatasourceOperations.java:
##########
@@ -52,9 +52,13 @@ public class DatasourceOperations {
 
   private static final Logger LOGGER = 
LoggerFactory.getLogger(DatasourceOperations.class);
 
+  // PG STATUS CODES
   private static final String CONSTRAINT_VIOLATION_SQL_CODE = "23505";

Review Comment:
   Not a blocker: do we need to handle a different code for H2? It would be 
nice to do so, but it's not related to this PR. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to