Skip to content

FTS5 integrity-check corrupts D1 shadow tables — publish fails with SQLITE_CORRUPT_VTAB on all collections #252

Closed

Description

@topgoldforum

Description

Description

Publishing any content (posts, pages, custom collections) fails with 500 on Cloudflare D1. Saving drafts works fine. Affects every collection that has search enabled.

Error

Content publish error: Error: D1_ERROR: database disk image is malformed: SQLITE_CORRUPT (extended: SQLITE_CORRUPT_VTAB)

Root cause identified

The bug is in FTSManager.verifyAndRepairIndex() in packages/core/src/search/fts-manager.ts.

This line runs on every Worker startup:
INSERT INTO "ftsTable"("{ftsTable}"("
ftsTable"("{ftsTable}") VALUES('integrity-check')

This FTS5 integrity-check command corrupts D1's FTS5 shadow tables. After corruption, the FTS triggers fire on every content update and cause publish to fail.

Workaround

Drop all FTS tables and triggers, then set search_config to disabled:

UPDATE _emdash_collections SET search_config = '{"enabled":false}' WHERE search_config IS NOT NULL;

Then drop all _emdash_fts_* tables and triggers manually via D1 console.

Suggested fix

Skip the integrity-check INSERT on D1, or catch the corruption and drop/rebuild rather than leaving tables in a corrupted state.

Environment

Steps to reproduce

Steps to reproduce

  1. Deploy EmDash to Cloudflare Workers with D1 database
  2. Run setup wizard and create admin account
  3. Create any collection with at least one searchable field
  4. Create a content item in that collection and save as draft — works fine
  5. Click Publish — fails with 500
  6. Check Cloudflare Observability logs — shows SQLITE_CORRUPT_VTAB
  7. Repeat for any collection (posts, pages, videos, custom) — all fail

Additional notes

  • The corruption happens on every Worker cold start because verifyAndRepairIndex() runs at startup
  • The integrity-check INSERT leaves the FTS5 shadow tables in a corrupted state
  • The corrupted triggers then fire on every content UPDATE, causing publish to fail
  • Dropping the FTS tables and triggers manually restores publish functionality
  • The issue recurs whenever EmDash recreates FTS tables for a new collection
  • This affects ALL content publishing on D1 — making the CMS unusable for production on Cloudflare without the manual workaround

Environment

Environment

  • emdash: 0.1.0
  • @emdash-cms/cloudflare: 0.1.0
  • @emdash-cms/plugin-audit-log: 0.1.0
  • @emdash-cms/plugin-forms: 0.1.0
  • Cloudflare Workers (paid plan)
  • Cloudflare D1 database
  • Cloudflare R2 storage
  • Cloudflare Worker Loader (Dynamic Workers)
  • Wrangler: 4.80.0
  • Node.js: 22.16.0
  • Bun: 1.2.15

Logs / error output

## Logs / error output
From Cloudflare Observability:

Content publish error: Error: D1_ERROR: database disk image is malformed: SQLITE_CORRUPT (extended: SQLITE_CORRUPT_VTAB)

Worker startup logs (repeating on every cold start):
FTS index for "pages" has 2 rows but content table has 1. Rebuilding.
Repaired 1 corrupted FTS index(es) at startup

Publish API call:
POST /_emdash/api/content/posts/{id}/publish → 500
POST /_emdash/api/content/pages/{id}/publish → 500
POST /_emdash/api/content/videos/{id}/publish → 500

Browser console:
Failed to load resource: the server responded with a status of 500
/_emdash/api/content/posts/{id}/publish

Activity

  1. added
    bug
    on Apr 4, 2026
  2. added a commit that references this issue on Apr 6, 2026
    29350d3
  3. mvanhorn commented on Apr 6, 2026

    @mvanhorn
    Contributor

    Implemented in #313 - removes the FTS5 integrity-check INSERT that corrupts D1 shadow tables. Row-count mismatch detection is kept as a safe alternative.

  4. added a commit that references this issue on Apr 7, 2026
    1bcfc50
  5. added a commit that references this issue on Apr 13, 2026
    46b50d3
  6. DavidPivert commented on May 18, 2026

    @DavidPivert
    Contributor

    Heads-up for anyone debugging a similar symptom on emdash ≥ 0.12 (where the PR #313 fix is shipped):

    The startup verifyAndRepairIndex() is fixed, but the INSERT INTO ftsTable(ftsTable) VALUES('integrity-check') command itself is still dangerous on D1 if you run it manually (e.g. while diagnosing FTS issues).

    I hit this on emdash 0.12.0 on Cloudflare D1:

    • After enabling i18n config (defineConfig({ i18n: {...} })), publishing an existing FR variant of a translated page started failing with SQLITE_CORRUPT_VTAB
    • The original cause was probably an unrelated FTS desync from the i18n migration's table rebuild (the ec_* tables get recreated by 019_i18n, but the FTS shadow tables aren't rebuilt at the same time)
    • Trying to diagnose with INSERT INTO _emdash_fts_pages(_emdash_fts_pages) VALUES('integrity-check') returned success: true but silently corrupted the shadow tables — every subsequent UPDATE on ec_pages then failed with SQLITE_CORRUPT_VTAB
    • A clean rebuild without any integrity-check call (DROP TABLE + CREATE VIRTUAL TABLE + INSERT SELECT + recreate triggers) restored a working state

    Two suggestions for the docs / repo:

    1. Could migrations/019_i18n.ts also rebuild the FTS shadow tables when it rebuilds the content tables? Currently they end up referencing rowids from a dropped table.
    2. A note somewhere in the troubleshooting docs telling users not to run INSERT INTO fts(fts) VALUES('integrity-check') manually on D1 would have saved me a couple of hours.

    Happy to file a fresh issue for either if it would be more useful.

  7. ascorbic commented on May 19, 2026

    @ascorbic
    Member

    #768 will be going out in a release today which should fix this properly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions