mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-07-29 17:59:47 +08:00
🐛 fix(table-designer): 修复 DuckDB 表设计主键保存失效
- 为 DuckDB 表结构变更补充 ADD PRIMARY KEY 预览 SQL - 保存前拦截已有主键表的主键替换与删除,避免假成功 - 补充 DuckDB 主键变更判定与 schema SQL 回归测试
This commit is contained in:
@@ -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');
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user