-
Notifications
You must be signed in to change notification settings - Fork 5
Cache Lifecycle and Performance
Ngan Pham edited this page May 6, 2026
·
2 revisions
- A fixture is declared in test context.
- FixtureKit generates cache by evaluating fixture definition in adapter isolation.
- Each registered coder wraps the definition block and records what it observes — by default
FixtureKit::ActiveRecordCoderrecords touched models and stores SQL restore statements + exposed ids. - Each test mounts cache data and rehydrates exposed records through
Repository.
For each coder's data, mount runs in this order:
- Group models by
connection_pool. For each pool, check out a connection viapool.with_connection. - Replay the coder's restore statements with referential integrity disabled (DELETEs + INSERTs in one batched call per connection).
- If
ActiveRecord.verify_foreign_keys_for_fixturesis true, validate foreign keys after the batch. A violation raisesFixtureKit::Errorwith a stale-cache hint. - Reset primary key sequences for the touched tables. Uses the batched
reset_column_sequences!on Rails 8.2+ or per-tablereset_pk_sequence!on older versions. No-op on adapters whose PK generators advance from explicit-id INSERTs (MySQL, SQLite).
At runner start, FixtureKit clears cache_path by default.
- RSpec start point:
before(:suite) - Minitest start point: class
run_suitebefore first generation
Set FIXTURE_KIT_PRESERVE_CACHE to keep cache files between runs:
FIXTURE_KIT_PRESERVE_CACHE=1 bundle exec rspecTruthy values are case-insensitive: 1, true, yes.
- Cache generation does real app writes once per fixture context.
- Cache mounting replays SQL in batches per connection with referential integrity disabled during replay.
- Repository reads are lazy and memoized per exposed name.