Skip to content

Commit 6583bca

Browse files
yahondaclaude
andcommitted
Stub use_foreign_keys? instead of swapping the connection pool in specs
The previous spec re-established ActiveRecord::Base with foreign_keys: false in before(:each) and restored CONNECTION_PARAMS in after(:each). That swaps the global pool, so the outer describe's before(:all)-captured @conn ends up pointing at a connection from the discarded pool. A later spec (schema_dumper_spec.rb:571 "should specify non-default tablespace in add index") then stubs default_tablespace on that stale @conn, while SchemaDumper queries the fresh pool's connection. The stub never takes effect, the index dump lacks the tablespace, and the example fails under randomized ordering. Stub @conn.use_foreign_keys? on the live connection instead. That keeps the pool intact, avoids cross-describe state leakage, and still exercises every oracle-enhanced gate (add_foreign_key early return, SchemaCreation FK skip, SchemaDumper FK skip). The foreign_keys: false → use_foreign_keys? == false plumbing is Rails core's responsibility (foreign_keys_enabled? = @config.fetch(...)) and is already covered upstream. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 26d4cc2 commit 6583bca

1 file changed

Lines changed: 10 additions & 15 deletions

File tree

spec/active_record/connection_adapters/oracle_enhanced/schema_dumper_spec.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,8 @@ def drop_test_posts_table
440440
end
441441
end
442442

443-
describe "foreign_keys: false database.yml option" do
444-
let(:foreign_keys_disabled_conn) { ActiveRecord::Base.lease_connection }
445-
443+
describe "with use_foreign_keys? returning false (foreign_keys: false in database.yml)" do
446444
before(:each) do
447-
ActiveRecord::Base.establish_connection(CONNECTION_PARAMS.merge(foreign_keys: false))
448445
schema_define do
449446
drop_table :test_comments, if_exists: true
450447
drop_table :test_posts, if_exists: true
@@ -460,52 +457,50 @@ def drop_test_posts_table
460457

461458
after(:each) do
462459
schema_define do
460+
remove_foreign_key :test_comments, :test_posts, if_exists: true
463461
drop_table :test_comments, if_exists: true
464462
drop_table :test_posts, if_exists: true
465463
end
466-
ensure
467-
ActiveRecord::Base.establish_connection(CONNECTION_PARAMS)
468-
end
469-
470-
it "reports use_foreign_keys? as false" do
471-
expect(foreign_keys_disabled_conn.use_foreign_keys?).to be false
472-
expect(foreign_keys_disabled_conn.supports_foreign_keys?).to be true
473464
end
474465

475466
it "schema dump omits add_foreign_key statements" do
476467
schema_define do
477468
add_foreign_key :test_comments, :test_posts
478469
end
470+
allow(@conn).to receive(:use_foreign_keys?).and_return(false)
479471
expect(standard_dump).not_to match(/add_foreign_key/)
480472
end
481473

482474
it "add_foreign_key is a silent no-op" do
475+
allow(@conn).to receive(:use_foreign_keys?).and_return(false)
483476
expect {
484477
schema_define do
485478
add_foreign_key :test_comments, :test_posts
486479
end
487480
}.not_to raise_error
488-
expect(foreign_keys_disabled_conn.foreign_keys("test_comments")).to be_empty
481+
expect(@conn.foreign_keys("test_comments")).to be_empty
489482
end
490483

491-
it "add_foreign_key skips deferrable validation when disabled" do
484+
it "add_foreign_key skips deferrable validation" do
485+
allow(@conn).to receive(:use_foreign_keys?).and_return(false)
492486
expect {
493487
schema_define do
494488
add_foreign_key :test_comments, :test_posts, deferrable: :always
495489
end
496490
}.not_to raise_error
497-
expect(foreign_keys_disabled_conn.foreign_keys("test_comments")).to be_empty
491+
expect(@conn.foreign_keys("test_comments")).to be_empty
498492
end
499493

500494
it "create_table with inline t.references foreign_key: true does not create the FK" do
495+
allow(@conn).to receive(:use_foreign_keys?).and_return(false)
501496
schema_define do
502497
drop_table :test_comments, if_exists: true
503498
create_table :test_comments, force: true do |t|
504499
t.string :body, limit: 4000
505500
t.references :test_post, foreign_key: true
506501
end
507502
end
508-
expect(foreign_keys_disabled_conn.foreign_keys("test_comments")).to be_empty
503+
expect(@conn.foreign_keys("test_comments")).to be_empty
509504
end
510505
end
511506

0 commit comments

Comments
 (0)