1 parent
278429ab07
commit
4c6c3142c0
18 files changed
+456
-7
No files matched your search
Vendored
+137
@@ -0,0 +1,137 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { shallowRef } from "vue";
|
||||
import type { TreeNode } from "@/types/database";
|
||||
import { mysqlAutoIncrementPreviewKey, mysqlAutoIncrementPreviewSql, mysqlAutoIncrementValue, showMysqlAutoIncrementConfirm, sidebarDangerTarget } from "@/components/sidebar/sidebarTreeDialogState";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
toast: vi.fn(),
|
||||
buildMysqlAutoIncrementSql: vi.fn(),
|
||||
ensureConnected: vi.fn(),
|
||||
executeWithProductionGuard: vi.fn(),
|
||||
refreshMutatedTableDataTabsForNode: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
||||
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: mocks.toast }) }));
|
||||
vi.mock("@/lib/database/dbAdminSql", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("@/lib/database/dbAdminSql")>()),
|
||||
buildMysqlAutoIncrementSql: mocks.buildMysqlAutoIncrementSql,
|
||||
}));
|
||||
|
||||
import { useSidebarTableMutationRuntime } from "@/composables/useSidebarTableMutationRuntime";
|
||||
|
||||
function tableNode(): TreeNode {
|
||||
return { id: "mysql-1:sales:events", label: "events", type: "table", connectionId: "mysql-1", database: "sales", isExpanded: false };
|
||||
}
|
||||
|
||||
function runtime(driverProfile: string | undefined = "mysql") {
|
||||
const activeNode = shallowRef(tableNode());
|
||||
const connectionStore = {
|
||||
getConfig: () => ({ id: "mysql-1", db_type: "mysql", driver_profile: driverProfile }),
|
||||
ensureConnected: mocks.ensureConnected,
|
||||
} as any;
|
||||
const feature = useSidebarTableMutationRuntime({
|
||||
activeNode,
|
||||
releaseActiveNodeReference: vi.fn(),
|
||||
connectionStore,
|
||||
currentDatabaseType: () => "mysql",
|
||||
databaseTypeForNode: () => "mysql",
|
||||
executeWithProductionGuard: mocks.executeWithProductionGuard,
|
||||
closeDroppedTableObjectTabsForNode: vi.fn(),
|
||||
refreshMutatedTableDataTabsForNode: mocks.refreshMutatedTableDataTabsForNode,
|
||||
});
|
||||
return { ...feature, activeNode };
|
||||
}
|
||||
|
||||
describe("useSidebarTableMutationRuntime MySQL AUTO_INCREMENT action", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
sidebarDangerTarget.value = null;
|
||||
showMysqlAutoIncrementConfirm.value = false;
|
||||
mysqlAutoIncrementValue.value = "99";
|
||||
mysqlAutoIncrementPreviewSql.value = "";
|
||||
mysqlAutoIncrementPreviewKey.value = "";
|
||||
mocks.ensureConnected.mockResolvedValue(undefined);
|
||||
mocks.buildMysqlAutoIncrementSql.mockResolvedValue("ALTER TABLE `sales`.`events` AUTO_INCREMENT = 1;");
|
||||
mocks.executeWithProductionGuard.mockResolvedValue(undefined);
|
||||
mocks.refreshMutatedTableDataTabsForNode.mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
it("opens with value 1 and builds only a preview", async () => {
|
||||
const feature = runtime();
|
||||
|
||||
feature.mysqlAutoIncrement();
|
||||
await vi.waitFor(() => expect(mocks.buildMysqlAutoIncrementSql).toHaveBeenCalled());
|
||||
|
||||
expect(feature.supportsMysqlAutoIncrement.value).toBe(true);
|
||||
expect(mysqlAutoIncrementValue.value).toBe("1");
|
||||
expect(showMysqlAutoIncrementConfirm.value).toBe(true);
|
||||
expect(mocks.ensureConnected).not.toHaveBeenCalled();
|
||||
expect(mocks.executeWithProductionGuard).not.toHaveBeenCalled();
|
||||
expect(mocks.buildMysqlAutoIncrementSql).toHaveBeenCalledWith(expect.objectContaining({ driverProfile: "mysql", schema: "sales", value: "1" }));
|
||||
});
|
||||
|
||||
it("canceling after open does not connect or execute", async () => {
|
||||
const feature = runtime();
|
||||
|
||||
feature.mysqlAutoIncrement();
|
||||
await vi.waitFor(() => expect(mocks.buildMysqlAutoIncrementSql).toHaveBeenCalledTimes(1));
|
||||
showMysqlAutoIncrementConfirm.value = false;
|
||||
|
||||
expect(mocks.ensureConnected).not.toHaveBeenCalled();
|
||||
expect(mocks.executeWithProductionGuard).not.toHaveBeenCalled();
|
||||
expect(mocks.refreshMutatedTableDataTabsForNode).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("executes the validated preview through existing guards and refreshes open data tabs", async () => {
|
||||
const feature = runtime();
|
||||
mysqlAutoIncrementValue.value = "18446744073709551615";
|
||||
mocks.buildMysqlAutoIncrementSql.mockResolvedValueOnce("ALTER TABLE `sales`.`events` AUTO_INCREMENT = 18446744073709551615;");
|
||||
await feature.refreshMysqlAutoIncrementPreviewSql();
|
||||
|
||||
await feature.confirmMysqlAutoIncrement();
|
||||
|
||||
expect(mocks.ensureConnected).toHaveBeenCalledWith("mysql-1");
|
||||
expect(mocks.executeWithProductionGuard).toHaveBeenCalledWith(tableNode(), mysqlAutoIncrementPreviewSql.value, { database: "sales", schema: undefined });
|
||||
expect(mocks.refreshMutatedTableDataTabsForNode).toHaveBeenCalledWith(tableNode());
|
||||
});
|
||||
|
||||
it("excludes MySQL-compatible profiles", () => {
|
||||
expect(runtime("mariadb").supportsMysqlAutoIncrement.value).toBe(false);
|
||||
expect(runtime("tidb").supportsMysqlAutoIncrement.value).toBe(false);
|
||||
expect(runtime("oceanbase").supportsMysqlAutoIncrement.value).toBe(false);
|
||||
expect(runtime("dolt").supportsMysqlAutoIncrement.value).toBe(false);
|
||||
expect(runtime("goldendb").supportsMysqlAutoIncrement.value).toBe(false);
|
||||
});
|
||||
|
||||
it("ignores out-of-order preview responses", async () => {
|
||||
const resolvers: Array<(sql: string) => void> = [];
|
||||
mocks.buildMysqlAutoIncrementSql.mockImplementation(() => new Promise<string>((resolve) => resolvers.push(resolve)));
|
||||
const feature = runtime();
|
||||
|
||||
mysqlAutoIncrementValue.value = "1";
|
||||
const first = feature.refreshMysqlAutoIncrementPreviewSql();
|
||||
mysqlAutoIncrementValue.value = "2";
|
||||
const second = feature.refreshMysqlAutoIncrementPreviewSql();
|
||||
resolvers[1]!("ALTER TABLE `sales`.`events` AUTO_INCREMENT = 2;");
|
||||
await second;
|
||||
resolvers[0]!("ALTER TABLE `sales`.`events` AUTO_INCREMENT = 1;");
|
||||
await first;
|
||||
|
||||
expect(mysqlAutoIncrementPreviewSql.value).toBe("ALTER TABLE `sales`.`events` AUTO_INCREMENT = 2;");
|
||||
});
|
||||
|
||||
it("rebuilds for the frozen confirmation target instead of executing another table's preview", async () => {
|
||||
const feature = runtime();
|
||||
mysqlAutoIncrementValue.value = "7";
|
||||
await feature.refreshMysqlAutoIncrementPreviewSql();
|
||||
const orders = { ...tableNode(), id: "mysql-1:sales:orders", label: "orders" };
|
||||
sidebarDangerTarget.value = orders;
|
||||
mocks.buildMysqlAutoIncrementSql.mockResolvedValueOnce("ALTER TABLE `sales`.`orders` AUTO_INCREMENT = 7;");
|
||||
|
||||
await feature.confirmMysqlAutoIncrement();
|
||||
|
||||
expect(mocks.buildMysqlAutoIncrementSql).toHaveBeenLastCalledWith(expect.objectContaining({ tableName: "orders", value: "7" }));
|
||||
expect(mocks.executeWithProductionGuard).toHaveBeenCalledWith(orders, "ALTER TABLE `sales`.`orders` AUTO_INCREMENT = 7;", { database: "sales", schema: undefined });
|
||||
});
|
||||
});
|
||||
@@ -4,9 +4,9 @@ import { useToast } from "@/composables/useToast";
|
||||
import { useConnectionStore } from "@/stores/connectionStore";
|
||||
import type { DatabaseType, TreeNode } from "@/types/database";
|
||||
import { supportsTableTruncate } from "@/lib/database/databaseCapabilities";
|
||||
import { buildDropTableSql, buildEmptyTableSql, buildTruncateTableSql, supportsDropTableCascade, supportsTruncateTableCascade, type TableAdminSqlOptions } from "@/lib/database/dbAdminSql";
|
||||
import { buildDropTableSql, buildEmptyTableSql, buildMysqlAutoIncrementSql, buildTruncateTableSql, supportsDropTableCascade, supportsNativeMysqlAutoIncrement, supportsTruncateTableCascade, type MysqlAutoIncrementSqlOptions, type TableAdminSqlOptions } from "@/lib/database/dbAdminSql";
|
||||
import { isSqlServerLinkedNode } from "@/lib/database/sqlServerLinkedServers";
|
||||
import { sidebarDangerTarget, showDropTableConfirm, showEmptyTableConfirm, showTruncateTableConfirm, dropTablePreviewSql, dropTableCascade, emptyTablePreviewSql, truncateTablePreviewSql, truncateTableCascade } from "@/components/sidebar/sidebarTreeDialogState";
|
||||
import { sidebarDangerTarget, showDropTableConfirm, showEmptyTableConfirm, showMysqlAutoIncrementConfirm, showTruncateTableConfirm, dropTablePreviewSql, dropTableCascade, emptyTablePreviewSql, mysqlAutoIncrementPreviewKey, mysqlAutoIncrementPreviewSql, mysqlAutoIncrementValue, truncateTablePreviewSql, truncateTableCascade } from "@/components/sidebar/sidebarTreeDialogState";
|
||||
|
||||
interface SidebarTableMutationRuntimeOptions {
|
||||
activeNode: ShallowRef<TreeNode>;
|
||||
@@ -28,6 +28,7 @@ export function useSidebarTableMutationRuntime(options: SidebarTableMutationRunt
|
||||
const supportsTruncate = computed(() => supportsTableTruncate(currentDatabaseType()));
|
||||
const canDropTableCascade = computed(() => activeNode.value.type === "table" && supportsDropTableCascade(currentDatabaseType()));
|
||||
const canTruncateTableCascade = computed(() => activeNode.value.type === "table" && supportsTruncateTableCascade(currentDatabaseType()));
|
||||
const supportsMysqlAutoIncrement = computed(() => activeNode.value.type === "table" && supportsNativeMysqlAutoIncrement(activeNode.value.connectionId ? connectionStore.getConfig(activeNode.value.connectionId) : undefined));
|
||||
|
||||
function tableAdminSqlOptions(optionsOverride?: { cascade?: boolean }): TableAdminSqlOptions {
|
||||
const result: TableAdminSqlOptions = {
|
||||
@@ -72,6 +73,59 @@ export function useSidebarTableMutationRuntime(options: SidebarTableMutationRunt
|
||||
truncateTablePreviewSql.value = await buildTruncateTableSql(truncateTableSqlOptions()).catch(() => "");
|
||||
}
|
||||
|
||||
function mysqlAutoIncrementSqlOptionsForNode(node: TreeNode): MysqlAutoIncrementSqlOptions {
|
||||
const config = node.connectionId ? connectionStore.getConfig(node.connectionId) : undefined;
|
||||
return {
|
||||
databaseType: config?.db_type ?? databaseTypeForNode(node) ?? "mysql",
|
||||
driverProfile: config?.driver_profile,
|
||||
schema: node.database || node.schema,
|
||||
tableName: node.label,
|
||||
value: mysqlAutoIncrementValue.value,
|
||||
};
|
||||
}
|
||||
|
||||
function mysqlAutoIncrementPreviewKeyForNode(node: TreeNode, sqlOptions = mysqlAutoIncrementSqlOptionsForNode(node)): string {
|
||||
return JSON.stringify([node.id, node.connectionId, node.database, sqlOptions.databaseType, sqlOptions.driverProfile?.trim().toLowerCase() || "", sqlOptions.schema || "", sqlOptions.tableName, sqlOptions.value]);
|
||||
}
|
||||
|
||||
async function refreshMysqlAutoIncrementPreviewSql() {
|
||||
const node = activeNode.value;
|
||||
const sqlOptions = mysqlAutoIncrementSqlOptionsForNode(node);
|
||||
const previewKey = mysqlAutoIncrementPreviewKeyForNode(node, sqlOptions);
|
||||
mysqlAutoIncrementPreviewSql.value = "";
|
||||
mysqlAutoIncrementPreviewKey.value = "";
|
||||
const sql = await buildMysqlAutoIncrementSql(sqlOptions).catch(() => "");
|
||||
if (previewKey !== mysqlAutoIncrementPreviewKeyForNode(activeNode.value)) return;
|
||||
mysqlAutoIncrementPreviewSql.value = sql;
|
||||
mysqlAutoIncrementPreviewKey.value = sql ? previewKey : "";
|
||||
}
|
||||
|
||||
function mysqlAutoIncrement() {
|
||||
if (!supportsMysqlAutoIncrement.value) return;
|
||||
mysqlAutoIncrementValue.value = "1";
|
||||
mysqlAutoIncrementPreviewKey.value = "";
|
||||
void refreshMysqlAutoIncrementPreviewSql();
|
||||
showMysqlAutoIncrementConfirm.value = true;
|
||||
}
|
||||
|
||||
async function confirmMysqlAutoIncrement() {
|
||||
const node = sidebarDangerTarget.value ?? activeNode.value;
|
||||
if (!node.connectionId || !node.database) return;
|
||||
try {
|
||||
const config = connectionStore.getConfig(node.connectionId);
|
||||
if (!supportsNativeMysqlAutoIncrement(config)) throw new Error("Setting AUTO_INCREMENT is supported only for native MySQL connections.");
|
||||
await connectionStore.ensureConnected(node.connectionId);
|
||||
const sqlOptions = mysqlAutoIncrementSqlOptionsForNode(node);
|
||||
const previewKey = mysqlAutoIncrementPreviewKeyForNode(node, sqlOptions);
|
||||
const sql = mysqlAutoIncrementPreviewKey.value === previewKey && mysqlAutoIncrementPreviewSql.value ? mysqlAutoIncrementPreviewSql.value : await buildMysqlAutoIncrementSql(sqlOptions);
|
||||
await options.executeWithProductionGuard(node, sql, { database: node.database, schema: node.schema });
|
||||
toast(t("contextMenu.mysqlAutoIncrementSuccess", { name: node.label, value: mysqlAutoIncrementValue.value }), 3000);
|
||||
await options.refreshMutatedTableDataTabsForNode(node);
|
||||
} catch (error: any) {
|
||||
toast(t("contextMenu.tableOperationFailed", { message: error?.message || String(error) }), 5000);
|
||||
}
|
||||
}
|
||||
|
||||
function dropTable() {
|
||||
dropTableCascade.value = false;
|
||||
void refreshDropTablePreviewSql();
|
||||
@@ -144,6 +198,7 @@ export function useSidebarTableMutationRuntime(options: SidebarTableMutationRunt
|
||||
supportsTruncate,
|
||||
canDropTableCascade,
|
||||
canTruncateTableCascade,
|
||||
supportsMysqlAutoIncrement,
|
||||
refreshDropTablePreviewSql,
|
||||
refreshTruncateTablePreviewSql,
|
||||
dropTable,
|
||||
@@ -153,5 +208,8 @@ export function useSidebarTableMutationRuntime(options: SidebarTableMutationRunt
|
||||
confirmEmptyTable,
|
||||
truncateTable,
|
||||
confirmTruncateTable,
|
||||
mysqlAutoIncrement,
|
||||
refreshMysqlAutoIncrementPreviewSql,
|
||||
confirmMysqlAutoIncrement,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user