feat(db): add schema repair and sentry cli fix command#197
Conversation
Add automatic schema repair for SQLite database migration issues. When a query fails due to missing columns or tables, the CLI now automatically repairs the schema and retries the operation. Also adds a manual `sentry cli fix` command for diagnostics and repair: - `sentry cli fix` - diagnose and repair schema issues - `sentry cli fix --dry-run` - show what would be fixed Auto-repair can be disabled via SENTRY_CLI_NO_AUTO_REPAIR=1 env var.
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨Dsn
Other
Bug Fixes 🐛
Documentation 📚
Internal Changes 🔧
Other
🤖 This preview updates automatically when you update the PR. |
Codecov Results 📊✅ Patch coverage is 91.01%. Project has 1744 uncovered lines. Files with missing lines (34)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 77.20% 77.99% +0.79%
==========================================
Files 57 58 +1
Lines 7497 7922 +425
Branches 0 0 —
==========================================
+ Hits 5788 6178 +390
- Misses 1709 1744 +35
- Partials 0 0 —Generated by Codecov Action |
- Refactor EXPECTED_TABLES to generate DDL from TABLE_SCHEMAS (single source of truth) - Move tryRepairAndRetry from telemetry.ts to schema.ts where repair logic belongs - Add addedInVersion tracking to columns for automatic EXPECTED_COLUMNS generation - Remove ~80 lines of duplicate code from telemetry.ts - Fix linting errors (block statements) and type error (EXPECTED_TABLES access)
|
Regarding why migrations are still needed alongside auto-repair: Auto-repair only handles additive schema changes (missing tables/columns). Explicit migrations are still needed for:
Auto-repair is a safety net for when schema version gets out of sync (e.g., database copied between machines, downgrade then upgrade), not a replacement for migrations. |
- Export TABLE_SCHEMAS, generateTableDDL, generatePreMigrationTableDDL from schema.ts - Update fix.test.ts to use helpers instead of hardcoded SQL - Update schema.test.ts to use helpers instead of hardcoded SQL - Tests now stay in sync with schema changes automatically
- Fix auto-repair swallowing retry errors by moving operation() call outside try-catch, so new errors propagate instead of original error - Reset rawDb to null in getDatabase() auto-invalidate block for consistency - Add validation in generatePreMigrationTableDDL for empty base columns
- Use RepairAttemptResult type to distinguish 'repair not attempted' from 'repair succeeded with undefined result' (fixes incorrect error throwing when stmt.get() legitimately returns undefined) - Wrap database initialization in try-catch to prevent connection leak if initSchema/runMigrations/migrateFromJson fails after Database() opens
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
Summary
Adds automatic schema repair for SQLite database migration issues. This fixes cases where the schema version gets out of sync with actual table structure during development.
Changes
Auto-Repair on SQLiteError
db.query()) and execution (stmt.get()) stagesSENTRY_CLI_NO_AUTO_REPAIR=1sentry cli fixCommandSchema Repair Utilities
EXPECTED_TABLES/EXPECTED_COLUMNS- Canonical schema definitionsgetSchemaIssues()- Diagnostics for missing tables/columnsrepairSchema()- Non-destructive repair (only adds, never deletes)Testing