This is an automated email from the ASF dual-hosted git repository.

JackieTien97 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 3a4524f859d Fix output path handling for table schema export
3a4524f859d is described below

commit 3a4524f859da0b4608569fe843312c7a0f82382a
Author: DaZuiZui <[email protected]>
AuthorDate: Thu Dec 3 17:52:12 2026 +0800

    Fix output path handling for table schema export
---
 .../apache/iotdb/tools/it/ExportSchemaTestIT.java  | 101 ++++++++++++++++++++-
 .../iotdb/tool/schema/AbstractExportSchema.java    |   4 +-
 .../iotdb/tool/schema/ExportSchemaTable.java       |  14 +--
 3 files changed, 111 insertions(+), 8 deletions(-)

diff --git 
a/integration-test/src/test/java/org/apache/iotdb/tools/it/ExportSchemaTestIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/tools/it/ExportSchemaTestIT.java
index 39325a47e41..b6c242a75e9 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/tools/it/ExportSchemaTestIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/tools/it/ExportSchemaTestIT.java
@@ -20,10 +20,14 @@ package org.apache.iotdb.tools.it;
 
 import org.apache.iotdb.cli.it.AbstractScriptIT;
 import org.apache.iotdb.isession.ISession;
+import org.apache.iotdb.isession.ITableSession;
+import org.apache.iotdb.isession.SessionDataSet;
 import org.apache.iotdb.it.env.EnvFactory;
 import org.apache.iotdb.it.framework.IoTDBTestRunner;
 import org.apache.iotdb.itbase.category.ClusterIT;
 import org.apache.iotdb.itbase.category.LocalStandaloneIT;
+import org.apache.iotdb.itbase.category.TableClusterIT;
+import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
 import org.apache.iotdb.rpc.IoTDBConnectionException;
 import org.apache.iotdb.rpc.StatementExecutionException;
 import org.apache.iotdb.tool.common.Constants;
@@ -33,16 +37,26 @@ import 
org.apache.tsfile.file.metadata.enums.CompressionType;
 import org.apache.tsfile.file.metadata.enums.TSEncoding;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
+import org.junit.rules.TemporaryFolder;
 import org.junit.runner.RunWith;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 @RunWith(IoTDBTestRunner.class)
-@Category({LocalStandaloneIT.class, ClusterIT.class})
 public class ExportSchemaTestIT extends AbstractScriptIT {
+  @Rule public TemporaryFolder folder = new TemporaryFolder();
   private static String ip;
 
   private static String port;
@@ -70,6 +84,7 @@ public class ExportSchemaTestIT extends AbstractScriptIT {
   }
 
   @Test
+  @Category({LocalStandaloneIT.class, ClusterIT.class})
   public void test() throws IOException {
     String os = System.getProperty("os.name").toLowerCase();
     if (os.startsWith("windows")) {
@@ -137,6 +152,90 @@ public class ExportSchemaTestIT extends AbstractScriptIT {
     testOutput(builder, output, 0);
   }
 
+  @Test
+  @Category({TableLocalStandaloneIT.class, TableClusterIT.class})
+  public void testTableExportOutputPath() throws Exception {
+    String database = "schema_export_path";
+    String ddl;
+    try (ITableSession session = 
EnvFactory.getEnv().getTableSessionConnection()) {
+      session.executeNonQueryStatement("CREATE DATABASE " + database);
+      session.executeNonQueryStatement("USE " + database);
+      session.executeNonQueryStatement("CREATE TABLE sensors (v INT64 FIELD)");
+      try (SessionDataSet result = session.executeQueryStatement("SHOW CREATE 
TABLE sensors")) {
+        assertTrue(result.hasNext());
+        ddl = result.next().getField(1).getStringValue();
+      }
+    }
+    String fileName = "dump_" + database + ".sql";
+    String expectedSql = "DROP TABLE IF EXISTS \"sensors\";\n" + ddl + ";\n";
+    Path target = 
folder.getRoot().toPath().resolve("parent").resolve("schemas");
+    String output = exportTableSchema(database, target, Constants.CODE_OK);
+    assertTrue(output, output.contains(Constants.EXPORT_COMPLETELY));
+    assertEquals(expectedSql, Files.readString(target.resolve(fileName)));
+
+    // Creating a symbolic link on Windows can require elevated privileges.
+    if (File.separatorChar != '\\') {
+      Path archive = folder.newFolder("archive").toPath();
+      Path link = 
Files.createSymbolicLink(folder.getRoot().toPath().resolve("latest"), archive);
+      exportTableSchema(database, link, Constants.CODE_OK);
+      assertEquals(expectedSql, Files.readString(archive.resolve(fileName)));
+    }
+
+    Path blocked = folder.newFolder("blocked").toPath();
+    Files.createDirectory(blocked.resolve(fileName));
+    output = exportTableSchema(database, blocked, Constants.CODE_ERROR);
+    assertTrue(output, output.contains(fileName));
+    assertFalse(output, output.contains(Constants.EXPORT_COMPLETELY));
+  }
+
+  private String exportTableSchema(String database, Path target, int 
expectedStatus)
+      throws Exception {
+    ProcessBuilder builder = new ProcessBuilder();
+    List<String> command = builder.command();
+    if (File.separatorChar == '\\') {
+      command.addAll(
+          List.of(
+              "cmd.exe",
+              "/c",
+              Path.of(toolsPath, "windows", "schema", 
"export-schema.bat").toString()));
+    } else {
+      command.addAll(List.of("bash", Path.of(toolsPath, "schema", 
"export-schema.sh").toString()));
+    }
+    command.addAll(
+        List.of(
+            "-h",
+            ip,
+            "-p",
+            port,
+            "-sql_dialect",
+            "table",
+            "-db",
+            database,
+            "-t",
+            target.toString()));
+    builder.environment().put("IOTDB_HOME", homePath);
+    File console = folder.newFile();
+    Process process = 
builder.redirectErrorStream(true).redirectOutput(console).start();
+    try {
+      assertTrue("Export command timed out", process.waitFor(60, 
TimeUnit.SECONDS));
+      String output = Files.readString(console.toPath());
+      assertEquals(output, expectedStatus, process.exitValue());
+      return output;
+    } finally {
+      if (process.isAlive()) {
+        List<ProcessHandle> descendants = process.descendants().toList();
+        try {
+          descendants.forEach(ProcessHandle::destroyForcibly);
+          for (ProcessHandle child : descendants) {
+            child.onExit().orTimeout(10, TimeUnit.SECONDS).join();
+          }
+        } finally {
+          process.destroyForcibly().onExit().orTimeout(10, 
TimeUnit.SECONDS).join();
+        }
+      }
+    }
+  }
+
   public void prepareSchema() {
     try (ISession session = EnvFactory.getEnv().getSessionConnection()) {
       session.open();
diff --git 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/AbstractExportSchema.java
 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/AbstractExportSchema.java
index 902198a7ad2..a036d4d15ff 100644
--- 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/AbstractExportSchema.java
+++ 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/AbstractExportSchema.java
@@ -22,12 +22,14 @@ package org.apache.iotdb.tool.schema;
 import org.apache.iotdb.rpc.IoTDBConnectionException;
 import org.apache.iotdb.rpc.StatementExecutionException;
 
+import java.io.IOException;
+
 public abstract class AbstractExportSchema extends AbstractSchemaTool {
 
   public abstract void init()
       throws InterruptedException, IoTDBConnectionException, 
StatementExecutionException;
 
-  protected abstract void exportSchemaToSqlFile();
+  protected abstract void exportSchemaToSqlFile() throws IOException;
 
   protected abstract void exportSchemaToCsvFile(String pathPattern, int index);
 }
diff --git 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ExportSchemaTable.java
 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ExportSchemaTable.java
index 6d5c6a8fca8..9d184c089c8 100644
--- 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ExportSchemaTable.java
+++ 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/schema/ExportSchemaTable.java
@@ -36,9 +36,10 @@ import org.apache.tsfile.external.commons.lang3.StringUtils;
 import org.apache.tsfile.read.common.Field;
 import org.apache.tsfile.read.common.RowRecord;
 
-import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -192,10 +193,11 @@ public class ExportSchemaTable extends 
AbstractExportSchema {
   }
 
   @Override
-  protected void exportSchemaToSqlFile() {
-    File file = new File(targetDirectory);
-    if (!file.isDirectory()) {
-      file.mkdir();
+  protected void exportSchemaToSqlFile() throws IOException {
+    Path target = Path.of(targetDirectory);
+    // Older JDK 17 releases reject an existing directory symlink in 
createDirectories.
+    if (!Files.isDirectory(target)) {
+      Files.createDirectories(target);
     }
     String fileName = targetDirectory + targetFile + "_" + database + ".sql";
     final Iterator<String> iterator = tableCommentList.keySet().iterator();
@@ -211,7 +213,7 @@ public class ExportSchemaTable extends AbstractExportSchema 
{
                     escapeSqlIdentifer(database),
                     escapeSqlIdentifer(tableName)));
         exportSchemaByShowCreate(sessionDataSet, fileName, tableName);
-      } catch (IoTDBConnectionException | StatementExecutionException | 
IOException e) {
+      } catch (IoTDBConnectionException | StatementExecutionException e) {
         ioTPrinter.println(Constants.COLUMN_SQL_MEET_ERROR_MSG + 
e.getMessage());
       } finally {
         if (ObjectUtils.isNotEmpty(sessionDataSet)) {

Reply via email to