feat(mysql): edit table auto-increment counter

Closes #7065
This commit is contained in:
t8y2 committed 2026-08-25 01:24:09 +00:00
1 parent 44dc2a88f8
commit 277386ec81
13 files changed
+513 -32

No files matched your search

@@ -0,0 +1,94 @@
import { describe, expect, it, vi } from "vitest";
import { buildMysqlAutoIncrementCounterStatement, canEditMysqlAutoIncrementCounter, mysqlAutoIncrementCounterDraft, refreshMysqlAutoIncrementCounterDraft } from "@/lib/table/mysqlAutoIncrementCounter";
function existingColumn(autoIncrement: boolean) {
return {
original: { name: "id" },
markedForDrop: false,
extra: { autoIncrement },
} as any;
}
describe("MySQL table AUTO_INCREMENT counter", () => {
it("keeps present and NULL metadata distinct without numeric conversion", () => {
expect(mysqlAutoIncrementCounterDraft("9007199254740993")).toEqual({
value: "9007199254740993",
originalValue: "9007199254740993",
});
expect(mysqlAutoIncrementCounterDraft("18446744073709551615")).toEqual({
value: "18446744073709551615",
originalValue: "18446744073709551615",
});
expect(mysqlAutoIncrementCounterDraft(null)).toEqual({ value: undefined, originalValue: undefined });
});
it("refreshes restored clean drafts and rebases restored dirty drafts on live metadata", () => {
expect(refreshMysqlAutoIncrementCounterDraft("30", { value: "10", originalValue: "10" }, true)).toEqual({
value: "30",
originalValue: "30",
});
expect(refreshMysqlAutoIncrementCounterDraft("15", { value: "20", originalValue: "10" }, true)).toEqual({
value: "20",
originalValue: "15",
});
expect(refreshMysqlAutoIncrementCounterDraft(null, { value: "20", originalValue: "10" }, true)).toEqual({
value: "20",
originalValue: undefined,
});
});
it("is editable only for existing native-MySQL tables with an active existing auto-increment column", () => {
const nativeMysql = { db_type: "mysql", driver_profile: "mysql" } as any;
expect(canEditMysqlAutoIncrementCounter(nativeMysql, false, [existingColumn(true)])).toBe(true);
expect(canEditMysqlAutoIncrementCounter(nativeMysql, true, [existingColumn(true)])).toBe(false);
expect(canEditMysqlAutoIncrementCounter(nativeMysql, false, [existingColumn(false)])).toBe(false);
expect(canEditMysqlAutoIncrementCounter(nativeMysql, false, [{ ...existingColumn(true), markedForDrop: true }])).toBe(false);
for (const connection of [
{ db_type: "jdbc", driver_profile: "mysql" },
{ db_type: "mysql", driver_profile: "mariadb" },
{ db_type: "mysql", driver_profile: "tidb" },
{ db_type: "mysql", driver_profile: "oceanbase" },
{ db_type: "mysql", driver_profile: "dolt" },
{ db_type: "goldendb", driver_profile: "goldendb" },
] as const) {
expect(canEditMysqlAutoIncrementCounter(connection as any, false, [existingColumn(true)])).toBe(false);
}
});
it("emits no DDL when unchanged and delegates changed values to the existing builder", async () => {
const buildSql = vi.fn(async ({ value }: { value: string }) => `ALTER TABLE \`sales\`.\`events\` AUTO_INCREMENT = ${value};`);
const common = {
enabled: true,
originalValue: "1",
databaseType: "mysql" as const,
driverProfile: "mysql",
schema: "sales",
tableName: "events",
buildSql,
};
await expect(buildMysqlAutoIncrementCounterStatement({ ...common, value: "1" })).resolves.toBeUndefined();
expect(buildSql).not.toHaveBeenCalled();
await expect(buildMysqlAutoIncrementCounterStatement({ ...common, value: "18446744073709551615" })).resolves.toContain("18446744073709551615");
expect(buildSql).toHaveBeenCalledWith(expect.objectContaining({ value: "18446744073709551615" }));
});
it("does not emit when metadata is unavailable and preserves builder validation errors", async () => {
const buildSql = vi.fn(async () => {
throw new Error("AUTO_INCREMENT must be a decimal integer");
});
const common = {
enabled: true,
databaseType: "mysql" as const,
driverProfile: "mysql",
schema: "sales",
tableName: "events",
buildSql,
};
await expect(buildMysqlAutoIncrementCounterStatement({ ...common, originalValue: undefined, value: undefined })).resolves.toBeUndefined();
await expect(buildMysqlAutoIncrementCounterStatement({ ...common, originalValue: "1", value: "1e3" })).rejects.toThrow("AUTO_INCREMENT must be a decimal integer");
});
});
+1
View File
@@ -161,6 +161,7 @@ export const listSchemas = forward("listSchemas");
export const listSchemaInfos = forward("listSchemaInfos");
export const listTables = forward("listTables");
export const getTableComment = forward("getTableComment");
export const getMysqlTableAutoIncrement = forward("getMysqlTableAutoIncrement");
export const listObjects = forward("listObjects");
export const listObjectStatistics = forward("listObjectStatistics");
export const listCompletionObjects = forward("listCompletionObjects");
+4
View File
@@ -831,6 +831,10 @@ export async function getTableComment(_connectionId: string, _database: string,
throw new Error("Table comment lookup is not available in the web backend");
}
export async function getMysqlTableAutoIncrement(_connectionId: string, _database: string, _table: string): Promise<string | null> {
throw new Error("MySQL AUTO_INCREMENT metadata is not available in the web backend");
}
export async function listObjects(connectionId: string, database: string, schema: string, objectTypes?: (SidebarObjectKind | "EVENT")[], filter?: string, limit?: number, offset?: number, catalog?: string, tableNameFilter?: TableNameFilter): Promise<ObjectInfo[]> {
return get(
`/api/schema/objects?${qs({
+4
View File
@@ -1109,6 +1109,10 @@ export async function getTableComment(connectionId: string, database: string, sc
});
}
export async function getMysqlTableAutoIncrement(connectionId: string, database: string, table: string): Promise<string | null> {
return invoke("get_mysql_table_auto_increment", { connectionId, database, table });
}
export async function listObjects(connectionId: string, database: string, schema: string, objectTypes?: (SidebarObjectKind | "EVENT")[], filter?: string, limit?: number, offset?: number, catalog?: string, tableNameFilter?: import("@/types/database").TableNameFilter): Promise<ObjectInfo[]> {
return invoke("list_objects", {
connectionId,
@@ -0,0 +1,40 @@
import type { ConnectionConfig, DatabaseType } from "@/types/database";
import { supportsNativeMysqlAutoIncrement, type MysqlAutoIncrementSqlOptions } from "@/lib/database/dbAdminSql";
import type { EditableStructureColumn } from "@/lib/table/tableStructureEditorSql";
export interface MysqlAutoIncrementCounterDraft {
value: string | undefined;
originalValue: string | undefined;
}
export function mysqlAutoIncrementCounterDraft(value: string | null): MysqlAutoIncrementCounterDraft {
const current = value ?? undefined;
return { value: current, originalValue: current };
}
export function refreshMysqlAutoIncrementCounterDraft(serverValue: string | null, current: MysqlAutoIncrementCounterDraft, preserveDraft: boolean): MysqlAutoIncrementCounterDraft {
const server = mysqlAutoIncrementCounterDraft(serverValue);
if (!preserveDraft || current.value === current.originalValue) return server;
return { value: current.value, originalValue: server.originalValue };
}
export function canEditMysqlAutoIncrementCounter(connection: Pick<ConnectionConfig, "db_type" | "driver_profile"> | undefined, isCreateMode: boolean, columns: readonly EditableStructureColumn[]): boolean {
if (isCreateMode || !supportsNativeMysqlAutoIncrement(connection)) return false;
return columns.some((column) => !!column.original && !column.markedForDrop && column.extra.autoIncrement === true);
}
export interface BuildMysqlAutoIncrementCounterStatementOptions extends Omit<MysqlAutoIncrementSqlOptions, "databaseType" | "value"> {
enabled: boolean;
databaseType: DatabaseType | undefined;
value: string | undefined;
originalValue: string | undefined;
buildSql: (options: MysqlAutoIncrementSqlOptions) => Promise<string>;
}
export async function buildMysqlAutoIncrementCounterStatement({ enabled, originalValue, buildSql, ...options }: BuildMysqlAutoIncrementCounterStatementOptions): Promise<string | undefined> {
const { databaseType, value } = options;
if (!enabled || databaseType === undefined || originalValue === undefined || value === undefined || value === originalValue) {
return undefined;
}
return buildSql({ ...options, databaseType, value });
}