🐛 fix(table-designer): 修复 DuckDB 表设计主键保存失效

- 为 DuckDB 表结构变更补充 ADD PRIMARY KEY 预览 SQL
- 保存前拦截已有主键表的主键替换与删除,避免假成功
- 补充 DuckDB 主键变更判定与 schema SQL 回归测试
This commit is contained in:
Syngnat
2026-06-05 11:25:44 +08:00
parent 6742495c6f
commit 805ab8b3d8
5 changed files with 148 additions and 0 deletions

View File

@@ -79,6 +79,12 @@ export interface BuildStarRocksMaterializedViewPreviewInput {
properties?: string;
}
const collectPrimaryKeyColumnKeys = (columns: EditableColumnSnapshot[]): string[] => (
columns
.filter((col) => col.key === 'PRI')
.map((col) => col._key)
);
const escapeSqlString = (value: string) => String(value || '').replace(/'/g, "''");
const stripIdentifierQuotes = unquoteSqlIdentifierPart;
@@ -607,6 +613,21 @@ const buildDuckDbAlterPreviewSql = (input: BuildAlterTablePreviewInput): string
}
});
const origPKKeys = collectPrimaryKeyColumnKeys(input.originalColumns);
const newPKKeys = collectPrimaryKeyColumnKeys(input.columns);
const keysChanged = origPKKeys.length !== newPKKeys.length || !origPKKeys.every((key) => newPKKeys.includes(key));
if (keysChanged) {
if (origPKKeys.length === 0 && newPKKeys.length > 0) {
const pkNames = input.columns
.filter((col) => col.key === 'PRI')
.map((col) => quoteIdentifierPart(col.name, dbType))
.join(', ');
statements.push(`ALTER TABLE ${tableRef}\nADD PRIMARY KEY (${pkNames});`);
} else {
statements.push('-- DuckDB 当前仅支持为无主键表新增 PRIMARY KEY已有主键的修改或删除需要通过重建表完成。');
}
}
return statements.join('\n');
};