Copilot commented on code in PR #6383:
URL: https://github.com/apache/hive/pull/6383#discussion_r2987247746


##########
standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/tools/schematool/TestSchemaToolForMetastore.java:
##########
@@ -486,6 +486,161 @@ public void testMetastoreDbPropertiesAfterUpgrade() 
throws HiveMetaException, IO
     validateMetastoreDbPropertiesTable();
   }
 
+  /**
+   * Runs {@code ddlScripts} twice. The first run applies all DDL normally. 
The second run
+   * re-runs the same script from scratch — exactly as a re-run of a failed 
upgrade would —
+   * and must not throw, because {@link IdempotentDDLExecutor} swallows 
"already exists" and
+   * "already gone" errors via {@link DbErrorCodes}.
+   */
+  private void executeWithIdempotencyCheck(String testName, String[] 
ddlScripts) throws Exception {
+    File scriptFile = generateTestScript(ddlScripts);
+    try {
+      schemaTool.execSql(scriptFile.getPath());
+    } catch (Exception e) {
+      Assert.fail("[" + testName + "] First run failed for " + 
dbms.getDbType() + ": " + e.getMessage());
+    }
+    try {
+      schemaTool.execSql(scriptFile.getPath());
+    } catch (IOException e) {
+      Assert.fail("[" + testName + "] Idempotent retry failed for " + 
dbms.getDbType()
+          + " — DbErrorCodes did not cover the error: " + e.getMessage());

Review Comment:
   In `executeWithIdempotencyCheck`, the retry path only catches `IOException` 
and the failure message assumes the root cause is missing `DbErrorCodes` 
coverage. `schemaTool.execSql(...)` can also throw other runtime exceptions 
(and can wrap SQL failures in an `IOException` whose root cause is the 
interesting part). Catch `Exception` here and include the exception/cause in 
the assertion message so failures from SQL syntax / parsing / connection issues 
don't get misreported as an error-code mapping problem.
   ```suggestion
       } catch (Exception e) {
         Assert.fail("[" + testName + "] Idempotent retry failed for " + 
dbms.getDbType()
             + " — possible missing DbErrorCodes mapping or underlying 
SQL/configuration issue. "
             + "Exception: " + e + ", cause: " + String.valueOf(e.getCause()));
   ```



##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/IdempotentDDLExecutor.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.hadoop.hive.metastore.tools.schematool;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.List;
+
+public class IdempotentDDLExecutor {
+  private static final Logger LOG = 
LoggerFactory.getLogger(IdempotentDDLExecutor.class);
+
+  private final Connection conn;
+  private final DbErrorCodes errorCodes;
+  private final HiveSchemaHelper.NestedScriptParser parser;
+  private final boolean verbose;
+
+  public IdempotentDDLExecutor(
+      Connection conn, String dbType, HiveSchemaHelper.NestedScriptParser 
parser, boolean verbose) {
+    this.conn = conn;
+    this.errorCodes = DbErrorCodes.forDbType(dbType);
+    this.parser = parser;
+    this.verbose = verbose;
+  }
+
+  public void executeScript(String scriptFile) throws SQLException, 
IOException {
+    LOG.info("Executing script line-by-line via IdempotentDDLExecutor: {}", 
scriptFile);
+    conn.setAutoCommit(true);
+
+    File file = new File(scriptFile);
+    List<String> commands = parser.getExecutableCommands(file.getParent(), 
file.getName());
+
+    for (String sqlStmt : commands) {
+      if (!sqlStmt.isEmpty()) {
+        executeStatement(sqlStmt);
+      }
+    }
+    if (verbose) {
+      System.out.println("Completed successfully.");
+    }
+  }
+
+  private void executeStatement(String sqlStmt) throws SQLException {
+    if (verbose) {
+      System.out.println("Executing: " + sqlStmt);
+    } 

Review Comment:
   `IdempotentDDLExecutor` uses `System.out.println(...)` for verbose output. 
Since this is also used in tests and the CLI already uses SLF4J logging, 
consider routing verbose output through the existing logging/printing utilities 
(or at least a single consistent output mechanism) to avoid interleaving stdout 
with other tool output and to make it easier to capture/redirect.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to