fix(data-grid): support cloning rows without primary keys
This commit is contained in:
5 files changed
+125
-6
No files matched your search
@@ -146,6 +146,32 @@ describe("queryStore hidden primary key editing", () => {
|
||||
expect(tab.queryEditabilityReason).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps insert disabled when a MySQL table has a physical primary key named like DBX ROWID", async () => {
|
||||
getColumns.mockResolvedValue([
|
||||
{ name: "__DBX_ROWID", data_type: "varchar", is_nullable: false, column_default: null, is_primary_key: true, extra: null },
|
||||
{ name: "name", data_type: "varchar", is_nullable: true, column_default: null, is_primary_key: false, extra: null },
|
||||
]);
|
||||
analyzeEditableQueryEditability.mockImplementation(async (sql: string) => ({
|
||||
editable: true,
|
||||
analysis: {
|
||||
schema: undefined,
|
||||
tableName: "users",
|
||||
selectStar: false,
|
||||
columns: [{ sourceName: "name", resultName: "name", expression: "name" }, ...(sql.includes("__DBX_PK_0") ? [{ sourceName: "__DBX_ROWID", resultName: "__DBX_PK_0", expression: "`__DBX_ROWID`" }] : [])],
|
||||
},
|
||||
}));
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query");
|
||||
|
||||
await store.executeTabSql(tabId, "SELECT name FROM users");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
await vi.waitFor(() => expect(tab.querySourceColumns).toEqual(["name", "__DBX_ROWID"]));
|
||||
expect(tab.queryAnalysis?.allowInsert).toBe(false);
|
||||
});
|
||||
|
||||
it("uses the connection default database for SQL library tabs without a saved database", async () => {
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
@@ -266,7 +292,7 @@ describe("queryStore hidden primary key editing", () => {
|
||||
await vi.waitFor(() => expect(tab.querySourceColumns).toEqual(["ID", "PLATFORM", "__DBX_ROWID"]));
|
||||
expect(tab.tableMeta?.primaryKeys).toEqual(["__DBX_ROWID"]);
|
||||
expect(tab.queryAnalysis).toBeDefined();
|
||||
expect(tab.queryAnalysis?.allowInsert).toBe(false);
|
||||
expect(tab.queryAnalysis?.allowInsert).not.toBe(false);
|
||||
expect(tab.queryEditabilityReason).toBeUndefined();
|
||||
});
|
||||
|
||||
|
||||
@@ -72,6 +72,10 @@ const CANCEL_ACK_SETTLE_TIMEOUT_MS = 2_000;
|
||||
const SAVED_SQL_EDITOR_POSITION_PERSIST_DELAY_MS = 500;
|
||||
type CloseConfirmContext = "tab" | "batch" | "app";
|
||||
|
||||
function hasHiddenPhysicalRowKey(databaseType: DatabaseType | undefined, hiddenPrimaryKeys: HiddenPrimaryKeyProjection[]): boolean {
|
||||
return hiddenPrimaryKeys.some((projection) => !usesSyntheticRowIdKey(databaseType, [projection.sourceName]));
|
||||
}
|
||||
|
||||
function cloneTabDraft<T>(value: T): T {
|
||||
return JSON.parse(JSON.stringify(value)) as T;
|
||||
}
|
||||
@@ -2794,13 +2798,13 @@ export const useQueryStore = defineStore("query", () => {
|
||||
}
|
||||
}
|
||||
|
||||
function analyzeQueryMetadataInBackground(tabId: string, sql: string, result: QueryResult, executionDatabase: string, traceId: string, elapsed: () => string, hiddenPrimaryKeys: HiddenPrimaryKeyProjection[] = []) {
|
||||
function analyzeQueryMetadataInBackground(tabId: string, sql: string, result: QueryResult, executionDatabase: string, traceId: string, elapsed: () => string, databaseType: DatabaseType | undefined, hiddenPrimaryKeys: HiddenPrimaryKeyProjection[] = []) {
|
||||
void (async () => {
|
||||
const tab = tabs.value.find((t) => t.id === tabId);
|
||||
if (!tab || tab.result !== result) return;
|
||||
queryExecutionLog("info", "metadata:start", { traceId, elapsed: elapsed() });
|
||||
const patch = await buildQueryMetadataPatch(tab, sql, executionDatabase, traceId, elapsed, hiddenPrimaryKeys);
|
||||
if (patch?.queryAnalysis && hiddenPrimaryKeys.length > 0) {
|
||||
if (patch?.queryAnalysis && hasHiddenPhysicalRowKey(databaseType, hiddenPrimaryKeys)) {
|
||||
patch.queryAnalysis = { ...patch.queryAnalysis, allowInsert: false };
|
||||
}
|
||||
const current = tabs.value.find((t) => t.id === tabId);
|
||||
@@ -3637,7 +3641,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
elapsed: elapsed(),
|
||||
});
|
||||
if (current.mode === "query" && current.result) {
|
||||
analyzeQueryMetadataInBackground(id, displayedQueryMetadataSql(current, queryMetadataSql), current.result, executionDatabase, traceId, elapsed, hiddenPrimaryKeys);
|
||||
analyzeQueryMetadataInBackground(id, displayedQueryMetadataSql(current, queryMetadataSql), current.result, executionDatabase, traceId, elapsed, effectiveDbType, hiddenPrimaryKeys);
|
||||
}
|
||||
} else {
|
||||
queryExecutionLog("warn", "stale-result", {
|
||||
@@ -4088,7 +4092,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
const metadataStartedAt = performance.now();
|
||||
const connection = useConnectionStore().getConfig(tab.connectionId);
|
||||
const executionDatabase = dataTabExecutionDatabase(connection, tab.database, tab.catalog);
|
||||
analyzeQueryMetadataInBackground(id, sourceStatement, tab.result, executionDatabase, uuid().slice(0, 8), () => `${Math.round(performance.now() - metadataStartedAt)}ms`);
|
||||
analyzeQueryMetadataInBackground(id, sourceStatement, tab.result, executionDatabase, uuid().slice(0, 8), () => `${Math.round(performance.now() - metadataStartedAt)}ms`, effectiveDatabaseTypeForConnection(connection));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user