Skip to content

chore: replace better-sqlite3 with bun:sqlite#167

Merged
sasagar merged 1 commit into
devfrom
chore/issue-166-bun-sqlite-migration
May 13, 2026
Merged

chore: replace better-sqlite3 with bun:sqlite#167
sasagar merged 1 commit into
devfrom
chore/issue-166-bun-sqlite-migration

Conversation

@sasagar
Copy link
Copy Markdown
Collaborator

@sasagar sasagar commented May 13, 2026

Closes #166

Summary

C++ ネイティブモジュール better-sqlite3 を Bun 組み込みの bun:sqlite に置換。CI から Node.js セットアップを撤廃し、ネイティブビルド依存(python / build-essential)も不要に。

Why

  • better-sqlite3 は Bun では動作せず、CI で Node.js 24 を追加セットアップする運用だった
  • ネイティブモジュールはサプライチェーン攻撃の表面積でもある(pre-built binary、build-time script)
  • Bun の bun:sqlite + drizzle-orm/bun-sqlite で同等機能を提供できる

Changes

Driver swap (packages/backend/src/db/index.ts)

  • drizzle-orm/better-sqlite3drizzle-orm/bun-sqlite
  • better-sqlite3bun:sqlite(Bun 組み込み)
  • sqlite.pragma("journal_mode = WAL")sqlite.exec("PRAGMA journal_mode = WAL")
  • SqliteDatabase 型を共通 export(D1 型もこれに合流)

Repository (22 files)

  • BetterSQLite3DatabaseBunSQLiteDatabase(型エイリアスのみ、ロジック変更なし)

Tests

  • src/tests/node/sqlite-repositories.test.ts(vitest 経由)を削除
  • src/tests/integration/sqlite-repositories.test.tsbun:test + bun:sqlite で再実装(28 テスト、全 pass)

Dependencies

  • 削除: better-sqlite3, @types/better-sqlite3, vitest
  • test:sqlite script: npx vitest run …node/…bun test …integration/…

CI (.github/workflows/ci.yml)

  • sqlite-tests job から actions/setup-node ステップを削除
  • bunx vitestbun test

Test plan

ローカル検証済み(macOS, Bun 1.3.13):

  • bun run typecheck (backend + frontend) — pass
  • bun run lint — 0 warnings, 0 errors (576 files)
  • bun test src/tests/unit/1012 pass / 0 fail
  • bun test src/tests/integration/70 pass / 0 fail(うち 28 が SQLite)
  • bun run build (backend) — 6.65 MB, 1966 modules
  • node_modules/better-sqlite3, vitest, @types/better-sqlite3 — すべて不在を確認

Compatibility

  • SQLite ファイルフォーマットは完全互換(既存 DB をそのまま使える)
  • bun:sqlite API は better-sqlite3 とほぼ互換、Drizzle 抽象化で吸収
  • D1 (Cloudflare Workers) パスは型のみ変更、実行時挙動は不変

Conflicts with PR #164

.github/workflows/ci.ymlsqlite-tests job が PR #164(hardening: Actions SHA 固定)と重なります。マージ順序により:

どちらにせよ簡単な競合解決で済みます。

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

Warning

Rate limit exceeded

@sasagar has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 30 minutes and 53 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 24791bd6-0736-41a3-8577-241c15f02326

📥 Commits

Reviewing files that changed from the base of the PR and between 3c76a5c and 865a259.

⛔ Files ignored due to path filters (1)
  • bun.lock is excluded by !**/*.lock
📒 Files selected for processing (28)
  • .github/workflows/ci.yml
  • packages/backend/package.json
  • packages/backend/src/db/index.ts
  • packages/backend/src/db/schema/sqlite.ts
  • packages/backend/src/repositories/sqlite/SqliteCustomEmojiRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteDriveFileRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteDriveFolderRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteFollowRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteInstanceBlockRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteInstanceSettingsRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteInvitationCodeRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteModerationAuditLogRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteNoteRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteNotificationRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteOAuthAccountRepository.ts
  • packages/backend/src/repositories/sqlite/SqlitePasskeyChallengeRepository.ts
  • packages/backend/src/repositories/sqlite/SqlitePasskeyCredentialRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteReactionRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteRemoteInstanceRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteRoleAssignmentRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteRoleRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteScheduledNoteRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteSessionRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteUserReportRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteUserRepository.ts
  • packages/backend/src/repositories/sqlite/SqliteUserWarningRepository.ts
  • packages/backend/src/tests/integration/sqlite-repositories.test.ts
  • packages/backend/src/tests/node/sqlite-repositories.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/issue-166-bun-sqlite-migration

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sasagar
Copy link
Copy Markdown
Collaborator Author

sasagar commented May 13, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@sasagar sasagar force-pushed the chore/issue-166-bun-sqlite-migration branch from fbaec90 to 8154df0 Compare May 13, 2026 17:08
Eliminates the C++ native module `better-sqlite3` in favor of Bun's
built-in `bun:sqlite`, removing the need for Node.js in CI and
simplifying the toolchain.

Driver swap (db/index.ts):
- drizzle-orm/better-sqlite3 → drizzle-orm/bun-sqlite
- better-sqlite3 → bun:sqlite (built-in)
- sqlite.pragma("journal_mode = WAL") → sqlite.exec("PRAGMA ...")
- Export SqliteDatabase type for shared use

Type renames across 22 repositories:
- BetterSQLite3Database → BunSQLiteDatabase

Tests consolidated to Bun test runner:
- Removed src/tests/node/sqlite-repositories.test.ts (vitest)
- Migrated full coverage into src/tests/integration/sqlite-repositories.test.ts
  using bun:test + bun:sqlite
- 28 SQLite integration tests pass under bun test

Dependencies removed:
- better-sqlite3 (native module)
- @types/better-sqlite3
- vitest (only used for the SQLite tests above)

CI simplification:
- Drop actions/setup-node step from sqlite-tests job
- bunx vitest → bun test

Verification (local):
- bun run typecheck (backend + frontend) — pass
- bun run lint — 0 warnings, 0 errors
- bun test src/tests/unit/ — 1012 pass / 0 fail
- bun test src/tests/integration/ — 70 pass / 0 fail
- bun run build (backend) — succeeds
- node_modules/better-sqlite3 — not installed (optional peer only)
@sasagar sasagar force-pushed the chore/issue-166-bun-sqlite-migration branch from 8154df0 to 865a259 Compare May 13, 2026 17:11
@sasagar sasagar merged commit 76dbd44 into dev May 13, 2026
11 checks passed
@sasagar sasagar deleted the chore/issue-166-bun-sqlite-migration branch May 13, 2026 17:16
This was referenced May 13, 2026
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