fix(sqlserver): recreate primary key when cloning tables
This commit is contained in:
1 parent
832e08ae1f
commit
7afa87e849
5 files changed
+189
-3
No files matched your search
+33
-1
@@ -428,7 +428,39 @@ public final class SqlServerLegacyAgent extends ConfiguredJdbcAgent {
|
||||
|
||||
@Override
|
||||
public List<IndexInfo> listIndexes(String schema, String table) {
|
||||
return super.listIndexes(metadataSchema(schema, table), table);
|
||||
String resolvedSchema = metadataSchema(schema, table);
|
||||
List<IndexInfo> indexes = super.listIndexes(resolvedSchema, table);
|
||||
return markPrimaryKeyIndex(indexes, resolvedSchema, table);
|
||||
}
|
||||
|
||||
// SQL Server names a primary-key index after its constraint (PK__<table>__<hex>
|
||||
// or a user-chosen name), so the shared JDBC metadata layer's "PRIMARY"
|
||||
// index-name convention never matches and is_primary stayed false. Resolve
|
||||
// the flag from DatabaseMetaData.getPrimaryKeys() PK_NAME instead, so table
|
||||
// cloning, the index tree badge, and DDL output see the real primary key.
|
||||
private List<IndexInfo> markPrimaryKeyIndex(List<IndexInfo> indexes, String schema, String table) {
|
||||
if (indexes.isEmpty()) {
|
||||
return indexes;
|
||||
}
|
||||
try {
|
||||
String primaryKeyName = null;
|
||||
try (java.sql.ResultSet rs = requireConnection().getMetaData().getPrimaryKeys(null, schema, table)) {
|
||||
if (rs.next()) {
|
||||
primaryKeyName = rs.getString("PK_NAME");
|
||||
}
|
||||
}
|
||||
if (primaryKeyName == null || primaryKeyName.trim().isEmpty()) {
|
||||
return indexes;
|
||||
}
|
||||
for (IndexInfo index : indexes) {
|
||||
if (primaryKeyName.equals(index.getName())) {
|
||||
index.setIs_primary(true);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
// Fail soft: keep the metadata-layer flags when primary-key lookup is unavailable.
|
||||
}
|
||||
return indexes;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+31
@@ -2,6 +2,7 @@ package com.dbx.agent.sqlserverlegacy;
|
||||
|
||||
import com.dbx.agent.ConnectParams;
|
||||
import com.dbx.agent.ColumnInfo;
|
||||
import com.dbx.agent.IndexInfo;
|
||||
import com.dbx.agent.test.TestSupport;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -603,4 +604,34 @@ class SqlServerLegacyAgentTest {
|
||||
private interface MethodHandler {
|
||||
Object handle(Method method, Object[] args) throws Throwable;
|
||||
}
|
||||
|
||||
@Test
|
||||
void listIndexesMarksPrimaryKeyByConstraintName() {
|
||||
SqlServerLegacyAgent agent = new SqlServerLegacyAgent();
|
||||
DatabaseMetaData metadata = proxy(DatabaseMetaData.class, (method, args) -> {
|
||||
if ("getIndexInfo".equals(method.getName())) {
|
||||
return metadataResultSet(Arrays.asList(
|
||||
Arrays.asList("PK__orders__3213E83F5A9632C8", "id"),
|
||||
Arrays.asList("PK__orders__3213E83F5A9632C8", "tenant_id"),
|
||||
Arrays.asList("IX_orders_name", "name")
|
||||
), Map.of("INDEX_NAME", 0, "COLUMN_NAME", 1));
|
||||
}
|
||||
if ("getPrimaryKeys".equals(method.getName())) {
|
||||
return metadataResultSet(Arrays.asList(
|
||||
Arrays.asList("PK__orders__3213E83F5A9632C8", "id"),
|
||||
Arrays.asList("PK__orders__3213E83F5A9632C8", "tenant_id")
|
||||
), Map.of("PK_NAME", 0, "COLUMN_NAME", 1));
|
||||
}
|
||||
return defaultValue(method.getReturnType());
|
||||
});
|
||||
TestSupport.setPrivateConnection(agent, proxy(Connection.class, (method, args) ->
|
||||
"getMetaData".equals(method.getName()) ? metadata : defaultValue(method.getReturnType())));
|
||||
|
||||
List<IndexInfo> indexes = agent.listIndexes("dbo", "orders");
|
||||
|
||||
List<IndexInfo> primary = indexes.stream().filter(IndexInfo::getIs_primary).toList();
|
||||
Assertions.assertEquals(1, primary.size(), indexes.toString());
|
||||
Assertions.assertEquals("PK__orders__3213E83F5A9632C8", primary.get(0).getName());
|
||||
Assertions.assertEquals(List.of("id", "tenant_id"), primary.get(0).getColumns());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user