Commit 923e1db
Honor foreign_keys: false database.yml option
Rails 7.1 (rails/rails#45301) introduced an adapter option that lets
applications opt out of foreign-key constraints via database.yml:
production:
adapter: oracle_enhanced
...
foreign_keys: false
The Rails helper use_foreign_keys? (= supports_foreign_keys? &&
@config.fetch(:foreign_keys, true)) is then used to gate
add_foreign_key, remove_foreign_key, foreign_key_for, and
SchemaCreation FK emission.
oracle-enhanced's SchemaCreation#visit_TableDefinition already routes
through use_foreign_keys?, and remove_foreign_key/foreign_key_for are
inherited unchanged so Rails core's gates apply. Two oracle-enhanced
overrides needed adjustment:
* OracleEnhanced::SchemaDumper#tables overrides Rails' base method and
emitted add_foreign_key lines unconditionally. Apply the gate inline
next to divergent_unique_constraints(tbl, stream) so the original
per-table interleave (FK_a -> UC_a -> FK_b -> UC_b) is preserved.
This matches PR #45301's original dumper-gate intent; rails/rails#48731
(Feb 2024) inadvertently reverted that gate to supports_foreign_keys?
upstream, so adopting use_foreign_keys? here lets Oracle users
actually benefit from foreign_keys: false.
* OracleEnhanced::SchemaStatements#add_foreign_key ran
assert_valid_deferrable before delegating to super, so an invalid
deferrable: value still raised when FKs were globally disabled.
return unless use_foreign_keys? at the top of the override matches
Rails core's contract (silent no-op) and mirrors the first line of
Rails' own add_foreign_key in abstract/schema_statements.rb.
Specs stub @conn.use_foreign_keys? rather than re-establishing the
connection with foreign_keys: false. Pool replacement leaks a stale
@conn into other describe blocks under randomized order, which
otherwise surfaced as a tablespace-dump failure in
schema_dumper_spec.rb:571. The integration-style proof that
foreign_keys: false flows through to use_foreign_keys? is Rails core's
responsibility (foreign_keys_enabled? = @config.fetch(:foreign_keys,
true)) and is covered upstream. The new specs here cover the four
oracle-enhanced surfaces that must honor the flag: dumper output,
add_foreign_key no-op, deferrable validation skip, and SchemaCreation
inline FK suppression with an Oracle-invalid deferrable value.
structure.sql is intentionally left untouched. Rails core's per-adapter
Tasks::*DatabaseTasks#structure_dump (pg_dump / mysqldump / sqlite3)
does not gate on foreign_keys_enabled? / use_foreign_keys? because
structure dumps reflect live DB state, not adapter config. Oracle's
structure_dump (lib/.../oracle_enhanced/structure_dump.rb) is left
likewise: migrations run with foreign_keys: false create no FKs so
the structure dump has nothing to emit; if FKs already exist in the
DB they surface in structure.sql exactly as pg_dump would.
Targets master only (Rails 8.2.alpha).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent ac83019 commit 923e1db
3 files changed
Lines changed: 80 additions & 1 deletion
File tree
- lib/active_record/connection_adapters/oracle_enhanced
- spec/active_record/connection_adapters/oracle_enhanced
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
735 | 735 | | |
736 | 736 | | |
737 | 737 | | |
| 738 | + | |
738 | 739 | | |
739 | 740 | | |
740 | 741 | | |
| |||
Lines changed: 78 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
440 | 440 | | |
441 | 441 | | |
442 | 442 | | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
443 | 521 | | |
444 | 522 | | |
445 | 523 | | |
| |||
0 commit comments