Skip to content

feat: add fsck command to detect and repair DB/storage inconsistencies [backport #975] - #976

Merged
kalbasit merged 1 commit into
release-0.9from
backport-975-to-release-0.9
Feb 23, 2026
Merged

kalbasit merged 1 commit into
release-0.9from
backport-975-to-release-0.9

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Bot-based backport to release-0.9, triggered by a label in #975.

Summary

  • Adds a new ncps fsck command that performs integrity checks between the database and storage backends
  • Uses a double-check pattern (collect suspects → re-verify → report → optionally repair) to safely handle in-flight operations without exclusive locks
  • Supports --dry-run (preview issues without changes) and --repair (fix without prompting)

Checks performed

  • Narinfos in DB with no linked nar_file entries
  • nar_files in DB not linked to any narinfo (orphaned)
  • nar_files in DB whose physical files are missing from storage
  • NAR files in storage with no corresponding DB record
  • (CDC mode) Orphaned chunks in DB, chunks missing from storage, orphaned chunk files in storage

Changes

  • New SQL queries (GetAllNarFiles, GetNarInfosWithoutNarFiles, GetAllChunks) added to all 3 DB backends and regenerated
  • WalkNars added to storage.NarStore interface with local + S3 implementations
  • WalkChunks added to chunk.Store interface with local + S3 implementations
  • New pkg/ncps/fsck.go command implementation
  • New pkg/ncps/fsck_test.go with 7 test cases (clean, narinfos-without-narfiles, orphaned-narfiles, missing-from-storage, orphaned-in-storage, repair, dry-run)

Test plan

  • go test -race ./pkg/ncps/... -run TestFsck -v — all subtests pass
  • ncps fsck --help shows correct flags
  • ncps fsck --dry-run ... reports issues without modifying anything
  • ncps fsck --repair ... fixes issues; second run shows 0 issues

🤖 Generated with Claude Code

#975)

## Summary

- Adds a new `ncps fsck` command that performs integrity checks between
the database and storage backends
- Uses a double-check pattern (collect suspects → re-verify → report →
optionally repair) to safely handle in-flight operations without
exclusive locks
- Supports `--dry-run` (preview issues without changes) and `--repair`
(fix without prompting)

## Checks performed

- Narinfos in DB with no linked `nar_file` entries
- `nar_files` in DB not linked to any narinfo (orphaned)
- `nar_files` in DB whose physical files are missing from storage
- NAR files in storage with no corresponding DB record
- (CDC mode) Orphaned chunks in DB, chunks missing from storage,
orphaned chunk files in storage

## Changes

- New SQL queries (`GetAllNarFiles`, `GetNarInfosWithoutNarFiles`,
`GetAllChunks`) added to all 3 DB backends and regenerated
- `WalkNars` added to `storage.NarStore` interface with local + S3
implementations
- `WalkChunks` added to `chunk.Store` interface with local + S3
implementations
- New `pkg/ncps/fsck.go` command implementation
- New `pkg/ncps/fsck_test.go` with 7 test cases (clean,
narinfos-without-narfiles, orphaned-narfiles, missing-from-storage,
orphaned-in-storage, repair, dry-run)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 4.6 <[email protected]>
(cherry picked from commit 7ef3479)
@kalbasit
kalbasit enabled auto-merge (squash) February 23, 2026 22:58
@dosubot dosubot Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. enhancement New feature or request go Pull requests that update go code labels Feb 23, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @kalbasit, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the data integrity capabilities of the ncps tool by introducing a new fsck command. This command is designed to systematically identify and optionally repair various inconsistencies that can arise between the database and different storage backends. By implementing a robust double-check pattern, it ensures that data is handled safely, even during active operations. The addition of this command improves the overall reliability and maintainability of the storage system by providing a powerful tool for diagnosing and resolving data synchronization issues.

Highlights

  • New fsck Command: Introduced a new ncps fsck command to detect and repair inconsistencies between the database and storage backends.
  • Comprehensive Integrity Checks: The fsck command performs checks for narinfos without linked nar_files, orphaned nar_files in the DB, nar_files missing from storage, orphaned NAR files in storage, and (in CDC mode) orphaned chunks in DB, chunks missing from storage, and orphaned chunk files in storage.
  • Safe Repair Mechanism: Implemented a double-check pattern (collect suspects -> re-verify -> report -> optionally repair) to safely handle in-flight operations without exclusive locks, preventing accidental data loss.
  • Flexible Operation Modes: The command supports --dry-run to preview issues without making changes and --repair to automatically fix issues without prompting.
  • Database Query Enhancements: Added new SQL queries (GetAllNarFiles, GetNarInfosWithoutNarFiles, GetAllChunks) and corresponding generated Go code across MySQL, PostgreSQL, and SQLite to facilitate consistency checks.
  • Storage Backend Walkers: Extended the storage.NarStore and chunk.Store interfaces with WalkNars and WalkChunks methods, respectively, enabling efficient iteration over stored NAR and chunk files for integrity verification.
Changelog
  • db/query.mysql.sql
    • Added new SQL queries for fetching all nar_files, narinfos without linked nar_files, and all chunks.
  • db/query.postgres.sql
    • Added new SQL queries for fetching all nar_files, narinfos without linked nar_files, and all chunks.
  • db/query.sqlite.sql
    • Added new SQL queries for fetching all nar_files, narinfos without linked nar_files, and all chunks.
  • pkg/database/generated_models.go
    • Added a new struct GetAllNarFilesRow to represent results from the GetAllNarFiles query.
  • pkg/database/generated_querier.go
    • Updated the Querier interface to include new methods: GetAllChunks, GetAllNarFiles, and GetNarInfosWithoutNarFiles.
  • pkg/database/generated_wrapper_mysql.go
    • Implemented new database wrapper methods for GetAllChunks, GetAllNarFiles, and GetNarInfosWithoutNarFiles.
  • pkg/database/generated_wrapper_postgres.go
    • Implemented new database wrapper methods for GetAllChunks, GetAllNarFiles, and GetNarInfosWithoutNarFiles.
  • pkg/database/generated_wrapper_sqlite.go
    • Implemented new database wrapper methods for GetAllChunks, GetAllNarFiles, and GetNarInfosWithoutNarFiles.
  • pkg/database/mysqldb/querier.go
    • Updated the MySQL Querier interface to include new methods for fetching nar_files, narinfos, and chunks.
  • pkg/database/mysqldb/query.mysql.sql.go
    • Generated Go code for new MySQL queries to retrieve nar_files, narinfos without nar_files, and chunks.
  • pkg/database/postgresdb/querier.go
    • Updated the PostgreSQL Querier interface to include new methods for fetching nar_files, narinfos, and chunks.
  • pkg/database/postgresdb/query.postgres.sql.go
    • Generated Go code for new PostgreSQL queries to retrieve nar_files, narinfos without nar_files, and chunks.
  • pkg/database/sqlitedb/querier.go
    • Updated the SQLite Querier interface to include new methods for fetching nar_files, narinfos, and chunks.
  • pkg/database/sqlitedb/query.sqlite.sql.go
    • Generated Go code for new SQLite queries to retrieve nar_files, narinfos without nar_files, and chunks.
  • pkg/ncps/fsck.go
    • Added the core implementation of the fsck command, including logic for collecting suspects, re-verifying, reporting, and repairing consistency issues.
  • pkg/ncps/fsck_test.go
    • Added comprehensive test cases for the fsck command, covering clean state, various inconsistency scenarios, dry-run, and repair functionalities across different database backends.
  • pkg/ncps/migrate_nar_to_chunks.go
    • Updated the CDC enabled check to use the new configValueTrue constant.
  • pkg/ncps/root.go
    • Registered the new fsckCommand with the main CLI application.
  • pkg/ncps/serve.go
    • Defined a new constant configValueTrue and updated its usage in CDC configuration loading.
  • pkg/storage/chunk/local.go
    • Implemented the WalkChunks method for local chunk storage, allowing iteration over stored chunks.
  • pkg/storage/chunk/s3.go
    • Implemented the WalkChunks method for S3 chunk storage, allowing iteration over stored chunks.
  • pkg/storage/chunk/store.go
    • Extended the Store interface with a new WalkChunks method.
  • pkg/storage/local/local.go
    • Implemented the WalkNars method for local NAR storage, enabling efficient iteration over stored NAR files.
  • pkg/storage/s3/s3.go
    • Implemented the WalkNars method for S3 NAR storage, enabling efficient iteration over stored NAR files, and added storeNarPrefix helper.
  • pkg/storage/store.go
    • Extended the NarStore interface with a new WalkNars method.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@dosubot

dosubot Bot commented Feb 23, 2026

Copy link
Copy Markdown

Related Documentation

Checked 7 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the ncps fsck command, which provides essential integrity checks and repair capabilities for the database and storage backends. The implementation uses a robust double-check pattern to handle concurrent operations safely. My feedback focuses on improving the performance of these checks, particularly for large storage backends like S3, by optimizing storage iteration and database lookups. I also suggested using filepath.WalkDir for better efficiency in local storage walking and adding a terminal check for interactive repairs.

Comment thread pkg/ncps/fsck.go Resolved
Comment thread pkg/ncps/fsck.go Resolved
Comment thread pkg/ncps/fsck.go Resolved
Comment thread pkg/ncps/fsck.go Resolved
Comment thread pkg/storage/local/local.go Resolved
Comment thread pkg/storage/chunk/local.go Resolved
@kalbasit
kalbasit merged commit 8062d6f into release-0.9 Feb 23, 2026
23 checks passed
@kalbasit
kalbasit deleted the backport-975-to-release-0.9 branch February 23, 2026 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update go code size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant