Skip to content

feat: auto-detect SQLite schema changes from external tools (#704) - #719

Merged
datlechin merged 1 commit into
mainfrom
feat/sqlite-auto-refresh
Apr 13, 2026
Merged

datlechin merged 1 commit into
mainfrom
feat/sqlite-auto-refresh

Conversation

@datlechin

Copy link
Copy Markdown
Member

Summary

SQLite databases modified by external tools (migration runners, CLI, other clients) now auto-refresh the sidebar and autocomplete cache without requiring manual Cmd+R.

Closes #704

Architecture

Two-layer detection using native macOS DispatchSource:

  1. File watcher (DatabaseFileWatcher) — monitors the .sqlite file via DispatchSource.makeFileSystemObjectSource with O_EVTONLY. Re-creates the file descriptor after each event to handle SQLite journaling (which invalidates the original inode via rename operations). 500ms debounce coalesces rapid writes.

  2. Window focus fallback — when a main window regains key status for a file-based connection, refreshTablesIfStale() runs if not recently refreshed (2-second dedup). Catches edge cases the file watcher might miss.

Both layers call the existing refreshTables() which updates sidebar + autocomplete cache.

Root cause of the original bug

refreshTables() was only called on Cmd+R (manual) or on initial connection. No mechanism existed to trigger it when the SQLite file was modified externally.

Root cause of the fd invalidation

SQLite journaling renames the database file during commits. The DispatchSource's file descriptor then points to the old inode, not the file at that path. Fix: re-open the fd after every event.

Changes

File Change
DatabaseFileWatcher.swift New — DispatchSource file watcher with fd re-creation and debounce
MainContentCoordinator.swift Start/stop watcher on lifecycle, refreshTablesIfStale() with 2s dedup
MainContentView.swift Refresh on window focus for file-based connections

Test plan

  • Open SQLite database in TablePro
  • In terminal: sqlite3 path/to/db "CREATE TABLE test (id INTEGER PRIMARY KEY)" → sidebar shows test within ~1 second
  • In terminal: sqlite3 path/to/db "DROP TABLE test" → sidebar removes test within ~1 second
  • In terminal: sqlite3 path/to/db "ALTER TABLE users ADD COLUMN phone TEXT" → click table, new column visible
  • Switch away from TablePro, run migration, switch back → sidebar updates on focus
  • Verify no CPU spike when idle with SQLite connection open
  • MySQL/PostgreSQL connections unaffected (no file watcher started)

@datlechin
datlechin merged commit 2de90a5 into main Apr 13, 2026
2 checks passed
@datlechin
datlechin deleted the feat/sqlite-auto-refresh branch April 13, 2026 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQLite: schema changes from external migrations not detected until app restart

1 participant