Skip to content

Verify foreign keys after mount when configured#56

Merged
ngan merged 1 commit into
mainfrom
np-verify-foreign-keys
May 6, 2026
Merged

Verify foreign keys after mount when configured#56
ngan merged 1 commit into
mainfrom
np-verify-foreign-keys

Conversation

@ngan
Copy link
Copy Markdown
Collaborator

@ngan ngan commented May 6, 2026

Summary

Mirror Rails' fixture-loading FK check: when ActiveRecord.verify_foreign_keys_for_fixtures is true, call connection.check_all_foreign_keys_valid! after the batch executes. Wrap any violation in a FixtureKit::Error with a hint that the cache may be stale.

Behavior

  • Default since Rails 8.0 load_defaults is true, so this turns on automatically for new apps.
  • PG and SQLite raise on violations.
  • MySQL inherits the abstract no-op (matches our PG-vs-MySQL story for sequence reset).

Why

The most likely trigger is regenerating fixtures against a different schema than the one currently loaded — e.g. someone changed an FK column in a migration but the cached fixture still references the old shape. Surface that as a fixture_kit error rather than a raw ActiveRecord::StatementInvalid that's hard to interpret.

Stack

Stacked on #55.

Test plan

  • New unit tests cover the enabled / disabled / wrapped-error paths
  • All existing specs pass on sqlite
  • CI green

🤖 Generated with Claude Code

@ngan ngan changed the base branch from np-filter-virtual-columns to main May 6, 2026 20:11
Mirror Rails' fixture-loading FK check: when
ActiveRecord.verify_foreign_keys_for_fixtures is true (default since
Rails 8.0 load_defaults), call connection.check_all_foreign_keys_valid!
after the batch executes.

PG and SQLite raise on violations; MySQL is a no-op. Wrap any
StatementInvalid in a FixtureKit::Error with a hint that the cache may
be stale relative to the schema or fixture definitions — most likely
trigger is a developer regenerating fixtures against a different schema
than the one currently loaded.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@ngan ngan force-pushed the np-verify-foreign-keys branch from 5ce3547 to c27556a Compare May 6, 2026 20:14
@ngan ngan marked this pull request as ready for review May 6, 2026 20:14
@ngan ngan merged commit 31a423a into main May 6, 2026
8 checks passed
@ngan ngan deleted the np-verify-foreign-keys branch May 6, 2026 20:16
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