fix(vastbase): include check constraints in table DDL
This commit is contained in:
1 parent
b5f462f8e0
commit
43f52463de
4 files changed
+169
-1
No files matched your search
@@ -0,0 +1,19 @@
|
||||
package com.dbx.agent;
|
||||
|
||||
public final class CheckConstraintInfo {
|
||||
private final String name;
|
||||
private final String definition;
|
||||
|
||||
public CheckConstraintInfo(String name, String definition) {
|
||||
this.name = name;
|
||||
this.definition = definition;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getDefinition() {
|
||||
return definition;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.dbx.agent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -37,6 +38,28 @@ public final class DdlBuilder {
|
||||
List<ForeignKeyInfo> foreignKeys,
|
||||
boolean useBacktick,
|
||||
boolean includeColumnComments
|
||||
) {
|
||||
return buildTableDdl(
|
||||
schema,
|
||||
table,
|
||||
columns,
|
||||
indexes,
|
||||
foreignKeys,
|
||||
Collections.emptyList(),
|
||||
useBacktick,
|
||||
includeColumnComments
|
||||
);
|
||||
}
|
||||
|
||||
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 tableRef = qualifiedName(schema, table, useBacktick);
|
||||
List<String> columnLines = new ArrayList<>();
|
||||
@@ -74,6 +97,16 @@ public final class DdlBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
for (CheckConstraintInfo constraint : checkConstraints) {
|
||||
if (!notBlank(constraint.getDefinition())) {
|
||||
continue;
|
||||
}
|
||||
String name = notBlank(constraint.getName())
|
||||
? "CONSTRAINT " + quoteIdent(constraint.getName(), useBacktick) + " "
|
||||
: "";
|
||||
columnLines.add(" " + name + constraint.getDefinition());
|
||||
}
|
||||
|
||||
StringBuilder ddl = new StringBuilder();
|
||||
ddl.append("CREATE TABLE ");
|
||||
ddl.append(tableRef);
|
||||
|
||||
@@ -245,7 +245,50 @@ public abstract class PostgresLikeAgent extends AbstractJdbcAgent {
|
||||
foreignKeys = Collections.emptyList();
|
||||
}
|
||||
|
||||
return DdlBuilder.buildTableDdl(schema, table, getColumns(schema, table), indexes, foreignKeys, mysqlCompatMode, true);
|
||||
List<CheckConstraintInfo> checkConstraints;
|
||||
try {
|
||||
checkConstraints = listCheckConstraints(schema, table);
|
||||
} catch (RuntimeException e) {
|
||||
// Some PostgreSQL-compatible databases expose incomplete catalog APIs.
|
||||
// DDL generation should still succeed with columns, indexes, and foreign keys.
|
||||
checkConstraints = Collections.emptyList();
|
||||
}
|
||||
|
||||
return DdlBuilder.buildTableDdl(
|
||||
schema,
|
||||
table,
|
||||
getColumns(schema, table),
|
||||
indexes,
|
||||
foreignKeys,
|
||||
checkConstraints,
|
||||
mysqlCompatMode,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
protected List<CheckConstraintInfo> listCheckConstraints(String schema, String table) {
|
||||
return unchecked(() -> {
|
||||
List<CheckConstraintInfo> result = new ArrayList<>();
|
||||
String sql = "SELECT co.conname AS constraint_name, pg_catalog.pg_get_constraintdef(co.oid, true) AS constraint_definition " +
|
||||
"FROM pg_catalog.pg_constraint co " +
|
||||
"JOIN pg_catalog.pg_class c ON c.oid = co.conrelid " +
|
||||
"JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace " +
|
||||
"WHERE co.contype = 'c' AND n.nspname = ? AND c.relname = ? " +
|
||||
"ORDER BY co.conname";
|
||||
try (java.sql.PreparedStatement stmt = requireConnection().prepareStatement(sql)) {
|
||||
stmt.setString(1, schema);
|
||||
stmt.setString(2, table);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
result.add(new CheckConstraintInfo(
|
||||
rs.getString("constraint_name"),
|
||||
rs.getString("constraint_definition")
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user