Skip to content

Fix D1 migration script statement parsing and idempotency#5

Merged
gcarvelli merged 1 commit intomainfrom
gcarvelli/fix-d1-migration
Feb 6, 2026
Merged

Fix D1 migration script statement parsing and idempotency#5
gcarvelli merged 1 commit intomainfrom
gcarvelli/fix-d1-migration

Conversation

@gcarvelli
Copy link

Fixes two issues in the migration runner that were preventing D1 database migrations from completing:

  1. Statement parsing bug: The comment filter was discarding entire statement chunks that started with --, including the CREATE TABLE recordings statement which was preceded by header comments. This caused all dependent tables to fail due to missing foreign key references.

  2. Idempotency: The INSERT into _migrations wasn't idempotent, causing unique constraint errors when migrations were re-applied.

The fixes make both the statement parsing robust to comments and the migration recording safe for re-application.

- Fix comment filtering to strip SQL comment lines from within statements
  instead of discarding entire chunks that start with '--'. This was causing
  the CREATE TABLE recordings statement to be filtered out when preceded by
  header comments, breaking foreign key references.
- Make migration recording idempotent with INSERT OR IGNORE so migrations
  can be safely re-applied without unique constraint errors.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@gcarvelli gcarvelli merged commit 8373698 into main Feb 6, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant