From 75f26d056de6c180d34c5501bf051aed50466ccc Mon Sep 17 00:00:00 2001 From: devlevic Date: Thu, 23 Jan 2025 14:20:18 -0300 Subject: [PATCH] Reorder jsonStatements to push drop references before dropping the table. When generating a DROP TABLE SQL with a DROP FK statement, the FK statement is generated after the DROP TABLE, causing a broken migration SQL. --- drizzle-kit/src/snapshotsDiffer.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drizzle-kit/src/snapshotsDiffer.ts b/drizzle-kit/src/snapshotsDiffer.ts index 0fd803288..dbade0ec4 100644 --- a/drizzle-kit/src/snapshotsDiffer.ts +++ b/drizzle-kit/src/snapshotsDiffer.ts @@ -1969,6 +1969,10 @@ export const applyPgSnapshotsDiff = async ( jsonStatements.push(...renameViews); jsonStatements.push(...alterViews); + // It needs to be before jsonDropTables, in case you're about to drop + // a table that is referenced by another one + jsonStatements.push(...jsonDroppedReferencesForAlteredTables); + jsonStatements.push(...jsonDropTables); jsonStatements.push(...jsonSetTableSchemas); jsonStatements.push(...jsonRenameTables); @@ -1977,8 +1981,6 @@ export const applyPgSnapshotsDiff = async ( jsonStatements.push(...jsonDeletedUniqueConstraints); jsonStatements.push(...jsonDeletedCheckConstraints); - jsonStatements.push(...jsonDroppedReferencesForAlteredTables); - // Will need to drop indexes before changing any columns in table // Then should go column alternations and then index creation jsonStatements.push(...jsonDropIndexesForAllAlteredTables);