fix(ddl): include table comments in generated DDL

This commit is contained in:
gggaiitx authored and GitHub committed 2026-07-13 21:06:08 +08:00
1 parent 5999fbf00c
commit 8a6640e184
12 files changed
+387 -17

No files matched your search

@@ -41,7 +41,14 @@ public abstract class BaseDatabaseAgent implements DatabaseAgent {
foreignKeys = Collections.emptyList();
}
return DatabaseAgent.buildTableDdl(schema, table, getColumns(schema, table), indexes, foreignKeys);
String tableComment = null;
try {
tableComment = getTableComment(schema, table);
} catch (RuntimeException e) {
// Table comment is optional; DDL generation should still succeed without it.
}
return DdlBuilder.buildTableDdl(schema, table, getColumns(schema, table), indexes, foreignKeys, Collections.emptyList(), false, false, tableComment);
}
@Override
@@ -144,7 +144,14 @@ public abstract class ConfiguredJdbcAgent extends AbstractJdbcAgent {
foreignKeys = Collections.emptyList();
}
return DatabaseAgent.buildTableDdl(schema, table, getColumns(schema, table), indexes, foreignKeys);
String tableComment = null;
try {
tableComment = getTableComment(schema, table);
} catch (RuntimeException e) {
// Table comment is optional; DDL generation should still succeed without it.
}
return DdlBuilder.buildTableDdl(schema, table, getColumns(schema, table), indexes, foreignKeys, Collections.emptyList(), false, false, tableComment);
}
@Override
@@ -82,7 +82,51 @@ public interface DatabaseAgent {
foreignKeys = Collections.emptyList();
}
return buildTableDdl(schema, table, getColumns(schema, table), indexes, foreignKeys);
String tableComment = null;
try {
tableComment = getTableComment(schema, table);
} catch (RuntimeException e) {
// Table comment is optional; DDL generation should still succeed without it.
}
return DdlBuilder.buildTableDdl(schema, table, getColumns(schema, table), indexes, foreignKeys, Collections.emptyList(), false, false, tableComment);
}
/**
* Returns the comment/description for a table, or null if not available.
* Default implementation looks up the comment from listTables results.
* Subclasses can override for more efficient queries.
*/
default String getTableComment(String schema, String table) {
try {
String caseInsensitiveComment = null;
int caseInsensitiveMatches = 0;
for (TableInfo info : listTables(schema)) {
if (!info.getName().equalsIgnoreCase(table)) {
continue;
}
if (info.getName().equals(table)) {
return nonBlankComment(info.getComment());
}
caseInsensitiveMatches++;
caseInsensitiveComment = nonBlankComment(info.getComment());
}
// Case-insensitive databases may normalize metadata names, but quoted
// mixed-case objects must never borrow a sibling table's comment.
if (caseInsensitiveMatches == 1) {
return caseInsensitiveComment;
}
} catch (RuntimeException e) {
// Ignore; table comment is optional.
}
return null;
}
static String nonBlankComment(String comment) {
if (comment != null && !comment.trim().isEmpty()) {
return comment;
}
return null;
}
List<IndexInfo> listIndexes(String schema, String table);
@@ -16,7 +16,7 @@ public final class DdlBuilder {
List<IndexInfo> indexes,
List<ForeignKeyInfo> foreignKeys
) {
return buildTableDdl(schema, table, columns, indexes, foreignKeys, false, false);
return buildTableDdl(schema, table, columns, indexes, foreignKeys, Collections.emptyList(), false, false, null);
}
public static String buildTableDdl(
@@ -27,7 +27,7 @@ public final class DdlBuilder {
List<ForeignKeyInfo> foreignKeys,
boolean useBacktick
) {
return buildTableDdl(schema, table, columns, indexes, foreignKeys, useBacktick, false);
return buildTableDdl(schema, table, columns, indexes, foreignKeys, Collections.emptyList(), useBacktick, false, null);
}
public static String buildTableDdl(
@@ -47,7 +47,8 @@ public final class DdlBuilder {
foreignKeys,
Collections.emptyList(),
useBacktick,
includeColumnComments
includeColumnComments,
null
);
}
@@ -60,6 +61,20 @@ public final class DdlBuilder {
List<CheckConstraintInfo> checkConstraints,
boolean useBacktick,
boolean includeColumnComments
) {
return buildTableDdl(schema, table, columns, indexes, foreignKeys, checkConstraints, useBacktick, includeColumnComments, null);
}
public static String buildTableDdl(
String schema,
String table,
List<ColumnInfo> columns,
List<IndexInfo> indexes,
List<ForeignKeyInfo> foreignKeys,
List<CheckConstraintInfo> checkConstraints,
boolean useBacktick,
boolean includeColumnComments,
String tableComment
) {
String tableRef = qualifiedName(schema, table, useBacktick);
List<String> columnLines = new ArrayList<>();
@@ -114,6 +129,14 @@ public final class DdlBuilder {
ddl.append(join(columnLines, ",\n"));
ddl.append("\n);\n");
if (notBlank(tableComment)) {
ddl.append("\nCOMMENT ON TABLE ");
ddl.append(tableRef);
ddl.append(" IS '");
ddl.append(sqlStringBody(tableComment));
ddl.append("';");
}
if (includeColumnComments) {
for (ColumnInfo column : columns) {
if (!notBlank(column.getComment())) {
@@ -259,6 +259,13 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
checkConstraints = Collections.emptyList();
}
String tableComment = null;
try {
tableComment = getTableComment(schema, table);
} catch (RuntimeException e) {
// Table comment is optional; DDL generation should still succeed without it.
}
return DdlBuilder.buildTableDdl(
schema,
table,
@@ -267,10 +274,34 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
foreignKeys,
checkConstraints,
mysqlCompatMode,
true
true,
tableComment
);
}
@Override
public String getTableComment(String schema, String table) {
return unchecked(() -> {
try (java.sql.PreparedStatement stmt = requireConnection().prepareStatement(
"SELECT obj_description(c.oid) AS table_comment " +
"FROM pg_catalog.pg_class c " +
"JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace " +
"WHERE n.nspname = ? AND c.relname = ? AND c.relkind IN ('r','m','f','p') " +
"LIMIT 1"
)) {
stmt.setString(1, schema);
stmt.setString(2, table);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
String comment = rs.getString("table_comment");
return (comment != null && !comment.trim().isEmpty()) ? comment : null;
}
}
}
return null;
});
}
protected List<CheckConstraintInfo> listCheckConstraints(String schema, String table) {
return unchecked(() -> {
List<CheckConstraintInfo> result = new ArrayList<>();