Skip to content

Local D1 migrations skipped on restart — tables missing in Miniflare #1362

@nshen

Description

@nshen

Description

When running alchemy dev, the local D1 database (Miniflare) loses all data on each restart because it runs in-memory. However, Alchemy skips re-applying migrations on subsequent starts ([skipped] DB Skipped Resource (no changes)), resulting in no such table errors.

Steps to Reproduce

  1. Create a project with D1Database and migrationsDir
  2. Run alchemy dev --stage dev — first run applies migrations correctly
  3. Stop the dev server (Ctrl+C)
  4. Run alchemy dev --stage dev again
  5. D1 resource is skipped: [skipped] DB Skipped Resource (no changes)
  6. Any query to D1 fails with D1_ERROR: no such table: <table_name>: SQLITE_ERROR

Expected Behavior

Local D1 migrations should be re-applied on every alchemy dev restart, since Miniflare D1 is in-memory and doesn't persist data between restarts.

Additional Context

  • Alchemy version: 0.82.2
  • The d1-local-migrations.js logic works correctly when it runs — the issue is that the D1Database resource is skipped entirely due to no changes detection.
  • d1Persist: true is set in miniflare-controller.js, but no sqlite files are created under .alchemy/miniflare/v3/d1/ — only KV has sqlite files.
  • applyLocalD1Migrations creates a separate Miniflare instance to run migrations, then dispose()s it. Even if migrations ran, the data wouldn't be available to the Worker's Miniflare instance since there's no shared sqlite file on disk.
  • Using CloudflareStateStore for state (not local filesystem), which may contribute to the skip logic not detecting that local data was lost.

Workaround

Using dev: { remote: true } on D1Database to connect to the deployed D1 instance during local development.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions