[drizzle-kit]: fix Cloudflare D1 data loss on table recreation for tables with FK onDelete cascade #5074
+451
−31
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes #4938
The main problem is that Cloudflare D1 seems to ignore
PRAGMA foreign_keys=OFF/ON;for migrations like this where you recreate a table and some other tables has a FKonDelete: "cascade"on the table that gets recreated. This causes full data loss on the dependent tables, and this PR fixes that by creating backup tables for the FL dependent tables. This bug currently blocks a better-auth update from 1.3.7 to anything higher.I wouldn't mind putting this behind a config setting since this is only a problem with specific sqlite implementations, but I feel like most people would miss that and lose their data...
I'm also not 100% sure if I caught all cases, but I've added tests to validate the fix and ran the generate command locally with the following schema to validate the fix: