fix(mysql): refresh stale snapshots and streamline table reloads
This commit is contained in:
11 files changed
+911
-54
No files matched your search
+134
-14
@@ -3,7 +3,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
|
|||||||
import { useDataGridActions } from "@/composables/useDataGridActions";
|
import { useDataGridActions } from "@/composables/useDataGridActions";
|
||||||
import { clearTableMetadataCache } from "@/lib/metadata/tableMetadataCache";
|
import { clearTableMetadataCache } from "@/lib/metadata/tableMetadataCache";
|
||||||
import { restoredDataTabReloadFilters } from "@/lib/table/tableDataRefresh";
|
import { restoredDataTabReloadFilters } from "@/lib/table/tableDataRefresh";
|
||||||
import type { QueryTab } from "@/types/database";
|
import type { IndexInfo, QueryTab } from "@/types/database";
|
||||||
|
|
||||||
const mocks = vi.hoisted(() => ({
|
const mocks = vi.hoisted(() => ({
|
||||||
buildTableSelectSql: vi.fn(),
|
buildTableSelectSql: vi.fn(),
|
||||||
@@ -79,14 +79,15 @@ vi.mock("@/stores/queryStore", () => ({
|
|||||||
if (staleOrder) tab.orderByInput = undefined;
|
if (staleOrder) tab.orderByInput = undefined;
|
||||||
return true;
|
return true;
|
||||||
}),
|
}),
|
||||||
setTableMeta: mocks.setTableMeta.mockImplementation((id: string, meta: NonNullable<QueryTab["tableMeta"]>) => {
|
setTableMeta: mocks.setTableMeta.mockImplementation((id: string, meta: NonNullable<QueryTab["tableMeta"]>, options: { rowIdentityPending?: boolean } = {}) => {
|
||||||
const tab = mocks.tabs.find((item) => item.id === id);
|
const tab = mocks.tabs.find((item) => item.id === id);
|
||||||
if (tab) {
|
if (tab) {
|
||||||
tab.tableMeta = meta;
|
tab.tableMeta = meta;
|
||||||
tab.tableMetaGeneration = mocks.metadataGeneration;
|
tab.tableMetaGeneration = mocks.metadataGeneration;
|
||||||
tab.tableMetaUpdatedAt = Date.now();
|
tab.tableMetaUpdatedAt = Date.now();
|
||||||
// 与真实 store 一致:仅真实元数据(columns 非空)落地才结束行标识等待
|
// 与真实 store 一致:仅真实元数据(columns 非空)落地才结束行标识等待
|
||||||
if (meta.columns.length > 0) tab.tableMetaPending = false;
|
if (options.rowIdentityPending) tab.tableMetaPending = true;
|
||||||
|
else if (meta.columns.length > 0) tab.tableMetaPending = false;
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
@@ -155,7 +156,7 @@ describe("useDataGridActions", () => {
|
|||||||
mocks.listIndexes.mockResolvedValue([]);
|
mocks.listIndexes.mockResolvedValue([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("manual refresh waits for cache deletion and fresh columns before building SQL", async () => {
|
it("manual refresh starts cache deletion without waiting and builds SQL from fresh columns", async () => {
|
||||||
const tab = tableDataTab();
|
const tab = tableDataTab();
|
||||||
mocks.tabs.push(tab);
|
mocks.tabs.push(tab);
|
||||||
let release!: () => void;
|
let release!: () => void;
|
||||||
@@ -168,26 +169,145 @@ describe("useDataGridActions", () => {
|
|||||||
const actions = useDataGridActions(computed(() => tab));
|
const actions = useDataGridActions(computed(() => tab));
|
||||||
const reload = actions.onReloadData(tab.id, tab.sql, "", "", "", 25, 50, "refresh");
|
const reload = actions.onReloadData(tab.id, tab.sql, "", "", "", 25, 50, "refresh");
|
||||||
await vi.waitFor(() => expect(mocks.deleteSchemaCachePrefix).toHaveBeenCalledWith("object-meta:v1:postgres-1:app:public:users:"));
|
await vi.waitFor(() => expect(mocks.deleteSchemaCachePrefix).toHaveBeenCalledWith("object-meta:v1:postgres-1:app:public:users:"));
|
||||||
expect(mocks.getColumns).not.toHaveBeenCalled();
|
// 持久缓存删除不再挡在元数据/查询前:列加载可以立即开始
|
||||||
expect(mocks.buildTableSelectSql).not.toHaveBeenCalled();
|
await vi.waitFor(() => expect(mocks.getColumns).toHaveBeenCalledTimes(1));
|
||||||
release();
|
release();
|
||||||
await reload;
|
await reload;
|
||||||
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ columns: ["id", "added"], limit: 25, offset: 50 }));
|
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(expect.objectContaining({ columns: ["id", "added"], limit: 25, offset: 50 }));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("preserves rows when cache deletion fails and allows retry", async () => {
|
it("manual refresh keeps index discovery off the awaited path", async () => {
|
||||||
|
const tab = tableDataTab();
|
||||||
|
mocks.tabs.push(tab);
|
||||||
|
let releaseIndexes!: (indexes: unknown[]) => void;
|
||||||
|
mocks.listIndexes.mockReturnValueOnce(
|
||||||
|
new Promise((resolve) => {
|
||||||
|
releaseIndexes = resolve;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const actions = useDataGridActions(computed(() => tab));
|
||||||
|
const reload = actions.onReloadData(tab.id, tab.sql, "", "", "", 100, 0, "refresh");
|
||||||
|
await reload;
|
||||||
|
// 等待段只拉列:SQL 已构建执行,listIndexes 仍未结算
|
||||||
|
expect(mocks.executeTabSql).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mocks.listIndexes).toHaveBeenCalledTimes(1);
|
||||||
|
releaseIndexes([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("manual refresh derives primary keys from fresh columns", async () => {
|
||||||
|
const tab = tableDataTab();
|
||||||
|
mocks.tabs.push(tab);
|
||||||
|
// 表结构变化:旧主键 id 被删,新主键 id2
|
||||||
|
mocks.getColumns.mockResolvedValue([{ name: "id2", data_type: "bigint", is_nullable: false, column_default: null, is_primary_key: true, extra: null }]);
|
||||||
|
const actions = useDataGridActions(computed(() => tab));
|
||||||
|
await actions.onReloadData(tab.id, tab.sql, "", "", "", 100, 0, "refresh");
|
||||||
|
// 旧 PK 不在新列中:等待段不得把失效 PK 写回 tab 元数据
|
||||||
|
expect(tab.tableMeta?.primaryKeys).toEqual(["id2"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([true, false])("keeps a surviving old key read-only until index discovery settles (success=%s)", async (success) => {
|
||||||
|
const tab = reactive(tableDataTab());
|
||||||
|
mocks.tabs.push(tab);
|
||||||
|
mocks.getColumns.mockResolvedValue([{ ...tab.tableMeta!.columns[0], is_primary_key: false }]);
|
||||||
|
let resolveIndexes!: (indexes: IndexInfo[]) => void;
|
||||||
|
let rejectIndexes!: (error: Error) => void;
|
||||||
|
mocks.listIndexes.mockReturnValueOnce(
|
||||||
|
new Promise<IndexInfo[]>((resolve, reject) => {
|
||||||
|
resolveIndexes = resolve;
|
||||||
|
rejectIndexes = reject;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const actions = useDataGridActions(computed(() => tab));
|
||||||
|
|
||||||
|
await actions.onReloadData(tab.id, tab.sql, "", "", "", 100, 0, "refresh");
|
||||||
|
|
||||||
|
expect(mocks.executeTabSql).toHaveBeenCalledOnce();
|
||||||
|
expect(tab.tableMetaPending).toBe(true);
|
||||||
|
expect(tab.tableMeta?.primaryKeys).toEqual([]);
|
||||||
|
if (success) {
|
||||||
|
resolveIndexes([]);
|
||||||
|
await vi.waitFor(() => expect(tab.tableMetaPending).toBe(false));
|
||||||
|
} else {
|
||||||
|
rejectIndexes(new Error("index discovery failed"));
|
||||||
|
await vi.waitFor(() => expect(mocks.setTableMeta).toHaveBeenCalledTimes(2));
|
||||||
|
expect(tab.tableMetaPending).toBe(true);
|
||||||
|
await actions.onReloadData(tab.id, tab.sql, "", "", "", 100, 0, "auto-refresh");
|
||||||
|
await vi.waitFor(() => expect(tab.tableMetaPending).toBe(false));
|
||||||
|
expect(mocks.listIndexes).toHaveBeenCalledTimes(2);
|
||||||
|
}
|
||||||
|
expect(tab.tableMeta?.primaryKeys).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses a freshly discovered unique index only after discovery finishes", async () => {
|
||||||
|
const tab = tableDataTab();
|
||||||
|
mocks.tabs.push(tab);
|
||||||
|
mocks.getColumns.mockResolvedValue([{ ...tab.tableMeta!.columns[0], is_primary_key: false }]);
|
||||||
|
let resolveIndexes!: (indexes: IndexInfo[]) => void;
|
||||||
|
mocks.listIndexes.mockReturnValueOnce(
|
||||||
|
new Promise<IndexInfo[]>((resolve) => {
|
||||||
|
resolveIndexes = resolve;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const actions = useDataGridActions(computed(() => tab));
|
||||||
|
|
||||||
|
await actions.onReloadData(tab.id, tab.sql, "", "", "", 100, 0, "refresh");
|
||||||
|
expect(tab.tableMetaPending).toBe(true);
|
||||||
|
expect(tab.tableMeta?.primaryKeys).toEqual([]);
|
||||||
|
resolveIndexes([{ name: "users_id_unique", columns: ["id"], is_unique: true, is_primary: false }]);
|
||||||
|
await vi.waitFor(() => expect(tab.tableMetaPending).toBe(false));
|
||||||
|
expect(tab.tableMeta?.primaryKeys).toEqual(["id"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(["TABLE", "VIEW", "MATERIALIZED_VIEW"])("preserves Oracle synthetic projection only for eligible tables (%s)", async (tableType) => {
|
||||||
|
mocks.getConfig.mockReturnValue({ id: "oracle-1", db_type: "oracle" });
|
||||||
|
const tab = tableDataTab({ connectionId: "oracle-1" });
|
||||||
|
tab.tableMeta!.tableType = tableType;
|
||||||
|
tab.tableMeta!.columns[0].is_primary_key = false;
|
||||||
|
tab.tableMeta!.primaryKeys = tableType === "TABLE" ? ["__DBX_ROWID"] : [];
|
||||||
|
mocks.tabs.push(tab);
|
||||||
|
mocks.getColumns.mockResolvedValue(tab.tableMeta!.columns);
|
||||||
|
let resolveIndexes!: (indexes: IndexInfo[]) => void;
|
||||||
|
mocks.listIndexes.mockReturnValueOnce(
|
||||||
|
new Promise<IndexInfo[]>((resolve) => {
|
||||||
|
resolveIndexes = resolve;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const actions = useDataGridActions(computed(() => tab));
|
||||||
|
|
||||||
|
await actions.onReloadData(tab.id, tab.sql, "", "", "", 100, 0, "refresh");
|
||||||
|
|
||||||
|
expect(mocks.executeTabSql).toHaveBeenCalledOnce();
|
||||||
|
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
includeRowId: tableType === "TABLE",
|
||||||
|
primaryKeys: tableType === "TABLE" ? ["__DBX_ROWID"] : [],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(tab.tableMetaPending).toBe(true);
|
||||||
|
resolveIndexes([]);
|
||||||
|
await vi.waitFor(() => expect(tab.tableMetaPending).toBe(false));
|
||||||
|
expect(tab.tableMeta?.primaryKeys).toEqual(tableType === "TABLE" ? ["__DBX_ROWID"] : []);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps old metadata read-only when fresh columns fail", async () => {
|
||||||
|
const tab = tableDataTab();
|
||||||
|
mocks.tabs.push(tab);
|
||||||
|
mocks.getColumns.mockRejectedValueOnce(new Error("columns failed"));
|
||||||
|
const actions = useDataGridActions(computed(() => tab));
|
||||||
|
|
||||||
|
await expect(actions.onReloadData(tab.id, tab.sql, "", "", "", 100, 0, "refresh")).rejects.toThrow("columns failed");
|
||||||
|
expect(tab.tableMetaPending).toBe(true);
|
||||||
|
expect(mocks.executeTabSql).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves rows when cache deletion fails and still executes", async () => {
|
||||||
const tab = tableDataTab();
|
const tab = tableDataTab();
|
||||||
mocks.tabs.push(tab);
|
mocks.tabs.push(tab);
|
||||||
const result = tab.result;
|
const result = tab.result;
|
||||||
const failure = new Error("cache deletion failed");
|
mocks.deleteSchemaCachePrefix.mockRejectedValueOnce(new Error("cache deletion failed"));
|
||||||
mocks.deleteSchemaCachePrefix.mockRejectedValueOnce(failure);
|
|
||||||
const actions = useDataGridActions(computed(() => tab));
|
const actions = useDataGridActions(computed(() => tab));
|
||||||
await expect(actions.onReloadData(tab.id, tab.sql, "", "", "", 100, 0, "refresh")).rejects.toMatchObject({ name: "ObjectCacheInvalidationError", reason: failure });
|
|
||||||
expect(tab.result).toBe(result);
|
|
||||||
expect(mocks.getColumns).not.toHaveBeenCalled();
|
|
||||||
expect(mocks.executeTabSql).not.toHaveBeenCalled();
|
|
||||||
expect(mocks.setExecuting).toHaveBeenLastCalledWith(tab.id, false);
|
|
||||||
await actions.onReloadData(tab.id, tab.sql, "", "", "", 100, 0, "refresh");
|
await actions.onReloadData(tab.id, tab.sql, "", "", "", 100, 0, "refresh");
|
||||||
|
expect(tab.result).toBe(result);
|
||||||
expect(mocks.executeTabSql).toHaveBeenCalledTimes(1);
|
expect(mocks.executeTabSql).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ function createEditorWithResult(
|
|||||||
tableColumns?: Array<{ name: string; data_type: string; extra?: string; column_default?: string }>,
|
tableColumns?: Array<{ name: string; data_type: string; extra?: string; column_default?: string }>,
|
||||||
mongoCollectionGrid = false,
|
mongoCollectionGrid = false,
|
||||||
quickEntry = false,
|
quickEntry = false,
|
||||||
|
editable = ref(true),
|
||||||
) {
|
) {
|
||||||
let editor: ReturnType<typeof useDataGridEditor>;
|
let editor: ReturnType<typeof useDataGridEditor>;
|
||||||
const result = ref<{ columns: string[]; rows: CellValue[][] }>({
|
const result = ref<{ columns: string[]; rows: CellValue[][] }>({
|
||||||
@@ -59,7 +60,7 @@ function createEditorWithResult(
|
|||||||
|
|
||||||
editor = useDataGridEditor({
|
editor = useDataGridEditor({
|
||||||
result: computed(() => result.value),
|
result: computed(() => result.value),
|
||||||
editable: computed(() => true),
|
editable: computed(() => editable.value),
|
||||||
databaseType: computed(() => (mongoCollectionGrid ? "mongodb" : "postgres")),
|
databaseType: computed(() => (mongoCollectionGrid ? "mongodb" : "postgres")),
|
||||||
normalizeEditorInput: mongoCollectionGrid ? mongoDocumentGridInputValue : undefined,
|
normalizeEditorInput: mongoCollectionGrid ? mongoDocumentGridInputValue : undefined,
|
||||||
connectionId: computed(() => "connection-1"),
|
connectionId: computed(() => "connection-1"),
|
||||||
@@ -145,6 +146,46 @@ describe("useDataGridEditor searched replacements", () => {
|
|||||||
mocks.getConfig.mockReturnValue(undefined);
|
mocks.getConfig.mockReturnValue(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each([true, false])("blocks queued saves while row identity is pending (autoSave=%s)", async (autoSave) => {
|
||||||
|
const editable = ref(true);
|
||||||
|
const editor = createEditor(undefined, true, undefined, undefined, [["old", "keep", "last"]], undefined, undefined, false, false, editable);
|
||||||
|
editor.newRows.value = [];
|
||||||
|
editor.applyCellValue(0, 0, "new");
|
||||||
|
editable.value = false;
|
||||||
|
|
||||||
|
await editor.saveChanges({ autoSave });
|
||||||
|
|
||||||
|
expect(mocks.prepareDataGridSave).not.toHaveBeenCalled();
|
||||||
|
expect(mocks.executeBatch).not.toHaveBeenCalled();
|
||||||
|
expect(editor.hasPendingChanges.value).toBe(true);
|
||||||
|
editable.value = true;
|
||||||
|
mocks.prepareDataGridSave.mockResolvedValue({ statements: ["UPDATE people SET first='new'"], rollbackStatements: [] });
|
||||||
|
mocks.executeBatch.mockResolvedValue([]);
|
||||||
|
await editor.saveChanges();
|
||||||
|
expect(mocks.executeBatch).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rechecks row identity readiness after asynchronous save preparation", async () => {
|
||||||
|
const editable = ref(true);
|
||||||
|
const editor = createEditor(undefined, true, undefined, undefined, [["old", "keep", "last"]], undefined, undefined, false, false, editable);
|
||||||
|
editor.newRows.value = [];
|
||||||
|
editor.applyCellValue(0, 0, "new");
|
||||||
|
let finishPreparation!: (value: { statements: string[]; rollbackStatements: string[] }) => void;
|
||||||
|
mocks.prepareDataGridSave.mockReturnValueOnce(
|
||||||
|
new Promise((resolve) => {
|
||||||
|
finishPreparation = resolve;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const save = editor.saveChanges();
|
||||||
|
expect(mocks.prepareDataGridSave).toHaveBeenCalledOnce();
|
||||||
|
editable.value = false;
|
||||||
|
finishPreparation({ statements: ["UPDATE people SET first='new'"], rollbackStatements: [] });
|
||||||
|
await save;
|
||||||
|
expect(mocks.executeBatch).not.toHaveBeenCalled();
|
||||||
|
expect(editor.hasPendingChanges.value).toBe(true);
|
||||||
|
expect(editor.isSaving.value).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
it("stages one undoable batch, keeps empty strings and preserves prior pending edits", () => {
|
it("stages one undoable batch, keeps empty strings and preserves prior pending edits", () => {
|
||||||
const editor = createEditor(undefined, true, undefined, undefined, [["hit", "keep", "hit"]]);
|
const editor = createEditor(undefined, true, undefined, undefined, [["hit", "keep", "hit"]]);
|
||||||
editor.newRows.value = [];
|
editor.newRows.value = [];
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ import { buildTableSelectSql, quoteTableDataIdentifier } from "@/lib/table/table
|
|||||||
import { tableOpenPageLimit } from "@/lib/table/tableOpenPageLimit";
|
import { tableOpenPageLimit } from "@/lib/table/tableOpenPageLimit";
|
||||||
import { tableDataLargeValuePreviewOptions } from "@/lib/dataGrid/dataGridLargeValues";
|
import { tableDataLargeValuePreviewOptions } from "@/lib/dataGrid/dataGridLargeValues";
|
||||||
import { elasticsearchCursorPageJumpRequestCount } from "@/lib/dataGrid/dataGridPagination";
|
import { elasticsearchCursorPageJumpRequestCount } from "@/lib/dataGrid/dataGridPagination";
|
||||||
import { shouldIncludeSyntheticRowId } from "@/lib/table/tableEditing";
|
import { editablePrimaryKeys, shouldIncludeSyntheticRowId } from "@/lib/table/tableEditing";
|
||||||
import { tableMetaForDataTab } from "@/lib/table/tableDataTabMeta";
|
import { tableMetaForDataTab } from "@/lib/table/tableDataTabMeta";
|
||||||
import * as api from "@/lib/backend/api";
|
import * as api from "@/lib/backend/api";
|
||||||
import type { QueryTab } from "@/types/database";
|
import type { ColumnInfo, QueryTab } from "@/types/database";
|
||||||
import { useToast } from "@/composables/useToast";
|
import { useToast } from "@/composables/useToast";
|
||||||
import { effectiveDatabaseTypeForConnection, metadataSchemaForConnection } from "@/lib/database/jdbcDialect";
|
import { effectiveDatabaseTypeForConnection, metadataSchemaForConnection } from "@/lib/database/jdbcDialect";
|
||||||
import { invalidateTableMetadataCache, loadTableMetadata, TABLE_METADATA_CACHE_TTL_MS } from "@/lib/metadata/tableMetadataCache";
|
import { invalidateTableMetadataCache, loadTableColumns, loadTableMetadata, TABLE_METADATA_CACHE_TTL_MS } from "@/lib/metadata/tableMetadataCache";
|
||||||
import { isDataTabMetadataLifecycleStale } from "@/lib/sidebar/dataTabOpenPolicy";
|
import { isDataTabMetadataLifecycleStale } from "@/lib/sidebar/dataTabOpenPolicy";
|
||||||
import { applyMongoFindSort } from "@/lib/mongo/mongoShellCommand";
|
import { applyMongoFindSort } from "@/lib/mongo/mongoShellCommand";
|
||||||
import { uuid } from "@/lib/common/utils";
|
import { uuid } from "@/lib/common/utils";
|
||||||
@@ -26,6 +26,8 @@ import { sqlExecutionTargetCapabilities } from "@/lib/database/sqlExecutionTarge
|
|||||||
|
|
||||||
const DATA_TAB_METADATA_TTL_MS = TABLE_METADATA_CACHE_TTL_MS;
|
const DATA_TAB_METADATA_TTL_MS = TABLE_METADATA_CACHE_TTL_MS;
|
||||||
|
|
||||||
|
type TableMetadataColumns = ColumnInfo[];
|
||||||
|
|
||||||
function visibleQuerySortColumns(columns: string[], hiddenColumnIndexes: number[] | undefined, columnIndex: number): { resultColumns: string[]; columnIndex: number } | undefined {
|
function visibleQuerySortColumns(columns: string[], hiddenColumnIndexes: number[] | undefined, columnIndex: number): { resultColumns: string[]; columnIndex: number } | undefined {
|
||||||
const hiddenIndexes = new Set(hiddenColumnIndexes ?? []);
|
const hiddenIndexes = new Set(hiddenColumnIndexes ?? []);
|
||||||
const resultColumns: string[] = [];
|
const resultColumns: string[] = [];
|
||||||
@@ -136,7 +138,7 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function refreshDataTabTableMeta(tab: QueryTab, options: { force?: boolean; trace?: { traceId: string; elapsed: () => string } } = {}): Promise<boolean> {
|
async function refreshDataTabTableMeta(tab: QueryTab, options: { force?: boolean; columnsOnly?: boolean; trace?: { traceId: string; elapsed: () => string } } = {}): Promise<boolean> {
|
||||||
if (tab.mode !== "data" || !tab.connectionId || !tab.database) return false;
|
if (tab.mode !== "data" || !tab.connectionId || !tab.database) return false;
|
||||||
const tableMeta = tableMetaForDataTab(tab);
|
const tableMeta = tableMetaForDataTab(tab);
|
||||||
if (!tableMeta?.tableName) return false;
|
if (!tableMeta?.tableName) return false;
|
||||||
@@ -153,6 +155,7 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
|
|||||||
// still receive its result. Only the latest request may update this tab.
|
// still receive its result. Only the latest request may update this tab.
|
||||||
const requestToken = {};
|
const requestToken = {};
|
||||||
metadataRequests.set(tab, requestToken);
|
metadataRequests.set(tab, requestToken);
|
||||||
|
tab.tableMetaPending = true;
|
||||||
const metadataGenerationAtStart = connectionStore.metadataGenerationFor(target.connectionId, target.database);
|
const metadataGenerationAtStart = connectionStore.metadataGenerationFor(target.connectionId, target.database);
|
||||||
const trace = options.trace;
|
const trace = options.trace;
|
||||||
|
|
||||||
@@ -165,22 +168,41 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
|
|||||||
}
|
}
|
||||||
const config = connectionStore.getConfig(target.connectionId);
|
const config = connectionStore.getConfig(target.connectionId);
|
||||||
const querySchema = metadataSchemaForConnection(config, target.database, target.schema);
|
const querySchema = metadataSchemaForConnection(config, target.database, target.schema);
|
||||||
console.info("[DBX][reloadData:metadata:get-columns:start]", { traceId: trace?.traceId, elapsed: trace?.elapsed(), schema: querySchema, table: target.tableName });
|
console.info("[DBX][reloadData:metadata:get-columns:start]", { traceId: trace?.traceId, elapsed: trace?.elapsed(), schema: querySchema, table: target.tableName, columnsOnly: options.columnsOnly === true });
|
||||||
// 复用共享表元数据缓存(30s TTL + in-flight 去重),多个入口对同一张表
|
// 复用共享表元数据缓存(30s TTL + in-flight 去重),多个入口对同一张表
|
||||||
// 不再各自往返 getColumns/listIndexes。跨连接生命周期的强制重建走 force,
|
// 不再各自往返 getColumns/listIndexes。跨连接生命周期的强制重建走 force,
|
||||||
// 避免同一共享缓存把断链前的旧列再次交回本次 reload。
|
// 避免同一共享缓存把断链前的旧列再次交回本次 reload。
|
||||||
const { metadata } = await loadTableMetadata({
|
const loaded: { columns: TableMetadataColumns; primaryKeys: string[]; rowIdentityResolved: boolean } = options.columnsOnly
|
||||||
connectionId: target.connectionId,
|
? await (async () => {
|
||||||
database: target.database,
|
const { columns } = await loadTableColumns({
|
||||||
schema: querySchema,
|
connectionId: target.connectionId,
|
||||||
tableName: target.tableName,
|
database: target.database,
|
||||||
tableType: target.tableType,
|
schema: querySchema,
|
||||||
databaseType: effectiveDatabaseTypeForConnection(config) ?? config?.db_type ?? "",
|
tableName: target.tableName,
|
||||||
driverProfile: config?.driver_profile || config?.db_type,
|
tableType: target.tableType,
|
||||||
catalog: target.catalog,
|
databaseType: effectiveDatabaseTypeForConnection(config) ?? config?.db_type ?? "",
|
||||||
force: options.force === true,
|
driverProfile: config?.driver_profile || config?.db_type,
|
||||||
});
|
catalog: target.catalog,
|
||||||
const columns = metadata.columns;
|
force: options.force === true,
|
||||||
|
});
|
||||||
|
const primaryKeys = editablePrimaryKeys(effectiveDatabaseTypeForConnection(config), columns, target.tableType);
|
||||||
|
return { columns, primaryKeys, rowIdentityResolved: false };
|
||||||
|
})()
|
||||||
|
: await (async () => {
|
||||||
|
const { metadata } = await loadTableMetadata({
|
||||||
|
connectionId: target.connectionId,
|
||||||
|
database: target.database,
|
||||||
|
schema: querySchema,
|
||||||
|
tableName: target.tableName,
|
||||||
|
tableType: target.tableType,
|
||||||
|
databaseType: effectiveDatabaseTypeForConnection(config) ?? config?.db_type ?? "",
|
||||||
|
driverProfile: config?.driver_profile || config?.db_type,
|
||||||
|
catalog: target.catalog,
|
||||||
|
force: options.force === true,
|
||||||
|
});
|
||||||
|
return { columns: metadata.columns, primaryKeys: metadata.primaryKeys, rowIdentityResolved: metadata.rowIdentityResolved !== false };
|
||||||
|
})();
|
||||||
|
const columns = loaded.columns;
|
||||||
console.info("[DBX][reloadData:metadata:get-columns:done]", { traceId: trace?.traceId, elapsed: trace?.elapsed(), columnCount: columns.length });
|
console.info("[DBX][reloadData:metadata:get-columns:done]", { traceId: trace?.traceId, elapsed: trace?.elapsed(), columnCount: columns.length });
|
||||||
if (metadataRequests.get(tab) !== requestToken || connectionStore.metadataGenerationFor(target.connectionId, target.database) !== metadataGenerationAtStart) {
|
if (metadataRequests.get(tab) !== requestToken || connectionStore.metadataGenerationFor(target.connectionId, target.database) !== metadataGenerationAtStart) {
|
||||||
console.info("[DBX][reloadData:metadata:superseded-by-connection-generation]", { traceId: trace?.traceId, elapsed: trace?.elapsed(), table: target.tableName });
|
console.info("[DBX][reloadData:metadata:superseded-by-connection-generation]", { traceId: trace?.traceId, elapsed: trace?.elapsed(), table: target.tableName });
|
||||||
@@ -202,8 +224,8 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
|
|||||||
console.info("[DBX][reloadData:metadata:stale-tab]", { traceId: trace?.traceId, elapsed: trace?.elapsed(), table: target.tableName });
|
console.info("[DBX][reloadData:metadata:stale-tab]", { traceId: trace?.traceId, elapsed: trace?.elapsed(), table: target.tableName });
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const primaryKeys = metadata.primaryKeys;
|
const primaryKeys = loaded.primaryKeys;
|
||||||
queryStore.setTableMeta(target.tabId, {
|
const refreshedMeta = {
|
||||||
catalog: target.catalog,
|
catalog: target.catalog,
|
||||||
database: target.database,
|
database: target.database,
|
||||||
schema: target.schema,
|
schema: target.schema,
|
||||||
@@ -211,7 +233,12 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
|
|||||||
tableType: target.tableType,
|
tableType: target.tableType,
|
||||||
columns,
|
columns,
|
||||||
primaryKeys,
|
primaryKeys,
|
||||||
});
|
};
|
||||||
|
if (loaded.rowIdentityResolved) {
|
||||||
|
queryStore.setTableMeta(target.tabId, refreshedMeta);
|
||||||
|
} else {
|
||||||
|
queryStore.setTableMeta(target.tabId, refreshedMeta, { rowIdentityPending: true });
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,7 +320,7 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
|
|||||||
}
|
}
|
||||||
const connectionGeneration = connectionStore.metadataGenerationFor(tab.connectionId, tab.database);
|
const connectionGeneration = connectionStore.metadataGenerationFor(tab.connectionId, tab.database);
|
||||||
const lifecycleStale = isDataTabMetadataLifecycleStale(tab, connectionGeneration);
|
const lifecycleStale = isDataTabMetadataLifecycleStale(tab, connectionGeneration);
|
||||||
const shouldRefreshMetadata = lifecycleStale || !hasRealTableMetaColumns || metadataAgeMs > DATA_TAB_METADATA_TTL_MS;
|
const shouldRefreshMetadata = lifecycleStale || tab.tableMetaPending || !hasRealTableMetaColumns || metadataAgeMs > DATA_TAB_METADATA_TTL_MS;
|
||||||
// Dameng 元数据必须与数据查询串行(同 useSidebarDataOpenRuntime),
|
// Dameng 元数据必须与数据查询串行(同 useSidebarDataOpenRuntime),
|
||||||
// 延后到查询完成后再启动。主动刷新和跨生命周期重建除外:必须先拿到新列再
|
// 延后到查询完成后再启动。主动刷新和跨生命周期重建除外:必须先拿到新列再
|
||||||
// 构建 SQL,否则第一次 toolbar reload 仍会沿用断链前的显式列列表。
|
// 构建 SQL,否则第一次 toolbar reload 仍会沿用断链前的显式列列表。
|
||||||
@@ -310,7 +337,7 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
if (lifecycleStale || intent === "refresh") {
|
if (lifecycleStale || intent === "refresh") {
|
||||||
if (!hasRealTableMetaColumns) tab.tableMetaPending = true;
|
tab.tableMetaPending = true;
|
||||||
console.info("[DBX][reloadData:metadata:await:start]", { traceId, elapsed: elapsed(), reason: intent === "refresh" ? "manual-refresh" : "lifecycle-stale", metadataAgeMs });
|
console.info("[DBX][reloadData:metadata:await:start]", { traceId, elapsed: elapsed(), reason: intent === "refresh" ? "manual-refresh" : "lifecycle-stale", metadataAgeMs });
|
||||||
try {
|
try {
|
||||||
if (intent === "refresh") {
|
if (intent === "refresh") {
|
||||||
@@ -323,13 +350,22 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
|
|||||||
tableName: meta.tableName,
|
tableName: meta.tableName,
|
||||||
};
|
};
|
||||||
invalidateTableMetadataCache(match);
|
invalidateTableMetadataCache(match);
|
||||||
await invalidateObjectMetadataCache(match, { strict: true });
|
// 持久缓存(object-meta L2)删除只服务后续结构面板/DDL 读取的
|
||||||
|
// 新鲜度,不参与本次查询构建:移出关键路径 fire-and-forget,
|
||||||
|
// 失败不再中断刷新(网格侧已由上面的内存失效保证列新鲜)。
|
||||||
|
console.info("[DBX][reloadData:object-cache-invalidate:start]", { traceId, elapsed: elapsed() });
|
||||||
|
invalidateObjectMetadataCache(match).catch((e: unknown) => {
|
||||||
|
console.warn("[DBX][reloadData:object-cache-invalidate:error]", { traceId, elapsed: elapsed(), error: e });
|
||||||
|
});
|
||||||
if (!stillCurrent() || connectionStore.metadataGenerationFor(tab.connectionId, tab.database) !== connectionGeneration) {
|
if (!stillCurrent() || connectionStore.metadataGenerationFor(tab.connectionId, tab.database) !== connectionGeneration) {
|
||||||
stopPreparing();
|
stopPreparing();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const rebuilt = await refreshDataTabTableMeta(tab, { force: true, trace: { traceId, elapsed } });
|
// 手动刷新等待段只拉列(getColumns),不等 listIndexes:列投影决定
|
||||||
|
// 本次 SELECT 的正确性;主键/索引用旧值与新列求交,PK 名不进 SQL
|
||||||
|
// 文本(仅作大值预览保护集合),查询本身不受索引元数据延迟影响。
|
||||||
|
const rebuilt = await refreshDataTabTableMeta(tab, { force: true, columnsOnly: intent === "refresh" && !lifecycleStale && hasRealTableMetaColumns, trace: { traceId, elapsed } });
|
||||||
console.info("[DBX][reloadData:metadata:await:done]", { traceId, elapsed: elapsed(), rebuilt });
|
console.info("[DBX][reloadData:metadata:await:done]", { traceId, elapsed: elapsed(), rebuilt });
|
||||||
if (!rebuilt) {
|
if (!rebuilt) {
|
||||||
stopPreparing();
|
stopPreparing();
|
||||||
@@ -345,6 +381,15 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
|
|||||||
const rebuiltColumnNames = tab.tableMeta?.columns.map((column) => column.name) ?? [];
|
const rebuiltColumnNames = tab.tableMeta?.columns.map((column) => column.name) ?? [];
|
||||||
incomingSortMissing = rebuiltColumnNames.length > 0 && simpleDataGridOrderByReferencesMissingColumn(orderBy, rebuiltColumnNames);
|
incomingSortMissing = rebuiltColumnNames.length > 0 && simpleDataGridOrderByReferencesMissingColumn(orderBy, rebuiltColumnNames);
|
||||||
if (incomingSortMissing) tab.orderByInput = undefined;
|
if (incomingSortMissing) tab.orderByInput = undefined;
|
||||||
|
if (intent === "refresh" && !lifecycleStale) {
|
||||||
|
void refreshDataTabTableMeta(tab, { force: false, trace: { traceId, elapsed } })
|
||||||
|
.then(() => {
|
||||||
|
console.info("[DBX][reloadData:metadata:background-indexes:done]", { traceId, elapsed: elapsed() });
|
||||||
|
})
|
||||||
|
.catch((e: any) => {
|
||||||
|
console.warn("[DBX][reloadData:metadata:background-indexes:error]", { traceId, elapsed: elapsed(), error: e });
|
||||||
|
});
|
||||||
|
}
|
||||||
} else if (shouldRefreshMetadata) {
|
} else if (shouldRefreshMetadata) {
|
||||||
// 元数据缺失(如重启恢复的标签页只持久化了占位身份)时行标识未知:
|
// 元数据缺失(如重启恢复的标签页只持久化了占位身份)时行标识未知:
|
||||||
// 挂起等待,防止数据查询先返回后编辑/保存以空 primaryKeys 短暂可用,
|
// 挂起等待,防止数据查询先返回后编辑/保存以空 primaryKeys 短暂可用,
|
||||||
|
|||||||
@@ -1849,6 +1849,7 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function saveChanges(saveOptions: SaveChangesOptions = {}) {
|
async function saveChanges(saveOptions: SaveChangesOptions = {}) {
|
||||||
|
if (!editable.value) return;
|
||||||
if (saveOptions.autoSave && manualSaveRequired.value) return;
|
if (saveOptions.autoSave && manualSaveRequired.value) return;
|
||||||
if (isSaving.value) {
|
if (isSaving.value) {
|
||||||
if (saveOptions.autoSave) pendingAutoSaveRequested = true;
|
if (saveOptions.autoSave) pendingAutoSaveRequested = true;
|
||||||
@@ -1881,6 +1882,7 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
|
|||||||
});
|
});
|
||||||
if (!confirmed) return;
|
if (!confirmed) return;
|
||||||
}
|
}
|
||||||
|
if (!editable.value) return;
|
||||||
if (customHandler && snapshot.newRows.length > 0 && customHandler.supportsInsert !== true && customHandler.canInsert !== true) {
|
if (customHandler && snapshot.newRows.length > 0 && customHandler.supportsInsert !== true && customHandler.canInsert !== true) {
|
||||||
saveError.value = i18n.global.t("grid.insertRowsNotSupported");
|
saveError.value = i18n.global.t("grid.insertRowsNotSupported");
|
||||||
return;
|
return;
|
||||||
@@ -1977,6 +1979,10 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!editable.value || stmtOptions?.tableMeta !== tableMeta.value) {
|
||||||
|
await finishInterruptedSaveChanges(snapshot);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
let apiResult: { affected_rows?: number } | undefined;
|
let apiResult: { affected_rows?: number } | undefined;
|
||||||
console.info("[DBX][dataGrid:save-statements]", {
|
console.info("[DBX][dataGrid:save-statements]", {
|
||||||
|
|||||||
@@ -55,6 +55,20 @@ describe("tableMetadataCache columns facet request counts", () => {
|
|||||||
mocks.listIndexes.mockResolvedValue([]);
|
mocks.listIndexes.mockResolvedValue([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("does not treat failed index discovery as a complete cacheable row identity", async () => {
|
||||||
|
mocks.getColumns.mockResolvedValue([{ ...column("id"), is_primary_key: false }]);
|
||||||
|
mocks.listIndexes.mockRejectedValueOnce(new Error("indexes unavailable"));
|
||||||
|
const failed = await loadTableMetadata(usersRequest);
|
||||||
|
expect(failed.metadata.rowIdentityResolved).toBe(false);
|
||||||
|
expect(failed.metadata.primaryKeys).toEqual([]);
|
||||||
|
expect(getCachedTableMetadata(usersRequest)).toBeUndefined();
|
||||||
|
|
||||||
|
const retried = await loadTableMetadata(usersRequest);
|
||||||
|
expect(retried.metadata.rowIdentityResolved).toBe(true);
|
||||||
|
expect(mocks.listIndexes).toHaveBeenCalledTimes(2);
|
||||||
|
expect(mocks.getColumns).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
it("R1 — cold single-source columns load: 1 getColumns, 0 listIndexes", async () => {
|
it("R1 — cold single-source columns load: 1 getColumns, 0 listIndexes", async () => {
|
||||||
const result = await loadTableColumns({ ...usersRequest });
|
const result = await loadTableColumns({ ...usersRequest });
|
||||||
expect(result.columns[0]?.name).toBe("users");
|
expect(result.columns[0]?.name).toBe("users");
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ export interface TableMetadata {
|
|||||||
columns: ColumnInfo[];
|
columns: ColumnInfo[];
|
||||||
indexes: IndexInfo[];
|
indexes: IndexInfo[];
|
||||||
primaryKeys: string[];
|
primaryKeys: string[];
|
||||||
|
rowIdentityResolved?: boolean;
|
||||||
cachedAt: number;
|
cachedAt: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,11 +332,16 @@ export async function loadTableMetadata(request: TableMetadataRequest): Promise<
|
|||||||
// discovery independently unless an agent-backed PostgreSQL-family
|
// discovery independently unless an agent-backed PostgreSQL-family
|
||||||
// relation must first report its visible schema for the index lookup.
|
// relation must first report its visible schema for the index lookup.
|
||||||
const resolveReportedSchema = (request.databaseType === "vastbase" || request.databaseType === "kingbase") && !request.schema;
|
const resolveReportedSchema = (request.databaseType === "vastbase" || request.databaseType === "kingbase") && !request.schema;
|
||||||
const indexesPromise = resolveReportedSchema ? undefined : loadTableIndexes(request).catch((): IndexInfo[] => []);
|
let rowIdentityResolved = true;
|
||||||
|
const indexFailure = (): IndexInfo[] => {
|
||||||
|
rowIdentityResolved = false;
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
const indexesPromise = resolveReportedSchema ? undefined : loadTableIndexes(request).catch(indexFailure);
|
||||||
const columnsResult = await columnsPromise;
|
const columnsResult = await columnsPromise;
|
||||||
const columns = columnsResult.columns;
|
const columns = columnsResult.columns;
|
||||||
const resolvedSchema = resolveReportedSchema ? columns.find((column) => column.resolved_schema)?.resolved_schema : request.schema;
|
const resolvedSchema = resolveReportedSchema ? columns.find((column) => column.resolved_schema)?.resolved_schema : request.schema;
|
||||||
const indexes = columns.length > 0 ? await (indexesPromise ?? loadTableIndexes({ ...request, schema: resolvedSchema }).catch((): IndexInfo[] => [])) : [];
|
const indexes = columns.length > 0 ? await (indexesPromise ?? loadTableIndexes({ ...request, schema: resolvedSchema }).catch(indexFailure)) : [];
|
||||||
const primaryKeys = editableRowIdentifierColumns(request.databaseType as DatabaseType, columns, indexes, request.tableType);
|
const primaryKeys = editableRowIdentifierColumns(request.databaseType as DatabaseType, columns, indexes, request.tableType);
|
||||||
return {
|
return {
|
||||||
schema: resolvedSchema || undefined,
|
schema: resolvedSchema || undefined,
|
||||||
@@ -346,6 +352,7 @@ export async function loadTableMetadata(request: TableMetadataRequest): Promise<
|
|||||||
columns,
|
columns,
|
||||||
indexes,
|
indexes,
|
||||||
primaryKeys,
|
primaryKeys,
|
||||||
|
rowIdentityResolved: rowIdentityResolved && columns.length > 0,
|
||||||
cachedAt: columnsResult.cachedAt,
|
cachedAt: columnsResult.cachedAt,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@@ -353,7 +360,7 @@ export async function loadTableMetadata(request: TableMetadataRequest): Promise<
|
|||||||
);
|
);
|
||||||
|
|
||||||
// 必须在 unregister 前比较:最后一个在途加载注销时会顺带清掉代数记录
|
// 必须在 unregister 前比较:最后一个在途加载注销时会顺带清掉代数记录
|
||||||
if (invalidationStampAtStart === (tableMetadataInvalidationStamps.get(scopeKey) ?? 0)) {
|
if (metadata.rowIdentityResolved && invalidationStampAtStart === (tableMetadataInvalidationStamps.get(scopeKey) ?? 0)) {
|
||||||
tableMetadataCache.set(scope, metadata, { cachedAt: metadata.cachedAt });
|
tableMetadataCache.set(scope, metadata, { cachedAt: metadata.cachedAt });
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -17,6 +17,25 @@ describe("data tab snapshot restore vs tableMetaPending", () => {
|
|||||||
setActivePinia(createPinia());
|
setActivePinia(createPinia());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("keeps projection-only metadata pending until complete identity metadata arrives", async () => {
|
||||||
|
const { useQueryStore } = await import("@/stores/queryStore");
|
||||||
|
const store = useQueryStore();
|
||||||
|
const tabId = store.createTab("pg-1", "app", "users", "data", "public");
|
||||||
|
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||||
|
const metadata = { tableName: "users", schema: "public", columns: [column("id")], primaryKeys: ["id"] };
|
||||||
|
store.setTableMeta(tabId, metadata);
|
||||||
|
expect(tab.tableMetaPending).toBe(false);
|
||||||
|
|
||||||
|
store.setTableMeta(tabId, { ...metadata, columns: [{ ...column("id"), is_primary_key: false }], primaryKeys: [] }, { rowIdentityPending: true });
|
||||||
|
expect(tab.tableMetaPending).toBe(true);
|
||||||
|
expect(tab.tableMeta?.primaryKeys).toEqual([]);
|
||||||
|
store.setTableMeta(tabId, { ...metadata, columns: [], primaryKeys: [] });
|
||||||
|
expect(tab.tableMetaPending).toBe(true);
|
||||||
|
|
||||||
|
store.setTableMeta(tabId, metadata);
|
||||||
|
expect(tab.tableMetaPending).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
it("does not roll back real metadata to a placeholder snapshot and re-pends placeholder restores", async () => {
|
it("does not roll back real metadata to a placeholder snapshot and re-pends placeholder restores", async () => {
|
||||||
vi.doMock("@/lib/tabs/tabResultCache", async (importOriginal) => {
|
vi.doMock("@/lib/tabs/tabResultCache", async (importOriginal) => {
|
||||||
const actual = await importOriginal<typeof import("@/lib/tabs/tabResultCache")>();
|
const actual = await importOriginal<typeof import("@/lib/tabs/tabResultCache")>();
|
||||||
|
|||||||
@@ -5064,9 +5064,10 @@ export const useQueryStore = defineStore("query", () => {
|
|||||||
return clearInvalidDataTabSortState(tab, tab.tableMeta.columns);
|
return clearInvalidDataTabSortState(tab, tab.tableMeta.columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setTableMeta(id: string, meta: NonNullable<QueryTab["tableMeta"]>) {
|
function setTableMeta(id: string, meta: NonNullable<QueryTab["tableMeta"]>, options: { rowIdentityPending?: boolean } = {}) {
|
||||||
const tab = tabs.value.find((t) => t.id === id);
|
const tab = tabs.value.find((t) => t.id === id);
|
||||||
if (tab) {
|
if (tab) {
|
||||||
|
if (options.rowIdentityPending) tab.tableMetaPending = true;
|
||||||
tab.tableMeta = meta;
|
tab.tableMeta = meta;
|
||||||
// 记录写入时的连接元数据代次:disconnect/关库/死池重连会使该代次递增,
|
// 记录写入时的连接元数据代次:disconnect/关库/死池重连会使该代次递增,
|
||||||
// 代次失配视同冷缓存,即使位于 30s TTL 窗口内也会重建结构(issue #6623 /
|
// 代次失配视同冷缓存,即使位于 30s TTL 窗口内也会重建结构(issue #6623 /
|
||||||
@@ -5078,7 +5079,7 @@ export const useQueryStore = defineStore("query", () => {
|
|||||||
// columns/primaryKeys 为空的占位身份(如 useNavigationTargets),不得
|
// columns/primaryKeys 为空的占位身份(如 useNavigationTargets),不得
|
||||||
// 借此提前解除编辑门控。失败/中止路径不清除——标签页保持只读是安全
|
// 借此提前解除编辑门控。失败/中止路径不清除——标签页保持只读是安全
|
||||||
// 兜底,刷新或重开表会重新加载元数据恢复
|
// 兜底,刷新或重开表会重新加载元数据恢复
|
||||||
if (meta.columns.length > 0) tab.tableMetaPending = false;
|
if (meta.columns.length > 0 && !options.rowIdentityPending) tab.tableMetaPending = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -269,11 +269,18 @@ pub struct TransactionSession {
|
|||||||
pub pool_key: String,
|
pub pool_key: String,
|
||||||
pub last_activity: std::time::Instant,
|
pub last_activity: std::time::Instant,
|
||||||
pub busy: bool,
|
pub busy: bool,
|
||||||
|
pub snapshot_rotation_safe: bool,
|
||||||
pub connection_id: String,
|
pub connection_id: String,
|
||||||
pub database: String,
|
pub database: String,
|
||||||
pub schema: Option<String>,
|
pub schema: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TransactionSession {
|
||||||
|
pub fn can_rotate_read_only_snapshot(&self, conn: &TxnConnection) -> bool {
|
||||||
|
self.snapshot_rotation_safe && matches!(conn, TxnConnection::Mysql(Some(_)) | TxnConnection::Postgres(_))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! agent_connection_pool_database_type {
|
macro_rules! agent_connection_pool_database_type {
|
||||||
() => {
|
() => {
|
||||||
DatabaseType::Dameng
|
DatabaseType::Dameng
|
||||||
@@ -5018,17 +5025,45 @@ impl AppState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn remove_connection_pools(&self, connection_id: &str) {
|
pub async fn remove_connection_pools(&self, connection_id: &str) {
|
||||||
|
self.rollback_manual_transaction_sessions(connection_id).await;
|
||||||
let removed = self.drain_connection_pools(connection_id).await;
|
let removed = self.drain_connection_pools(connection_id).await;
|
||||||
self.clear_metadata_gates_for_connection(connection_id).await;
|
self.clear_metadata_gates_for_connection(connection_id).await;
|
||||||
self.pool_routing_control().close_removed(removed).await;
|
self.pool_routing_control().close_removed(removed).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn remove_connection_pools_detached(&self, connection_id: &str) {
|
pub async fn remove_connection_pools_detached(&self, connection_id: &str) {
|
||||||
|
self.rollback_manual_transaction_sessions(connection_id).await;
|
||||||
let removed = self.drain_connection_pools(connection_id).await;
|
let removed = self.drain_connection_pools(connection_id).await;
|
||||||
self.clear_metadata_gates_for_connection(connection_id).await;
|
self.clear_metadata_gates_for_connection(connection_id).await;
|
||||||
self.pool_routing_control().close_removed_in_background(removed);
|
self.pool_routing_control().close_removed_in_background(removed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Close and roll back every manual-transaction session of a connection
|
||||||
|
/// before its pools are drained. The transaction sessions hold dedicated
|
||||||
|
/// connections outside the pools being removed; without this the session
|
||||||
|
/// map survives a user disconnect with open transactions, and a later
|
||||||
|
/// reconnect/execution could observe or reuse the stale snapshot state.
|
||||||
|
/// Errors are logged and the session is dropped regardless: pool removal
|
||||||
|
/// is already the caller's decision.
|
||||||
|
async fn rollback_manual_transaction_sessions(&self, connection_id: &str) {
|
||||||
|
let sessions: Vec<(String, TransactionSession)> = {
|
||||||
|
let mut map = self.transaction_sessions.write().await;
|
||||||
|
let keys: Vec<String> = map
|
||||||
|
.iter()
|
||||||
|
.filter(|(_, session)| session.connection_id == connection_id)
|
||||||
|
.map(|(id, _)| id.clone())
|
||||||
|
.collect();
|
||||||
|
keys.into_iter().filter_map(|id| map.remove(&id).map(|session| (id, session))).collect()
|
||||||
|
};
|
||||||
|
for (session_id, session) in sessions {
|
||||||
|
let mut conn = session.connection.lock().await;
|
||||||
|
let outcome = crate::query::rollback_manual_txn_connection(&mut conn).await;
|
||||||
|
if let Err(error) = outcome {
|
||||||
|
log::warn!("[connection:manual-txn:rollback-on-disconnect] session={} error={}", session_id, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn invalidate_agent_pool_if_current(
|
pub async fn invalidate_agent_pool_if_current(
|
||||||
&self,
|
&self,
|
||||||
pool_key: &str,
|
pool_key: &str,
|
||||||
|
|||||||
+382
-10
@@ -3995,6 +3995,38 @@ async fn execute_multi_mysql(
|
|||||||
let statements_ms = statements_started_at.elapsed().as_millis();
|
let statements_ms = statements_started_at.elapsed().as_millis();
|
||||||
drop(executor);
|
drop(executor);
|
||||||
|
|
||||||
|
// Tab-scoped single-connection pools disable COM_RESET_CONNECTION on return
|
||||||
|
// (to preserve session state like temporary tables), so an open transaction
|
||||||
|
// left on the connection — a user-typed BEGIN/START TRANSACTION without
|
||||||
|
// COMMIT, or a canceled/aborted batch that skipped its cleanup — would pin
|
||||||
|
// the REPEATABLE READ snapshot for every later auto-commit query on that
|
||||||
|
// tab, making the tab read stale rows until disconnect. Closing any open
|
||||||
|
// transaction before returning the connection restores the auto-commit
|
||||||
|
// contract; ROLLBACK on an already-committed/implicit transaction is a
|
||||||
|
// server no-op, and a failure here only discards this connection.
|
||||||
|
{
|
||||||
|
let rollback_started_at = std::time::Instant::now();
|
||||||
|
match conn.query_drop("ROLLBACK").await {
|
||||||
|
Ok(()) => {
|
||||||
|
if rollback_started_at.elapsed() > std::time::Duration::from_millis(5) {
|
||||||
|
log::info!(
|
||||||
|
"[query][mysql-batch] trace_id={} open_txn_rollback_ms={}",
|
||||||
|
trace_id,
|
||||||
|
rollback_started_at.elapsed().as_millis()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
// A failed ROLLBACK leaves the transaction state unknown: drop
|
||||||
|
// the connection instead of returning it to the session pool.
|
||||||
|
log::warn!("[query][mysql-batch] trace_id={} open_txn_rollback_failed error={}", trace_id, error);
|
||||||
|
let _ = tokio::time::timeout(Duration::from_secs(5), conn.disconnect()).await;
|
||||||
|
state.remove_pool_by_key(pool_key).await;
|
||||||
|
return Ok(results);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log::info!(
|
log::info!(
|
||||||
"[query][mysql-batch] trace_id={} checkout_ms={} catalog_ms={} statements_ms={} total_ms={} result_count={} row_counts={:?}",
|
"[query][mysql-batch] trace_id={} checkout_ms={} catalog_ms={} statements_ms={} total_ms={} result_count={} row_counts={:?}",
|
||||||
trace_id,
|
trace_id,
|
||||||
@@ -5581,6 +5613,7 @@ async fn begin_transaction_session(
|
|||||||
pool_key: pool_key.clone(),
|
pool_key: pool_key.clone(),
|
||||||
last_activity: std::time::Instant::now(),
|
last_activity: std::time::Instant::now(),
|
||||||
busy: false,
|
busy: false,
|
||||||
|
snapshot_rotation_safe: !consistent_snapshot,
|
||||||
connection_id: connection_id.to_string(),
|
connection_id: connection_id.to_string(),
|
||||||
database: database.to_string(),
|
database: database.to_string(),
|
||||||
schema: schema.map(|s| s.to_string()),
|
schema: schema.map(|s| s.to_string()),
|
||||||
@@ -5754,6 +5787,9 @@ pub async fn execute_in_manual_transaction_with_options(
|
|||||||
// session remains intact.
|
// session remains intact.
|
||||||
check_read_only_for_connection_multi(state, &pool_key, &statements).await?;
|
check_read_only_for_connection_multi(state, &pool_key, &statements).await?;
|
||||||
|
|
||||||
|
let classification: Vec<bool> =
|
||||||
|
classify_manual_transaction_statements(db_type, statements.len(), options.classification_sql.as_deref());
|
||||||
|
|
||||||
let connection = {
|
let connection = {
|
||||||
let mut sessions = state.transaction_sessions.write().await;
|
let mut sessions = state.transaction_sessions.write().await;
|
||||||
let Some(session) = sessions.get_mut(txn_session_id) else {
|
let Some(session) = sessions.get_mut(txn_session_id) else {
|
||||||
@@ -5767,6 +5803,8 @@ pub async fn execute_in_manual_transaction_with_options(
|
|||||||
Some(session)
|
Some(session)
|
||||||
} else {
|
} else {
|
||||||
session.busy = true;
|
session.busy = true;
|
||||||
|
session.snapshot_rotation_safe &=
|
||||||
|
classification.len() == statements.len() && classification.iter().all(|proven| *proven);
|
||||||
session.last_activity = std::time::Instant::now();
|
session.last_activity = std::time::Instant::now();
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@@ -5788,15 +5826,6 @@ pub async fn execute_in_manual_transaction_with_options(
|
|||||||
let row_limit = options.max_rows.unwrap_or(MAX_ROWS).max(1);
|
let row_limit = options.max_rows.unwrap_or(MAX_ROWS).max(1);
|
||||||
let mut results = Vec::with_capacity(statements.len());
|
let mut results = Vec::with_capacity(statements.len());
|
||||||
|
|
||||||
// Sticky-dialect classification pairing. The core splits both the execution
|
|
||||||
// SQL and, when present, the user-facing classification SQL with the same
|
|
||||||
// dialect-aware splitter. A marker is emitted only when both lists have the
|
|
||||||
// same non-zero count and every paired user statement is proven read-only;
|
|
||||||
// any mismatch is fail-closed (no marker). This is deliberately a
|
|
||||||
// trust-boundary count/position pairing, not a SQL-equivalence parser.
|
|
||||||
let classification: Vec<bool> =
|
|
||||||
classify_manual_transaction_statements(db_type, statements.len(), options.classification_sql.as_deref());
|
|
||||||
|
|
||||||
let mut conn = connection.lock().await;
|
let mut conn = connection.lock().await;
|
||||||
for (i, statement) in statements.iter().enumerate() {
|
for (i, statement) in statements.iter().enumerate() {
|
||||||
let result = match &mut *conn {
|
let result = match &mut *conn {
|
||||||
@@ -5856,6 +5885,29 @@ pub async fn execute_in_manual_transaction_with_options(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Snapshot rotation for fully proven read-only batches (native MySQL/PG
|
||||||
|
// connections only): see rotate_clean_read_only_snapshot. Runs while the
|
||||||
|
// session is still marked busy so no concurrent execution can observe the
|
||||||
|
// half-rotated transaction. A rotation failure tears the session down and
|
||||||
|
// leans on the frontend rolled-back-session recovery (next execution opens
|
||||||
|
// a fresh session) instead of failing the already-successful batch.
|
||||||
|
if manual_txn_batch_fully_proven_read_only(&results) && {
|
||||||
|
let sessions = state.transaction_sessions.read().await;
|
||||||
|
sessions.get(txn_session_id).map(|session| session.can_rotate_read_only_snapshot(&conn)).unwrap_or(false)
|
||||||
|
} {
|
||||||
|
if let Err(rotation_error) = rotate_clean_read_only_snapshot(&mut conn, schema).await {
|
||||||
|
let removed = {
|
||||||
|
let mut sessions = state.transaction_sessions.write().await;
|
||||||
|
sessions.remove(txn_session_id).is_some()
|
||||||
|
};
|
||||||
|
if removed {
|
||||||
|
let _ = rollback_manual_txn_connection(&mut conn).await;
|
||||||
|
release_manual_txn_session_pool(state, &connection_id, &mut conn).await;
|
||||||
|
}
|
||||||
|
let _ = rotation_error;
|
||||||
|
}
|
||||||
|
}
|
||||||
drop(conn);
|
drop(conn);
|
||||||
|
|
||||||
let should_watch = {
|
let should_watch = {
|
||||||
@@ -5916,6 +5968,7 @@ where
|
|||||||
Some(sessions.remove(txn_session_id).expect("session exists").connection)
|
Some(sessions.remove(txn_session_id).expect("session exists").connection)
|
||||||
} else {
|
} else {
|
||||||
session.busy = true;
|
session.busy = true;
|
||||||
|
session.snapshot_rotation_safe = false;
|
||||||
session.last_activity = std::time::Instant::now();
|
session.last_activity = std::time::Instant::now();
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@@ -6068,7 +6121,54 @@ where
|
|||||||
stream_result
|
stream_result
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn rollback_manual_txn_connection(conn: &mut TxnConnection) -> Result<(), String> {
|
/// Whether a finished batch was fully proven read-only. Session history is
|
||||||
|
/// checked separately before rotating its snapshot.
|
||||||
|
fn manual_txn_batch_fully_proven_read_only(results: &[ExecuteMultiResult]) -> bool {
|
||||||
|
!results.is_empty() && results.iter().all(|result| result.manual_transaction_proven_read_only)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Rotate the transaction on a natively-connected MySQL/PostgreSQL session after
|
||||||
|
/// a fully proven read-only batch. MySQL REPEATABLE READ (and PostgreSQL when
|
||||||
|
/// the server default was changed to a snapshot isolation) pins the read view of
|
||||||
|
/// the first SELECT for the whole transaction: a user who keeps polling a clean
|
||||||
|
/// read-only session would otherwise never see rows committed by others, and the
|
||||||
|
/// clean-state toolbar hides Commit/Rollback, leaving disconnect/reconnect as
|
||||||
|
/// the only visible way out. A rollback of a read-only transaction and a fresh
|
||||||
|
/// BEGIN are both cheap metadata operations. The session-history gate excludes
|
||||||
|
/// any prior write or uncertain batch. Failures never fail the successful batch:
|
||||||
|
/// the session is torn down instead and the frontend's existing
|
||||||
|
/// rolled-back-session recovery transparently opens a new one on the next run.
|
||||||
|
async fn rotate_clean_read_only_snapshot(conn: &mut TxnConnection, schema: Option<&str>) -> Result<(), String> {
|
||||||
|
match conn {
|
||||||
|
TxnConnection::Mysql(Some(conn)) => {
|
||||||
|
conn.query_drop("ROLLBACK").await.map_err(|e| format!("ROLLBACK failed: {e}"))?;
|
||||||
|
conn.query_drop("START TRANSACTION").await.map_err(|e| format!("START TRANSACTION failed: {e}"))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
TxnConnection::Postgres(conn) => {
|
||||||
|
conn.execute_typed("ROLLBACK", &[]).await.map_err(|e| format!("ROLLBACK failed: {e}"))?;
|
||||||
|
conn.execute_typed("BEGIN", &[]).await.map_err(|e| format!("BEGIN failed: {e}"))?;
|
||||||
|
if let Some(schema) = schema {
|
||||||
|
db::postgres::set_postgres_search_path(
|
||||||
|
conn,
|
||||||
|
schema,
|
||||||
|
db::postgres::PostgresSearchPathContext::LocalTransaction,
|
||||||
|
db::connection_timeout(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("SET search_path failed: {e}"))?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
// Agent and external-driver sessions cannot reopen in place (their
|
||||||
|
// rollback path closes the dedicated session), so they keep the
|
||||||
|
// snapshot semantics; proven-read-only markers for those dialects do
|
||||||
|
// not reach this helper's native variants in practice.
|
||||||
|
_ => Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn rollback_manual_txn_connection(conn: &mut TxnConnection) -> Result<(), String> {
|
||||||
rollback_manual_txn_connection_with_postgres_timeout(conn, None).await
|
rollback_manual_txn_connection_with_postgres_timeout(conn, None).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6476,6 +6576,276 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::query_cancel::RunningTaskMetadata;
|
use crate::query_cancel::RunningTaskMetadata;
|
||||||
|
|
||||||
|
mod manual_transaction_snapshot_tests {
|
||||||
|
use super::*;
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
|
||||||
|
struct SnapshotTestConnect {
|
||||||
|
commands: Arc<Mutex<Vec<String>>>,
|
||||||
|
fail_once: Option<&'static str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl deadpool_postgres::Connect for SnapshotTestConnect {
|
||||||
|
fn connect(
|
||||||
|
&self,
|
||||||
|
config: &tokio_postgres::Config,
|
||||||
|
) -> futures::future::BoxFuture<
|
||||||
|
'_,
|
||||||
|
Result<(tokio_postgres::Client, tokio::task::JoinHandle<()>), tokio_postgres::Error>,
|
||||||
|
> {
|
||||||
|
let mut config = config.clone();
|
||||||
|
config.user("test").ssl_mode(tokio_postgres::config::SslMode::Disable);
|
||||||
|
let commands = Arc::clone(&self.commands);
|
||||||
|
let mut fail_once = self.fail_once;
|
||||||
|
Box::pin(async move {
|
||||||
|
let (client_socket, mut server_socket) = tokio::io::duplex(8192);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let startup_length = server_socket.read_u32().await.unwrap();
|
||||||
|
let mut startup = vec![0; startup_length as usize - 4];
|
||||||
|
server_socket.read_exact(&mut startup).await.unwrap();
|
||||||
|
server_socket.write_all(b"R\0\0\0\x08\0\0\0\0Z\0\0\0\x05I").await.unwrap();
|
||||||
|
let mut statement = String::new();
|
||||||
|
while let Ok(message_type) = server_socket.read_u8().await {
|
||||||
|
let length = server_socket.read_u32().await.unwrap();
|
||||||
|
let mut payload = vec![0; length as usize - 4];
|
||||||
|
server_socket.read_exact(&mut payload).await.unwrap();
|
||||||
|
let (response_type, response) = match message_type {
|
||||||
|
b'P' => {
|
||||||
|
statement =
|
||||||
|
String::from_utf8(payload.split(|byte| *byte == 0).nth(1).unwrap().to_vec())
|
||||||
|
.unwrap();
|
||||||
|
(b'1', Vec::new())
|
||||||
|
}
|
||||||
|
b'B' => (b'2', Vec::new()),
|
||||||
|
b'D' => {
|
||||||
|
if payload.first() == Some(&b'S') {
|
||||||
|
server_socket.write_all(b"t\0\0\0\x06\0\0").await.unwrap();
|
||||||
|
}
|
||||||
|
(b'n', Vec::new())
|
||||||
|
}
|
||||||
|
b'E' => {
|
||||||
|
commands.lock().unwrap().push(statement.clone());
|
||||||
|
if fail_once == Some(statement.as_str()) {
|
||||||
|
fail_once = None;
|
||||||
|
(b'E', b"SERROR\0CXX000\0Mtest statement failed\0\0".to_vec())
|
||||||
|
} else {
|
||||||
|
let tag = if statement.starts_with("UPDATE") {
|
||||||
|
"UPDATE 1"
|
||||||
|
} else if statement.starts_with("SELECT") {
|
||||||
|
"SELECT 0"
|
||||||
|
} else {
|
||||||
|
statement.as_str()
|
||||||
|
};
|
||||||
|
(b'C', format!("{tag}\0").into_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b'S' => (b'Z', vec![b'I']),
|
||||||
|
b'C' => (b'3', Vec::new()),
|
||||||
|
b'X' => break,
|
||||||
|
other => panic!("unexpected PostgreSQL message: {other}"),
|
||||||
|
};
|
||||||
|
server_socket.write_u8(response_type).await.unwrap();
|
||||||
|
server_socket.write_u32((response.len() + 4) as u32).await.unwrap();
|
||||||
|
server_socket.write_all(&response).await.unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let (client, connection) = config.connect_raw(client_socket, tokio_postgres::NoTls).await?;
|
||||||
|
let task = tokio::spawn(async move {
|
||||||
|
let _ = connection.await;
|
||||||
|
});
|
||||||
|
Ok((client, task))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn snapshot_state(
|
||||||
|
fail_once: Option<&'static str>,
|
||||||
|
) -> (AppState, Arc<Mutex<Vec<String>>>, tempfile::TempDir) {
|
||||||
|
let directory = tempfile::tempdir().unwrap();
|
||||||
|
let state = AppState::new(Storage::open(&directory.path().join("storage.db")).await.unwrap());
|
||||||
|
let config = test_connection_config(DatabaseType::Postgres);
|
||||||
|
state.configs.write().await.insert(config.id.clone(), config);
|
||||||
|
let commands = Arc::new(Mutex::new(Vec::new()));
|
||||||
|
let manager = deadpool_postgres::Manager::from_connect(
|
||||||
|
tokio_postgres::Config::new(),
|
||||||
|
SnapshotTestConnect { commands: Arc::clone(&commands), fail_once },
|
||||||
|
deadpool_postgres::ManagerConfig::default(),
|
||||||
|
);
|
||||||
|
let pool = deadpool_postgres::Pool::builder(manager).max_size(1).build().unwrap();
|
||||||
|
let connection = timeout(Duration::from_secs(5), pool.get()).await.unwrap().unwrap();
|
||||||
|
state.transaction_sessions.write().await.insert(
|
||||||
|
"snapshot".to_string(),
|
||||||
|
TransactionSession {
|
||||||
|
connection: Arc::new(tokio::sync::Mutex::new(TxnConnection::Postgres(Box::new(connection)))),
|
||||||
|
pool_key: "conn-1".to_string(),
|
||||||
|
last_activity: std::time::Instant::now(),
|
||||||
|
busy: false,
|
||||||
|
snapshot_rotation_safe: true,
|
||||||
|
connection_id: "conn-1".to_string(),
|
||||||
|
database: "test".to_string(),
|
||||||
|
schema: None,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
(state, commands, directory)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute_snapshot_batch(
|
||||||
|
state: &AppState,
|
||||||
|
sql: &str,
|
||||||
|
classification_sql: Option<&str>,
|
||||||
|
) -> Result<Vec<ExecuteMultiResult>, String> {
|
||||||
|
timeout(
|
||||||
|
Duration::from_secs(5),
|
||||||
|
execute_in_manual_transaction_with_options(
|
||||||
|
state,
|
||||||
|
"snapshot",
|
||||||
|
sql,
|
||||||
|
"test",
|
||||||
|
None,
|
||||||
|
ManualTransactionExecutionOptions {
|
||||||
|
classification_sql: classification_sql.map(str::to_string),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("manual transaction must not deadlock")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn snapshot_predicate_uses_the_already_held_connection_guard() {
|
||||||
|
let (state, _, _directory) = snapshot_state(None).await;
|
||||||
|
let connection = Arc::clone(&state.transaction_sessions.read().await["snapshot"].connection);
|
||||||
|
let guard = connection.lock().await;
|
||||||
|
assert!(connection.try_lock().is_err());
|
||||||
|
let mut sessions = state.transaction_sessions.write().await;
|
||||||
|
let session = sessions.get_mut("snapshot").unwrap();
|
||||||
|
assert!(session.can_rotate_read_only_snapshot(&guard));
|
||||||
|
session.snapshot_rotation_safe = false;
|
||||||
|
assert!(!session.can_rotate_read_only_snapshot(&guard));
|
||||||
|
session.snapshot_rotation_safe = true;
|
||||||
|
assert!(!session.can_rotate_read_only_snapshot(&TxnConnection::Mysql(None)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn repeated_reads_rotate_and_no_statement_preserves_clean_history() {
|
||||||
|
let (state, commands, _directory) = snapshot_state(None).await;
|
||||||
|
execute_snapshot_batch(&state, "-- no statement", None).await.unwrap();
|
||||||
|
assert!(commands.lock().unwrap().is_empty());
|
||||||
|
for _ in 0..2 {
|
||||||
|
let results = execute_snapshot_batch(&state, "SELECT 1", Some("SELECT 1")).await.unwrap();
|
||||||
|
assert!(manual_txn_batch_fully_proven_read_only(&results));
|
||||||
|
let sessions = state.transaction_sessions.read().await;
|
||||||
|
assert!(!sessions["snapshot"].busy);
|
||||||
|
assert!(sessions["snapshot"].snapshot_rotation_safe);
|
||||||
|
}
|
||||||
|
assert_eq!(*commands.lock().unwrap(), ["SELECT 1", "ROLLBACK", "BEGIN", "SELECT 1", "ROLLBACK", "BEGIN"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn writes_unknown_and_mixed_batches_permanently_prevent_rotation() {
|
||||||
|
for (sql, classification) in [
|
||||||
|
("UPDATE users SET id = 2", Some("UPDATE users SET id = 2")),
|
||||||
|
("SELECT 1", None),
|
||||||
|
("SELECT 1", Some("SELECT unknown_function()")),
|
||||||
|
("SELECT 1; UPDATE users SET id = 2", Some("SELECT 1; UPDATE users SET id = 2")),
|
||||||
|
] {
|
||||||
|
let (state, commands, _directory) = snapshot_state(None).await;
|
||||||
|
execute_snapshot_batch(&state, sql, classification).await.unwrap();
|
||||||
|
for _ in 0..2 {
|
||||||
|
execute_snapshot_batch(&state, "SELECT 1", Some("SELECT 1")).await.unwrap();
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let sessions = state.transaction_sessions.read().await;
|
||||||
|
assert!(!sessions["snapshot"].snapshot_rotation_safe);
|
||||||
|
assert!(!sessions["snapshot"].busy);
|
||||||
|
}
|
||||||
|
assert!(!commands.lock().unwrap().iter().any(|sql| sql == "ROLLBACK" || sql == "BEGIN"));
|
||||||
|
commit_manual_transaction(&state, "snapshot").await.unwrap();
|
||||||
|
assert_eq!(commands.lock().unwrap().last().unwrap(), "COMMIT");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn unclassified_stream_prevents_later_snapshot_rotation() {
|
||||||
|
let (state, commands, _directory) = snapshot_state(None).await;
|
||||||
|
stream_rows_in_manual_transaction(&state, "snapshot", "SELECT 1", 10, |_| Ok(())).await.unwrap();
|
||||||
|
execute_snapshot_batch(&state, "SELECT 1", Some("SELECT 1")).await.unwrap();
|
||||||
|
assert!(!state.transaction_sessions.read().await["snapshot"].snapshot_rotation_safe);
|
||||||
|
assert_eq!(*commands.lock().unwrap(), ["SELECT 1", "SELECT 1"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn rotation_failure_removes_session_without_failing_successful_read() {
|
||||||
|
for failure in ["ROLLBACK", "BEGIN"] {
|
||||||
|
let (state, commands, _directory) = snapshot_state(Some(failure)).await;
|
||||||
|
let results = execute_snapshot_batch(&state, "SELECT 1", Some("SELECT 1")).await.unwrap();
|
||||||
|
assert!(manual_txn_batch_fully_proven_read_only(&results));
|
||||||
|
assert!(!state.transaction_sessions.read().await.contains_key("snapshot"));
|
||||||
|
let expected = if failure == "BEGIN" {
|
||||||
|
vec!["SELECT 1", "ROLLBACK", "BEGIN", "ROLLBACK"]
|
||||||
|
} else {
|
||||||
|
vec!["SELECT 1", "ROLLBACK", "ROLLBACK"]
|
||||||
|
};
|
||||||
|
assert_eq!(*commands.lock().unwrap(), expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn failed_batch_after_write_keeps_existing_rollback_cleanup() {
|
||||||
|
let (state, commands, _directory) = snapshot_state(Some("SELECT 1")).await;
|
||||||
|
execute_snapshot_batch(&state, "UPDATE users SET id = 2", Some("UPDATE users SET id = 2")).await.unwrap();
|
||||||
|
let error = execute_snapshot_batch(&state, "SELECT 1", Some("SELECT 1")).await.unwrap_err();
|
||||||
|
assert!(error.contains("manual transaction was rolled back"));
|
||||||
|
assert!(!state.transaction_sessions.read().await.contains_key("snapshot"));
|
||||||
|
assert_eq!(*commands.lock().unwrap(), ["UPDATE users SET id = 2", "SELECT 1", "ROLLBACK"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn cancelled_stream_after_write_keeps_existing_rollback_cleanup() {
|
||||||
|
let (state, commands, _directory) = snapshot_state(None).await;
|
||||||
|
execute_snapshot_batch(&state, "UPDATE users SET id = 2", Some("UPDATE users SET id = 2")).await.unwrap();
|
||||||
|
let cancel = CancellationToken::new();
|
||||||
|
cancel.cancel();
|
||||||
|
let result =
|
||||||
|
stream_rows_in_manual_transaction_with_cancel(&state, "snapshot", "SELECT 1", 10, Some(cancel), |_| {
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(!state.transaction_sessions.read().await.contains_key("snapshot"));
|
||||||
|
let commands = commands.lock().unwrap();
|
||||||
|
assert_eq!(commands.last().unwrap(), "ROLLBACK");
|
||||||
|
assert!(!commands.iter().any(|sql| sql == "BEGIN"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn manual_txn_batch_proven_read_only_requires_non_empty_all_proven_results() {
|
||||||
|
// Empty batch (e.g. comments-only script) must not rotate: there is no
|
||||||
|
// snapshot to renew and rotating would churn a BEGIN for nothing.
|
||||||
|
assert!(!manual_txn_batch_fully_proven_read_only(&[]));
|
||||||
|
|
||||||
|
let unproven =
|
||||||
|
vec![ExecuteMultiResult::success_with_optional_server_large_values(empty_query_result(1), false)];
|
||||||
|
assert!(!manual_txn_batch_fully_proven_read_only(&unproven));
|
||||||
|
|
||||||
|
let proven = unproven
|
||||||
|
.clone()
|
||||||
|
.into_iter()
|
||||||
|
.map(|result| result.with_manual_transaction_proven_read_only())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
assert!(manual_txn_batch_fully_proven_read_only(&proven));
|
||||||
|
|
||||||
|
// One write statement anywhere in the batch keeps the transaction.
|
||||||
|
let mixed = vec![
|
||||||
|
ExecuteMultiResult::success_with_optional_server_large_values(empty_query_result(1), false)
|
||||||
|
.with_manual_transaction_proven_read_only(),
|
||||||
|
ExecuteMultiResult::success_with_optional_server_large_values(empty_query_result(1), false),
|
||||||
|
];
|
||||||
|
assert!(!manual_txn_batch_fully_proven_read_only(&mixed));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn redshift_queries_prefer_text_protocol() {
|
fn redshift_queries_prefer_text_protocol() {
|
||||||
assert!(postgres_prefers_text_protocol(Some(DatabaseType::Redshift)));
|
assert!(postgres_prefers_text_protocol(Some(DatabaseType::Redshift)));
|
||||||
@@ -9454,6 +9824,7 @@ for line in sys.stdin:
|
|||||||
pool_key: pool_key.to_string(),
|
pool_key: pool_key.to_string(),
|
||||||
last_activity: std::time::Instant::now(),
|
last_activity: std::time::Instant::now(),
|
||||||
busy: false,
|
busy: false,
|
||||||
|
snapshot_rotation_safe: true,
|
||||||
connection_id: "jdbc-conn".to_string(),
|
connection_id: "jdbc-conn".to_string(),
|
||||||
database: "dbx_test".to_string(),
|
database: "dbx_test".to_string(),
|
||||||
schema: None,
|
schema: None,
|
||||||
@@ -10649,6 +11020,7 @@ for line in sys.stdin:
|
|||||||
pool_key: "agent-conn".to_string(),
|
pool_key: "agent-conn".to_string(),
|
||||||
last_activity: std::time::Instant::now(),
|
last_activity: std::time::Instant::now(),
|
||||||
busy: false,
|
busy: false,
|
||||||
|
snapshot_rotation_safe: true,
|
||||||
connection_id: "agent-conn".to_string(),
|
connection_id: "agent-conn".to_string(),
|
||||||
database: "ORCL".to_string(),
|
database: "ORCL".to_string(),
|
||||||
schema: None,
|
schema: None,
|
||||||
|
|||||||
@@ -0,0 +1,197 @@
|
|||||||
|
use dbx_core::connection::AppState;
|
||||||
|
use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
|
||||||
|
use dbx_core::query::{
|
||||||
|
begin_database_backup_snapshot, begin_manual_transaction, commit_manual_transaction, execute_in_manual_transaction,
|
||||||
|
execute_in_manual_transaction_with_options, execute_sql_statement, rollback_manual_transaction,
|
||||||
|
ManualTransactionExecutionOptions,
|
||||||
|
};
|
||||||
|
use dbx_core::storage::Storage;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
fn live_config(prefix: &str, database_type: DatabaseType, default_port: u16) -> ConnectionConfig {
|
||||||
|
serde_json::from_value(serde_json::json!({
|
||||||
|
"id": format!("snapshot-rotation-{}", uuid::Uuid::new_v4().simple()),
|
||||||
|
"name": "Snapshot rotation regression",
|
||||||
|
"db_type": database_type,
|
||||||
|
"host": std::env::var(format!("{prefix}_HOST")).expect("live database host"),
|
||||||
|
"port": std::env::var(format!("{prefix}_PORT")).ok().and_then(|port| port.parse::<u16>().ok()).unwrap_or(default_port),
|
||||||
|
"username": std::env::var(format!("{prefix}_USER")).expect("live database user"),
|
||||||
|
"password": std::env::var(format!("{prefix}_PASSWORD")).expect("live database password"),
|
||||||
|
"database": std::env::var(format!("{prefix}_DATABASE")).expect("live database name"),
|
||||||
|
"connect_timeout_secs": 5,
|
||||||
|
"query_timeout_secs": 30,
|
||||||
|
"idle_timeout_secs": 60,
|
||||||
|
"keepalive_interval_secs": 0,
|
||||||
|
"url_params": std::env::var(format!("{prefix}_URL_PARAMS")).ok()
|
||||||
|
}))
|
||||||
|
.expect("live connection configuration")
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn setup(config: ConnectionConfig) -> (Arc<AppState>, std::path::PathBuf, String) {
|
||||||
|
let storage_path = std::env::temp_dir().join(format!("dbx-snapshot-rotation-{}.db", uuid::Uuid::new_v4().simple()));
|
||||||
|
let state = Arc::new(AppState::new(Storage::open(&storage_path).await.expect("temporary storage")));
|
||||||
|
let table_name = format!("dbx_pr9434_{}", uuid::Uuid::new_v4().simple());
|
||||||
|
let database = config.database.clone().expect("database");
|
||||||
|
state.configs.write().await.insert(config.id.clone(), config.clone());
|
||||||
|
execute_sql_statement(
|
||||||
|
&state,
|
||||||
|
&config.id,
|
||||||
|
&database,
|
||||||
|
&format!("CREATE TABLE {table_name} (id INTEGER PRIMARY KEY)"),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("create isolated regression table");
|
||||||
|
execute_sql_statement(
|
||||||
|
&state,
|
||||||
|
&config.id,
|
||||||
|
&database,
|
||||||
|
&format!("INSERT INTO {table_name} (id) VALUES (1)"),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("seed regression table");
|
||||||
|
(state, storage_path, table_name)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn assert_transaction_row_count(
|
||||||
|
state: &AppState,
|
||||||
|
transaction: &str,
|
||||||
|
database: &str,
|
||||||
|
table_name: &str,
|
||||||
|
expected: i64,
|
||||||
|
) {
|
||||||
|
let sql = format!("SELECT COUNT(*) AS row_count FROM {table_name}");
|
||||||
|
let result = execute_in_manual_transaction_with_options(
|
||||||
|
state,
|
||||||
|
transaction,
|
||||||
|
&sql,
|
||||||
|
database,
|
||||||
|
None,
|
||||||
|
ManualTransactionExecutionOptions {
|
||||||
|
max_rows: Some(10),
|
||||||
|
classification_sql: Some(sql.clone()),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("read transaction rows");
|
||||||
|
assert!(result[0].manual_transaction_proven_read_only);
|
||||||
|
assert_row_count(&result[0].result.rows, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert_row_count(rows: &[Vec<serde_json::Value>], expected: i64) {
|
||||||
|
assert_eq!(rows.len(), 1);
|
||||||
|
assert_eq!(rows[0].len(), 1);
|
||||||
|
let count = rows[0][0].as_i64().or_else(|| rows[0][0].as_str().and_then(|value| value.parse().ok()));
|
||||||
|
assert_eq!(count, Some(expected));
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cleanup(state: &AppState, config: &ConnectionConfig, table_name: &str, storage_path: std::path::PathBuf) {
|
||||||
|
execute_sql_statement(
|
||||||
|
state,
|
||||||
|
&config.id,
|
||||||
|
config.database.as_deref().expect("database"),
|
||||||
|
&format!("DROP TABLE {table_name}"),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("drop isolated regression table");
|
||||||
|
let _ = std::fs::remove_file(storage_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "requires DBX_LIVE_MANUAL_TXN_MYSQL_* on the remote test server"]
|
||||||
|
async fn live_mysql_clean_manual_reads_refresh_snapshot_after_external_write() {
|
||||||
|
let config = live_config("DBX_LIVE_MANUAL_TXN_MYSQL", DatabaseType::Mysql, 3306);
|
||||||
|
let database = config.database.clone().expect("database");
|
||||||
|
let (state, storage_path, table_name) = setup(config.clone()).await;
|
||||||
|
let transaction =
|
||||||
|
begin_manual_transaction(&state, &config.id, &database, None, None).await.expect("begin transaction");
|
||||||
|
assert_transaction_row_count(&state, &transaction, &database, &table_name, 1).await;
|
||||||
|
execute_sql_statement(
|
||||||
|
&state,
|
||||||
|
&config.id,
|
||||||
|
&database,
|
||||||
|
&format!("INSERT INTO {table_name} (id) VALUES (2)"),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("external committed write");
|
||||||
|
assert_transaction_row_count(&state, &transaction, &database, &table_name, 2).await;
|
||||||
|
rollback_manual_transaction(&state, &transaction).await.expect("rollback read-only transaction");
|
||||||
|
cleanup(&state, &config, &table_name, storage_path).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "requires DBX_LIVE_MANUAL_TXN_MYSQL_* on the remote test server"]
|
||||||
|
async fn live_mysql_explicit_backup_snapshot_stays_repeatable() {
|
||||||
|
let config = live_config("DBX_LIVE_MANUAL_TXN_MYSQL", DatabaseType::Mysql, 3306);
|
||||||
|
let database = config.database.clone().expect("database");
|
||||||
|
let (state, storage_path, table_name) = setup(config.clone()).await;
|
||||||
|
let transaction =
|
||||||
|
begin_database_backup_snapshot(&state, &config.id, &database).await.expect("begin consistent snapshot");
|
||||||
|
assert_transaction_row_count(&state, &transaction, &database, &table_name, 1).await;
|
||||||
|
execute_sql_statement(
|
||||||
|
&state,
|
||||||
|
&config.id,
|
||||||
|
&database,
|
||||||
|
&format!("INSERT INTO {table_name} (id) VALUES (2)"),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("external committed write");
|
||||||
|
assert_transaction_row_count(&state, &transaction, &database, &table_name, 1).await;
|
||||||
|
rollback_manual_transaction(&state, &transaction).await.expect("rollback backup snapshot");
|
||||||
|
cleanup(&state, &config, &table_name, storage_path).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn verify_write_then_read_survives_commit(config: ConnectionConfig) {
|
||||||
|
let database = config.database.clone().expect("database");
|
||||||
|
let (state, storage_path, table_name) = setup(config.clone()).await;
|
||||||
|
let transaction =
|
||||||
|
begin_manual_transaction(&state, &config.id, &database, None, None).await.expect("begin transaction");
|
||||||
|
execute_in_manual_transaction(
|
||||||
|
&state,
|
||||||
|
&transaction,
|
||||||
|
&format!("INSERT INTO {table_name} (id) VALUES (2)"),
|
||||||
|
&database,
|
||||||
|
None,
|
||||||
|
Some(10),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("uncommitted write");
|
||||||
|
assert_transaction_row_count(&state, &transaction, &database, &table_name, 2).await;
|
||||||
|
assert_transaction_row_count(&state, &transaction, &database, &table_name, 2).await;
|
||||||
|
commit_manual_transaction(&state, &transaction).await.expect("commit must retain prior write");
|
||||||
|
let result = execute_sql_statement(
|
||||||
|
&state,
|
||||||
|
&config.id,
|
||||||
|
&database,
|
||||||
|
&format!("SELECT COUNT(*) AS row_count FROM {table_name}"),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.expect("read committed rows");
|
||||||
|
assert_row_count(&result.rows, 2);
|
||||||
|
cleanup(&state, &config, &table_name, storage_path).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "requires DBX_LIVE_MANUAL_TXN_MYSQL_* on the remote test server"]
|
||||||
|
async fn live_mysql_write_then_reads_preserve_commit() {
|
||||||
|
verify_write_then_read_survives_commit(live_config("DBX_LIVE_MANUAL_TXN_MYSQL", DatabaseType::Mysql, 3306)).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore = "requires DBX_LIVE_MANUAL_TXN_POSTGRES_* on the remote test server"]
|
||||||
|
async fn live_postgres_write_then_reads_preserve_commit() {
|
||||||
|
verify_write_then_read_survives_commit(live_config("DBX_LIVE_MANUAL_TXN_POSTGRES", DatabaseType::Postgres, 5432))
|
||||||
|
.await;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user