fix(sqlite): convert source_id raw SQL to Drizzle sync helpers#2634
Conversation
Fresh SQLite installs crashed with `SqliteError: no such column: source_id` whenever a call reached `getTelemetryByNodeAveraged`, `purgeChannelMessages`, or `purgeDirectMessages` with a `sourceId` argument — which happens on any 4.0 alpha install that auto-creates a default source. The SQLite branches in `DatabaseService` were hand-rolled raw SQL that referenced `source_id`, but the schema column is `sourceId`. Rather than re-rename the raw SQL, convert the three sites to dispatch through Drizzle-backed sync helpers on the repositories so column names come from the schema and the drift can't recur: - messages.ts: new `purgeChannelMessagesSqlite()` and `purgeDirectMessagesSqlite()` using Drizzle delete/where/run - telemetry.ts: new `getTelemetryByNodeAveragedSqlite()` using Drizzle select/groupBy with `sql` templates for CAST-based bucketing - services/database.ts: SQLite branches in purge+averaged methods now call the new helpers; removed ~140 lines of raw SQL Regression tests (11 new): - telemetry.extra.test.ts: 5 tests exercising sourceId scoping (crash regression, per-source filtering, unscoped fetch, raw-type scoping, sinceTimestamp interaction) - messages.purge.test.ts: 6 tests covering both purge sync helpers (crash regression, per-source isolation, broadcast exclusion) Fixes #2631 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Claude finished @Yeraze's task —— View job PR ReviewThis PR effectively addresses issue #2631 by converting problematic raw SQL queries to type-safe Drizzle helpers. The solution is architecturally sound and includes comprehensive regression testing. Todo List
✅ StrengthsExcellent architectural approach: Instead of just fixing the column name in raw SQL, this PR properly moves the logic into repository layers where it belongs, following established patterns. Type safety improvement: Converting from raw SQL to Drizzle query builders provides compile-time safety and prevents the schema/query drift that caused the original issue. Comprehensive test coverage: 11 new regression tests specifically target the crash scenario and edge cases around Clean implementation:
|
System Test ResultsMeshMonitor System Test ResultsTest Run: 2026-04-11 19:56:41 EDT Test Summary
✅ Overall Result: PASSEDAll deployment configurations are working correctly! Test DetailsConfiguration Import:
Quick Start Test:
Security Test:
V1 API Test:
Reverse Proxy Test:
Reverse Proxy + OIDC Test:
Virtual Node CLI Test:
Backup & Restore Test:
Database Migration Test:
DB Backing Consistency Test:
|
Summary
Fresh 4.0 alpha SQLite installs crashed with
SqliteError: no such column: source_idwhenever a call reachedgetTelemetryByNodeAveraged,purgeChannelMessages, orpurgeDirectMessageswith asourceIdargument — which happens on every install that auto-creates a default source. Three raw SQL sites inDatabaseServicereferenced the column assource_idwhile the SQLite schema defines it assourceId(camelCase, matching Postgres/MySQL via Drizzle).Rather than re-rename the raw SQL, this PR converts the three sites to dispatch through new Drizzle-backed sync helpers on the repositories. Column names now come from the schema, so the snake_case/camelCase drift that caused #2631 cannot recur.
Changes
src/db/repositories/messages.ts— New sync helperspurgeChannelMessagesSqlite()andpurgeDirectMessagesSqlite()using Drizzledelete().where().run()src/db/repositories/telemetry.ts— New sync helpergetTelemetryByNodeAveragedSqlite()using Drizzleselect().groupBy()withsqltemplates for theCAST((timestamp / ?) * ? AS INTEGER)time-bucket expressionsrc/services/database.ts— SQLite branches inpurgeChannelMessages,purgeDirectMessages, andgetTelemetryByNodeAveragednow dispatch to the new sync helpers. Removed ~140 lines of raw SQLsrc/db/repositories/telemetry.extra.test.ts— 5 new regression tests exercisingsourceIdscoping (crash regression, per-source filtering, unscoped fetch, raw-type scoping,sinceTimestampinteraction)src/db/repositories/messages.purge.test.ts— New file with 6 tests for both purge sync helpers (crash regression, per-source isolation, broadcast!ffffffffexclusion)CHANGELOG.md— Bug fix entry under the 4.0 multi-source sectionIssues Resolved
Fixes #2631
Documentation Updates
CHANGELOG.mdupdated with a bug fix entry under the Unreleased 4.0 multi-source architecture section. No user-facing docs affected.Testing
tsc --noEmit)🤖 Generated with Claude Code