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,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 });
}