Skip to content

fix: remove stale WAL/SHM files before pulling new databases#210

Merged
coji merged 3 commits intomainfrom
fix/pull-db-stale-wal
Mar 18, 2026
Merged

fix: remove stale WAL/SHM files before pulling new databases#210
coji merged 3 commits intomainfrom
fix/pull-db-stale-wal

Conversation

@coji
Copy link
Owner

@coji coji commented Mar 18, 2026

Summary

  • ops pull-db でDBを取得した後、古い -wal/-shm ジャーナルファイルが残っていると、新しいDBに適用されて "database disk image is malformed" エラーが発生する問題を修正
  • バックアップ後・tar展開前に古いDBファイルを全て削除するステップを追加

Test plan

  • pnpm ops pull-db -- --app <app> で正常にDBが取得できること
  • pull後に -wal/-shm ファイルが残らないこと

🤖 Generated with Claude Code

Summary by CodeRabbit

  • バグ修正
    • データベース取得・復元時に事前に既存のデータベース関連ファイル(ジャーナルや付随ファイル含む)を自動削除するようにし、古いジャーナル情報による破損や同期失敗を防止して信頼性を向上しました。

After backup, old -wal/-shm journal files were not removed before
extracting the new tar archive. SQLite would then try to apply the
stale WAL to the freshly pulled DB, causing "database disk image
is malformed" errors.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@coderabbitai
Copy link

coderabbitai bot commented Mar 18, 2026

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

ops/commands/pull-db.ts に事前クリーンアップ手順を追加し、lib/data-dir に新しい公開関数 removeAllDbFiles() を導入。pull/restore コマンドは既存DBファイル(.db/.db-wal/.db-shm)をこの新関数で削除するように切替え、pull の際にはフルパスでサニタイズ関数を呼び出すよう更新された。

Changes

Cohort / File(s) Summary
データディレクトリユーティリティ
ops/lib/data-dir.ts
node:fsnode:path を導入し、export function removeAllDbFiles(): number を追加。DATA_DIR 内の .db, -wal, -shm ファイルを検出して削除し、削除数を返す。
pull-db コマンド
ops/commands/pull-db.ts
新しい事前クリーンアップ Step を追加して removeAllDbFiles() を呼び出す。サニタイズ呼び出しにファイル名ではなく DATA_DIR と結合したフルパスを渡すように変更。手順番号とコメントを調整。
restore-db コマンド
ops/commands/restore-db.ts
既存の手動削除ロジックを removeAllDbFiles() 呼び出しへ置換。インポートに新関数を追加。制御フロー自体は変更なし。
sequenceDiagram
    autonumber
    actor Operator
    participant CLI as "ops/commands/pull-db.ts"
    participant DataDir as "ops/lib/data-dir.ts\nremoveAllDbFiles()"
    participant FS as "Filesystem (DATA_DIR)"
    participant Remote as "Remote Backup/Storage"

    Operator->>CLI: pull-db 実行
    CLI->>DataDir: removeAllDbFiles()
    DataDir->>FS: scan & delete (*.db, *-wal, *-shm)
    FS-->>DataDir: 削除結果 (count)
    DataDir-->>CLI: 削除数を返す
    CLI->>Remote: 既存バックアップの確認/取得
    Remote-->>CLI: バックアップ転送
    CLI->>FS: 圧縮展開 & sanitizeExportSettings(DATA_DIR/fullpath)
    CLI-->>Operator: 完了ログ/結果
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 ちょこっと跳ねて、古いファイルをさらり
WAL と SHM にさよならの一歩✨
新しい pull のために床をきれいにして
ピカピカの DB を迎えるよ 💾
ぴょん、作業完了!お祝いのにんじん🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PRのタイトルは「fix: remove stale WAL/SHM files before pulling new databases」で、変更内容の主要な目的と完全に一致しています。古いWAL/SHMファイルを削除するステップを追加することが、このPRの中心的な変更であり、タイトルはそれを明確かつ簡潔に表現しています。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/pull-db-stale-wal
📝 Coding Plan
  • Generate coding plan for human review comments

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.

…restore-db

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
ops/commands/pull-db.ts (1)

148-154: LGTM! WAL/SHMファイルのクリーンアップは適切に実装されています。

実装は正しく、PRの目的(stale WAL journalによるDBの破損防止)を達成しています。isDbFileプレディケートは.db.db-wal.db-shmを正しくマッチし、バックアップ後・プル前の適切なタイミングで削除されています。

オプションの提案:削除したファイル数をログに出力すると、デバッグ時に役立つかもしれません。

🔧 削除ログを追加する場合
   // Step 1.5: Remove old DB files (including -wal/-shm) to prevent
   // stale WAL journals from corrupting newly pulled databases
   const oldDbFiles = fs.readdirSync(DATA_DIR).filter(isDbFile)
   for (const file of oldDbFiles) {
     fs.unlinkSync(path.join(DATA_DIR, file))
   }
+  if (oldDbFiles.length > 0) {
+    consola.info(`Removed ${oldDbFiles.length} old database file(s)`)
+  }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ops/commands/pull-db.ts` around lines 148 - 154, Add a debug/info log after
removing old DB files to report how many files were deleted: after computing
oldDbFiles (the array filtered by isDbFile against DATA_DIR) and performing the
fs.unlinkSync removal loop, call the repository/process logger to emit a message
including oldDbFiles.length and optionally list the filenames; reference
oldDbFiles, isDbFile, DATA_DIR, and the existing unlink loop so the log is
placed immediately after the deletions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@ops/commands/pull-db.ts`:
- Around line 148-154: Add a debug/info log after removing old DB files to
report how many files were deleted: after computing oldDbFiles (the array
filtered by isDbFile against DATA_DIR) and performing the fs.unlinkSync removal
loop, call the repository/process logger to emit a message including
oldDbFiles.length and optionally list the filenames; reference oldDbFiles,
isDbFile, DATA_DIR, and the existing unlink loop so the log is placed
immediately after the deletions.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 27792f12-c843-4302-8435-bafb05a662f6

📥 Commits

Reviewing files that changed from the base of the PR and between ab81f5f and 6665077.

📒 Files selected for processing (1)
  • ops/commands/pull-db.ts

@coji coji merged commit 988f80f into main Mar 18, 2026
6 checks passed
@coji coji deleted the fix/pull-db-stale-wal branch March 18, 2026 11: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.

1 participant