Skip to content

refactor(db): DRY up database layer with shared helpers and lint enforcement#550

Merged
BYK merged 1 commit intomainfrom
refactor/dry-db-layer
Mar 24, 2026
Merged

refactor(db): DRY up database layer with shared helpers and lint enforcement#550
BYK merged 1 commit intomainfrom
refactor/dry-db-layer

Conversation

@BYK
Copy link
Copy Markdown
Member

@BYK BYK commented Mar 24, 2026

Summary

DRY up the src/lib/db/ layer by extracting shared helpers, removing duplicated SQL patterns, and adding GritQL lint rules to prevent regression.

Changes

New helpers in src/lib/db/utils.ts

  • getMetadata(db, keys) — batch read from metadata table (SELECT ... WHERE key IN (...))
  • setMetadata(db, entries) — batch write in a single transaction
  • clearMetadata(db, keys) — batch delete (DELETE ... WHERE key IN (...))
  • touchCacheEntry(table, keyColumn, keyValue) — shared last_accessed timestamp update

Refactored DB modules

  • install-info.ts — 4 individual queries → 1 batch call per operation
  • version-check.ts, release-channel.ts — same metadata helper adoption
  • dsn-cache.ts, project-cache.ts — private touchCacheEntry → shared helper
  • project-cache.ts — deduplicated 4 get/set functions via getByKey/setByKey helpers
  • project-aliases.ts — manual BEGIN/COMMIT/ROLLBACKdb.transaction()()

De-async synchronous functions

Removed async/Promise<> from 27 DB functions that only do synchronous SQLite operations, plus ~370 await removals at call sites. Narrowed the biome useAwait: "off" override to only files with genuinely async operations.

GritQL lint rules (3 new)

  • no-raw-metadata-queries.grit — enforces getMetadata/setMetadata/clearMetadata
  • no-manual-transactions.grit — enforces db.transaction()()
  • no-inline-touch-cache.grit — enforces shared touchCacheEntry()

Documentation

  • AGENTS.md — expanded SQL Utilities section, replaced stale "Async Config Functions" with accurate "DB and Config Function Signatures"

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 24, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (formatters) Colorize SQL in DB span descriptions by BYK in #546
  • Add sentry sourcemap inject and sentry sourcemap upload commands by BYK in #547
  • Native debug ID injection and sourcemap upload by BYK in #543

Internal Changes 🔧

Coverage

  • Use informational-patch input instead of sed hack by BYK in #544
  • Make checks informational on release branches by BYK in #541

Other

  • (ci) Upgrade GitHub Actions to Node 24 runtime by BYK in #542
  • (db) DRY up database layer with shared helpers and lint enforcement by BYK in #550
  • (issue-list) Use collapse parameter to skip unused Snuba queries by BYK in #545
  • Regenerate skill files by github-actions[bot] in ec1ffe28

🤖 This preview updates automatically when you update the PR.

@BYK BYK marked this pull request as ready for review March 24, 2026 11:20
…rcement

Add metadata helpers (getMetadata, setMetadata, clearMetadata) and shared
touchCacheEntry to src/lib/db/utils.ts, replacing repeated per-file SQL
boilerplate across install-info, version-check, release-channel, dsn-cache,
project-cache, and project-aliases modules.

Key changes:
- Batch metadata reads/writes/deletes via IN clauses and transactions
- Shared touchCacheEntry eliminates 3 duplicate private functions
- Manual BEGIN/COMMIT/ROLLBACK replaced with db.transaction()()
- Deduplicated project-cache get/set pairs into getByKey/setByKey helpers
- Removed async/Promise from 27 synchronous SQLite functions and ~370 await
  calls at call sites, narrowing the biome useAwait override to only files
  with genuinely async operations (stat, clearResponseCache, refreshToken)
- 3 GritQL lint rules enforce using helpers over raw SQL patterns
- AGENTS.md updated with new helper docs and corrected sync signatures
@BYK BYK force-pushed the refactor/dry-db-layer branch from 000e8d9 to 3eec916 Compare March 24, 2026 11:25
@github-actions
Copy link
Copy Markdown
Contributor

Codecov Results 📊

126 passed | Total: 126 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

✅ Patch coverage is 98.05%. Project has 1036 uncovered lines.
✅ Project coverage is 96.03%. Comparing base (base) to head (head).

Files with missing lines (12)
File Patch % Lines
resolve-target.ts 90.59% ⚠️ 69 Missing
org-list.ts 96.16% ⚠️ 18 Missing
projects.ts 92.82% ⚠️ 15 Missing
detector.ts 92.46% ⚠️ 15 Missing
dsn-cache.ts 95.54% ⚠️ 10 Missing
auth.ts 96.49% ⚠️ 6 Missing
resolver.ts 94.57% ⚠️ 5 Missing
organizations.ts 96.80% ⚠️ 4 Missing
regions.ts 95.79% ⚠️ 4 Missing
utils.ts 96.05% ⚠️ 3 Missing
login.ts 98.52% ⚠️ 2 Missing
delete.ts 99.40% ⚠️ 1 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    96.02%    96.03%    +0.01%
==========================================
  Files          193       193         —
  Lines        26142     26105       -37
  Branches         0         0         —
==========================================
+ Hits         25103     25069       -34
- Misses        1039      1036        -3
- Partials         0         0         —

Generated by Codecov Action

@BYK BYK merged commit 4f8797b into main Mar 24, 2026
22 checks passed
@BYK BYK deleted the refactor/dry-db-layer branch March 24, 2026 11:31
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.

1 participant