Skip to content

fix(packages): migrate older DiskCache schemas to add leases.refcount#133

Merged
zackees merged 1 commit intomainfrom
fix/disk-cache-schema-migration
Apr 19, 2026
Merged

fix(packages): migrate older DiskCache schemas to add leases.refcount#133
zackees merged 1 commit intomainfrom
fix/disk-cache-schema-migration

Conversation

@zackees
Copy link
Copy Markdown
Member

@zackees zackees commented Apr 19, 2026

Summary

Fixes #124. Any DiskCache::lease() call against a production cache DB created before the leases.refcount column was added errors with no such column: refcount, blocking fbuild lnk pull and every other lease-consuming code path.

Approach — option B (versioned migrations)

Replaces the single-step migrate_v1 with an append-only, idempotent migration framework keyed by stable ids in a new schema_migrations table:

  • m001_initial_schema — original tables + indexes, all CREATE TABLE IF NOT EXISTS so it's safe on databases that already have them.
  • m002_add_leases_refcountALTER TABLE leases ADD COLUMN refcount INTEGER NOT NULL DEFAULT 1; guarded by a PRAGMA table_info(leases) check. No-op on caches that already have the column from m001.

Each migration runs inside its own transaction and is skipped if its id is already present in schema_migrations. Future deltas are append-only. The legacy cache_meta.schema_version key is still mirrored for tooling that inspects it, but the authoritative source of truth is now the migrations table.

Chose option B over option A because the issue itself flags option 2 as the longer-term clean answer and the framework cost is small (~60 LOC) compared to re-adding a migration infrastructure the next time a column gets added.

best_effort_lease revert

This PR also reverts the best_effort_lease() workaround introduced in #119's lnk/resolver.rs. Since every opened cache now has leases.refcount guaranteed by the migration, the lease is once again a hard requirement — cache.lease(entry).map_err(map_cache_err)?. Revert is in the same commit as the migration: 90d18d0.

Files touched

  • crates/fbuild-packages/src/disk_cache/index.rs — migration framework + MIGRATIONS list + leases_has_refcount helper + 3 new unit tests.
  • crates/fbuild-packages/src/lnk/resolver.rs — remove best_effort_lease, use cache.lease()? directly.
  • crates/fbuild-packages/tests/disk_cache_schema_migration.rs — new integration test seeding a legacy schema and asserting DiskCache::open_at + cache.lease() succeed end-to-end.
  • crates/fbuild-packages/tests/README.md — new (required by repo's readme-guard hook).

Test plan

  • uv run cargo test -p fbuild-packages — all 391 unit tests + 2 new migration integration tests + 4 lnk_e2e tests pass.
  • uv run cargo clippy --workspace --all-targets -- -D warnings — clean.
  • New test_legacy_schema_missing_refcount_is_migrated reproduces the exact bug scenario (pre-migration DB → open → lease) before the fix and passes after.
  • test_migrations_idempotent_across_reopens guards against re-running ALTER TABLE on second open.
  • Fresh-DB regression guard (fresh_cache_lease_still_works) confirms no regression for new users.

🤖 Generated with Claude Code

Older production cache databases (pre-#119) lack the `leases.refcount`
column that `pin()`/`unpin()` expect, causing every call to
`DiskCache::lease()` to error with "no such column: refcount" and
blocking any `.lnk` pull against a long-lived cache.

Replace the single-step `migrate_v1` with an append-only versioned
migration framework tracked in a `schema_migrations` table, keyed by
stable migration ids:

  - m001_initial_schema     — original tables + indexes (idempotent
                              CREATE TABLE IF NOT EXISTS).
  - m002_add_leases_refcount — ALTER TABLE adds refcount INTEGER NOT
                               NULL DEFAULT 1 when the column is
                               absent; no-op on new caches that
                               already have it from m001.

Each migration runs in its own transaction and is skipped if its id
is already present in `schema_migrations`. Future deltas just append.
The legacy `cache_meta.schema_version` key is still mirrored for
tooling that inspects it, but the authoritative source of truth is
now the migrations table.

Revert the `best_effort_lease()` workaround in `lnk/resolver.rs` that
#119 added: the lease is once again a hard requirement, since the
migration guarantees the column exists on every opened cache.

Tests:
  - `test_legacy_schema_missing_refcount_is_migrated` (unit): hand-
    seeds the pre-migration schema, opens via `CacheIndex::open`,
    asserts `pin()`/`unpin()` succeed.
  - `test_migrations_idempotent_across_reopens` (unit): second open
    must not re-apply migrations or double-error on ALTER TABLE.
  - `test_all_migrations_recorded_after_open` (unit): regression
    guard ensuring every registered migration is recorded.
  - `legacy_cache_migrates_and_lease_succeeds` (integration): full
    `DiskCache::open_at` → `cache.lease()` flow against a seeded
    legacy DB, directly reproducing the issue-#124 scenario.
  - `fresh_cache_lease_still_works` (integration): regression guard
    for the fresh-DB happy path.

Closes #124

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 19, 2026

Warning

Rate limit exceeded

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

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 30 minutes and 33 seconds.

⌛ 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: 22ac8a1c-bc12-43d8-9701-e2a3a938a10a

📥 Commits

Reviewing files that changed from the base of the PR and between aed0649 and 90d18d0.

📒 Files selected for processing (4)
  • crates/fbuild-packages/src/disk_cache/index.rs
  • crates/fbuild-packages/src/lnk/resolver.rs
  • crates/fbuild-packages/tests/README.md
  • crates/fbuild-packages/tests/disk_cache_schema_migration.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/disk-cache-schema-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.

@zackees zackees merged commit 5a1a346 into main Apr 19, 2026
8 of 77 checks passed
zackees added a commit that referenced this pull request Apr 19, 2026
Cuts a release containing the two P0 fixes landed since 2.1.19:

- #134 "P0 regression — Operation not permitted (os error 1) on warm build"
- #135 "preserve exec bit on fbuild console script in wheel"

Both are currently blocking every FastLED uno build on GitHub Actions:
the wheel's console script installs without +x, so CI can't even run
`fbuild --version`, and the subsequent compile fails with
`Operation not permitted (os error 1)` on every example.

Also includes:

- #131 rustfmt on lnk pipeline
- #133 DiskCache leases.refcount schema migration
- #128 AVR orchestrator fingerprint fast-path + telemetry (#127)
- #126 FBUILD_WATCH_SET_CACHE_SECS env override
- f8533d3 extend watch-set fingerprint fast-path to AVR orchestrator

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
zackees added a commit that referenced this pull request Apr 19, 2026
Cuts a release containing the two P0 fixes landed since 2.1.19:

- #134 "P0 regression — Operation not permitted (os error 1) on warm build"
- #135 "preserve exec bit on fbuild console script in wheel"

Both are currently blocking every FastLED uno build on GitHub Actions:
the wheel's console script installs without +x, so CI can't even run
`fbuild --version`, and the subsequent compile fails with
`Operation not permitted (os error 1)` on every example.

Also includes:

- #131 rustfmt on lnk pipeline
- #133 DiskCache leases.refcount schema migration
- #128 AVR orchestrator fingerprint fast-path + telemetry (#127)
- #126 FBUILD_WATCH_SET_CACHE_SECS env override
- f8533d3 extend watch-set fingerprint fast-path to AVR orchestrator

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

disk_cache::Lease::acquire fails on older schemas — 'no such column: refcount'

1 participant