Skip to content

Commit 737d324

Browse files
committed
no-mistakes(review): Guard D1 migrate path against primary-key moves (#258)
1 parent 272ee9d commit 737d324

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

  • server/typescript/packages/cli/src/commands

‎server/typescript/packages/cli/src/commands/migrate.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ async function runD1Migrate(
930930
config: ResolvedMigrateConfig,
931931
metaRoot: string,
932932
runner: WranglerRunner,
933-
_fmt: OutputFormat = "text",
933+
fmt: OutputFormat = "text",
934934
): Promise<number> {
935935
// 1. Resolve wrangler.toml + binding.
936936
const wranglerConfigPath = config.d1.wranglerConfigPath
@@ -1030,6 +1030,10 @@ async function runD1Migrate(
10301030
// @constraintName models churning and enum @values changes silent on D1.
10311031
dialect: "d1",
10321032
allow: tokensToAllowOptions(config.allow),
1033+
// #258 — adopting a live D1 DB whose PRIMARY KEY differs from the metadata identity
1034+
// has no expressible migration; refuse loudly instead of emitting SQL that drops
1035+
// the constraint and breaks referencing FKs at apply (same failure as the online path).
1036+
refusePrimaryKeyChange: true,
10331037
// #208 §7 — declared-@unmanaged objects are external (see the online path above).
10341038
unmanagedNames: collectUnmanagedNames(metadata),
10351039
onAmbiguous: async (a) => {
@@ -1038,6 +1042,12 @@ async function runD1Migrate(
10381042
},
10391043
});
10401044
} catch (err) {
1045+
// #258 — a primary-key move has no expressible migration; refuse loudly.
1046+
if (err instanceof PrimaryKeyChangeError) {
1047+
log.error(`migrate: ${err.message}`);
1048+
emitStructuredError(`migrate: ${err.message}`, "align the primary key manually, or reconcile the metadata identity to match the live table", fmt);
1049+
return 1;
1050+
}
10411051
if ((err as Error).message.includes("aborted by onAmbiguous")) {
10421052
const entries = ambiguousToEntries(collectedAmbiguous);
10431053
for (const e of entries) {

0 commit comments

Comments
 (0)