Sqlite fts5 corruption rebuild

From divinevideo/divine-mobile · View interactive version

Sqlite fts5 corruption rebuild is an AI agent skill from the GitHub repository divinevideo/divine-mobile. Fix SQLite "database disk image is malformed" errors caused by corrupted FTS5 indexes. Use when: (1) UPDATE/INSERT fails with "stepping, database disk image is malformed" but PRAGMA integrity_check returns "ok", (2) Only specific records fail to update while others succeed, (3) Records contain dirty

How to use this skill

Skill file

SQLite FTS5 Index Corruption from Dirty Data

Problem

SQLite returns "database disk image is malformed" (error code 11) when trying to

UPDATE or INSERT records, even though PRAGMA integrity_check reports the database

as "ok". The error only affects specific records, not all writes.

Context / Trigger Conditions

control characters in columns that are indexed by FTS5

Solution

  1. Identify FTS5 tables linked to the affected table:

```sql

SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '%fts%';

```

  1. Stop any processes holding the database open (the rebuild requires exclusive access):

```bash

kill $(lsof -ti :PORT) # Kill the server process

```

  1. Rebuild the FTS5 index:

```sql

INSERT INTO table_fts(table_fts) VALUES('rebuild');

```

Replace table_fts with your actual FTS5 table name (e.g., people_fts).

  1. Retry the failed operation - it should now succeed.
  1. Restart your application server.

Verification

Example

# Error occurs:
sqlite3 data/tracker.db "UPDATE people SET name = 'More' WHERE id = 11;"
# Error: stepping, database disk image is malformed (11)

# But integrity check passes:
sqlite3 data/tracker.db "PRAGMA integrity_check;"
# ok

# And SELECT works fine:
sqlite3 data/tracker.db "SELECT id, name FROM people WHERE id = 11;"
# 11|More Masajes\t\n\t\tEscort 23 aƱos...

# Fix: Stop server, rebuild FTS5 index
kill $(lsof -ti :3001)
sqlite3 data/tracker.db "INSERT INTO people_fts(people_fts) VALUES('rebuild');"

# Now the update works:
sqlite3 data/tracker.db "UPDATE people SET name = 'More' WHERE id = 11;"
# Success!

Prevention

```javascript

const cleanName = rawName.replace(/[\t\n\r]+/g, ' ').trim();

```

independently without affecting the main table

Notes

making this hard to diagnose

not the main table, which is why integrity_check passes

the database open (check with lsof or fuser)

(e.g., via sqlite3 CLI) while a WAL-mode connection is active

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.