fix(db): name the custom widget secret foreign key within mysql limits

drizzle derives `custom_widget_secret_definition_id_custom_widget_definition_id_fk`,
which is 65 characters. MySQL caps identifiers at 64 and rejects it with
error 1059, so migration 0040 created the key under a shortened name. The
schema never declared that name, so every `migration:mysql:generate` run
re-proposed dropping and recreating the key under the long one, producing a
migration that cannot be applied.

Declaring the shortened name explicitly makes the schema match what is
actually in the database. Only the mysql schema changes; sqlite and
postgresql keep their derived names and need no migration.

The parity test matched keys across dialects by generated name, which no
longer holds, so it now matches on what a key links - its columns, target
table and target columns - and compares the referential actions. That also
means it reads keys through getTableConfig and so covers table-level
declarations, not just inline .references() ones.

Adds a guard test asserting no mysql table, foreign key or index name
exceeds 64 characters, so the next long pairing fails in CI instead of
producing an unappliable migration.
This commit is contained in:
Thomas Camlong committed 2026-08-07 18:14:23 +02:00
1 parent 7276bdad21
commit 09c56002d6
2 files changed
+68 -35

No files matched your search

+10 -3
View File
@@ -6,6 +6,7 @@ import type { AnyMySqlColumn } from "drizzle-orm/mysql-core";
import {
boolean,
customType,
foreignKey,
index,
int,
mediumtext,
@@ -597,14 +598,20 @@ export const customWidgetSecrets = mysqlTable(
updatedAt: timestamp()
.$onUpdateFn(() => new Date())
.notNull(),
definitionId: varchar({ length: 64 })
.notNull()
.references(() => customWidgetDefinitions.id, { onDelete: "cascade" }),
definitionId: varchar({ length: 64 }).notNull(),
},
(table) => ({
compoundKey: primaryKey({
columns: [table.definitionId, table.kind],
}),
// The name drizzle derives here is 65 characters, one over MySQL's 64-character identifier
// limit, so migration 0040 created the key under this shortened name. Declaring it explicitly
// keeps `drizzle-kit generate` from re-proposing an ALTER that MySQL rejects with error 1059.
definitionReference: foreignKey({
name: "cw_secret_definition_id_cw_definition_id_fk",
columns: [table.definitionId],
foreignColumns: [customWidgetDefinitions.id],
}).onDelete("cascade"),
}),
);