mchades commented on code in PR #9027:
URL: https://github.com/apache/gravitino/pull/9027#discussion_r2498168575


##########
core/src/test/java/org/apache/gravitino/storage/TestSQLScripts.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * 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.gravitino.storage;
+
+import static 
org.apache.gravitino.integration.test.util.TestDatabaseName.MYSQL_JDBC_BACKEND;
+import static 
org.apache.gravitino.integration.test.util.TestDatabaseName.PG_JDBC_BACKEND;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.integration.test.container.MySQLContainer;
+import org.apache.gravitino.integration.test.container.PostgreSQLContainer;
+import org.apache.gravitino.integration.test.util.BaseIT;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+public class TestSQLScripts extends BaseIT {
+
+  private String jdbcBackend;
+  private Path scriptDir;
+  @TempDir private File tempDir;
+
+  @BeforeAll
+  public void startIntegrationTest() {
+    jdbcBackend = System.getenv("jdbcBackend");
+    Assertions.assertNotNull(jdbcBackend, "jdbcBackend environment variable is 
not set");
+
+    String gravitinoHome = System.getenv("GRAVITINO_HOME");
+    Assertions.assertNotNull(gravitinoHome, "GRAVITINO_HOME environment 
variable is not set");
+    scriptDir = Path.of(gravitinoHome, "scripts", jdbcBackend.toLowerCase());
+    Assertions.assertTrue(Files.exists(scriptDir), "Script directory does not 
exist: " + scriptDir);
+  }
+
+  @Test
+  public void testSQLScripts() throws IOException, SQLException {
+    File[] scriptFiles = scriptDir.toFile().listFiles();
+    Assertions.assertNotNull(scriptFiles, "No script files found in " + 
scriptDir);
+    // Sort files to ensure correct execution order (schema -> upgrade)
+    Arrays.sort(scriptFiles, Comparator.comparing(File::getName));
+
+    // A map to store connections for different schema versions
+    Map<String, Connection> versionConnections = new HashMap<>();
+    Pattern schemaPattern =
+        Pattern.compile("schema-([\\d.]+)-" + jdbcBackend.toLowerCase() + 
"\\.sql");
+    Pattern upgradePattern =
+        Pattern.compile("upgrade-([\\d.]+)-to-([\\d.]+)-" + 
jdbcBackend.toLowerCase() + "\\.sql");
+
+    for (File scriptFile : scriptFiles) {
+      Matcher schemaMatcher = schemaPattern.matcher(scriptFile.getName());
+      Matcher upgradeMatcher = upgradePattern.matcher(scriptFile.getName());
+
+      if (schemaMatcher.matches()) {
+        String version = schemaMatcher.group(1);
+        String dbName = "schema_" + version.replace('.', '_');
+
+        Connection conn = getSQLConnection(jdbcBackend, dbName);
+        Assertions.assertDoesNotThrow(
+            () -> executeSQLScript(conn, scriptFile),
+            "Failed to execute schema script for version " + version);
+        versionConnections.put(version, conn);
+
+      } else if (upgradeMatcher.matches()) {
+        String fromVersion = upgradeMatcher.group(1);
+
+        Connection conn = versionConnections.get(fromVersion);
+        Assertions.assertNotNull(
+            conn, "No existing database connection found for version " + 
fromVersion);
+        Assertions.assertDoesNotThrow(
+            () -> executeSQLScript(conn, scriptFile),
+            "Failed to execute upgrade script" + " in file " + 
scriptFile.getName());
+      } else {
+        Assertions.fail("Unrecognized script file name: " + 
scriptFile.getName());
+      }
+    }
+
+    // Close all connections
+    for (Connection conn : versionConnections.values()) {
+      if (conn != null) {
+        conn.close();
+      }
+    }
+  }

Review Comment:
   It's a UT, and whether to use a container depends on the value of 
`jdbcBackend`, so it doesn't require the docker test tag.



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