Skip to content

Commit 93b1d39

Browse files
Merge pull request rails#47499 from fatkodima/activerecord-tests-speedup
Speedup activerecord tests
2 parents b146cd4 + 2a6adc9 commit 93b1d39

File tree

11 files changed

+59
-25
lines changed

11 files changed

+59
-25
lines changed

activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ def build_explain_clause(options = [])
160160
end
161161

162162
private
163+
IDLE_TRANSACTION_STATUSES = [PG::PQTRANS_IDLE, PG::PQTRANS_INTRANS, PG::PQTRANS_INERROR]
164+
private_constant :IDLE_TRANSACTION_STATUSES
165+
163166
def cancel_any_running_query
164-
return unless @raw_connection && @raw_connection.transaction_status != PG::PQTRANS_IDLE
167+
return if @raw_connection.nil? || IDLE_TRANSACTION_STATUSES.include?(@raw_connection.transaction_status)
168+
165169
@raw_connection.cancel
166170
@raw_connection.block
167171
rescue PG::Error

activerecord/lib/active_record/schema_dumper.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def initialize(connection, options = {})
6565
@connection = connection
6666
@version = connection.migration_context.current_version rescue nil
6767
@options = options
68+
@ignore_tables = [
69+
ActiveRecord::Base.schema_migrations_table_name,
70+
ActiveRecord::Base.internal_metadata_table_name,
71+
self.class.ignore_tables
72+
].flatten
6873
end
6974

7075
# turns 20170404131909 into "2017_04_04_131909"
@@ -317,7 +322,7 @@ def remove_prefix_and_suffix(table)
317322
end
318323

319324
def ignored?(table_name)
320-
[ActiveRecord::Base.schema_migrations_table_name, ActiveRecord::Base.internal_metadata_table_name, ignore_tables].flatten.any? do |ignored|
325+
@ignore_tables.any? do |ignored|
321326
ignored === remove_prefix_and_suffix(table_name)
322327
end
323328
end

activerecord/test/cases/adapters/mysql2/mysql2_adapter_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ def test_does_not_raise_note_level_warnings
414414
end
415415

416416
def test_warnings_do_not_change_returned_value_of_exec_update
417+
previous_logger = ActiveRecord::Base.logger
418+
ActiveRecord::Base.logger = ActiveSupport::Logger.new(nil)
417419
ActiveRecord.db_warnings_action = :log
418420

419421
# Mysql2 will raise an error when attempting to perform an update that warns if the sql_mode is set to strict
@@ -426,10 +428,13 @@ def test_warnings_do_not_change_returned_value_of_exec_update
426428
assert_equal 1, result
427429
ensure
428430
@conn.execute("SET @@SESSION.sql_mode='#{old_sql_mode}'")
431+
ActiveRecord::Base.logger = previous_logger
429432
ActiveRecord.db_warnings_action = @original_db_warnings_action
430433
end
431434

432435
def test_warnings_do_not_change_returned_value_of_exec_delete
436+
previous_logger = ActiveRecord::Base.logger
437+
ActiveRecord::Base.logger = ActiveSupport::Logger.new(nil)
433438
ActiveRecord.db_warnings_action = :log
434439

435440
# Mysql2 will raise an error when attempting to perform a delete that warns if the sql_mode is set to strict
@@ -442,6 +447,7 @@ def test_warnings_do_not_change_returned_value_of_exec_delete
442447
assert_equal 1, result
443448
ensure
444449
@conn.execute("SET @@SESSION.sql_mode='#{old_sql_mode}'")
450+
ActiveRecord::Base.logger = previous_logger
445451
ActiveRecord.db_warnings_action = @original_db_warnings_action
446452
end
447453

activerecord/test/cases/adapters/postgresql/create_unlogged_tables_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ class Thing < ActiveRecord::Base
1818
end
1919

2020
def setup
21+
@previous_unlogged_tables = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables
2122
@connection = ActiveRecord::Base.connection
2223
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = false
2324
end
2425

2526
teardown do
2627
@connection.drop_table TABLE_NAME, if_exists: true
27-
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = false
28+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = @previous_unlogged_tables
2829
end
2930

3031
def test_logged_by_default

activerecord/test/cases/adapters/postgresql/enum_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_assigning_enum_to_nil
101101
def test_schema_dump
102102
@connection.add_column "postgresql_enums", "good_mood", :mood, default: "happy", null: false
103103

104-
output = dump_all_table_schema
104+
output = dump_table_schema("postgresql_enums")
105105

106106
assert_includes output, "# Note that some types may not work with other database engines. Be careful if changing database."
107107

activerecord/test/cases/batches_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,14 @@ def test_each_should_execute_if_id_is_in_select
9292
end
9393

9494
def test_warn_if_order_scope_is_set
95+
previous_logger = ActiveRecord::Base.logger
96+
ActiveRecord::Base.logger = ActiveSupport::Logger.new(nil)
97+
9598
assert_called(ActiveRecord::Base.logger, :warn) do
9699
Post.order("title").find_each { |post| post }
97100
end
101+
ensure
102+
ActiveRecord::Base.logger = previous_logger
98103
end
99104

100105
def test_logger_not_required

activerecord/test/cases/fixtures_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,14 @@ def test_insert_with_datetime
440440
end
441441

442442
def test_logger_level_invariant
443+
previous_logger = ActiveRecord::Base.logger
444+
ActiveRecord::Base.logger = ActiveSupport::Logger.new(nil)
445+
443446
level = ActiveRecord::Base.logger.level
444447
create_fixtures("topics")
445448
assert_equal level, ActiveRecord::Base.logger.level
449+
ensure
450+
ActiveRecord::Base.logger = previous_logger
446451
end
447452

448453
def test_instantiation

activerecord/test/cases/helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
require "active_support/core_ext/kernel/reporting"
1212
require "active_support/core_ext/kernel/singleton_class"
1313

14+
if defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
15+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = true
16+
end
17+
1418
# TODO: Move all these random hacks into the ARTest namespace and into the support/ dir
1519

1620
Thread.abort_on_exception = true

activerecord/test/cases/schema_dumper_test.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def test_schema_dump_includes_limit_constraint_for_integer_columns
150150
end
151151

152152
def test_schema_dump_with_string_ignored_table
153-
output = dump_all_table_schema(["accounts"])
153+
output = dump_table_schema("authors")
154154
assert_no_match %r{create_table "accounts"}, output
155155
assert_match %r{create_table "authors"}, output
156156
assert_no_match %r{create_table "schema_migrations"}, output
@@ -350,13 +350,13 @@ def test_schema_dump_includes_extensions
350350
connection = ActiveRecord::Base.connection
351351

352352
connection.stub(:extensions, ["hstore"]) do
353-
output = dump_all_table_schema
353+
output = dump_all_table_schema(/./)
354354
assert_match "# These are extensions that must be enabled", output
355355
assert_match %r{enable_extension "hstore"}, output
356356
end
357357

358358
connection.stub(:extensions, []) do
359-
output = dump_all_table_schema
359+
output = dump_all_table_schema(/./)
360360
assert_no_match "# These are extensions that must be enabled", output
361361
assert_no_match %r{enable_extension}, output
362362
end
@@ -366,13 +366,13 @@ def test_schema_dump_includes_extensions_in_alphabetic_order
366366
connection = ActiveRecord::Base.connection
367367

368368
connection.stub(:extensions, ["hstore", "uuid-ossp", "xml2"]) do
369-
output = dump_all_table_schema
369+
output = dump_all_table_schema(/./)
370370
enabled_extensions = output.scan(%r{enable_extension "(.+)"}).flatten
371371
assert_equal ["hstore", "uuid-ossp", "xml2"], enabled_extensions
372372
end
373373

374374
connection.stub(:extensions, ["uuid-ossp", "xml2", "hstore"]) do
375-
output = dump_all_table_schema
375+
output = dump_all_table_schema(/./)
376376
enabled_extensions = output.scan(%r{enable_extension "(.+)"}).flatten
377377
assert_equal ["hstore", "uuid-ossp", "xml2"], enabled_extensions
378378
end
@@ -457,7 +457,7 @@ def test_schema_dump_with_table_name_prefix_and_suffix
457457
migration = CreateDogMigration.new
458458
migration.migrate(:up)
459459

460-
output = dump_all_table_schema
460+
output = dump_table_schema("dog_owners", "dogs")
461461
assert_no_match %r{create_table "foo_.+_bar"}, output
462462
assert_no_match %r{add_index "foo_.+_bar"}, output
463463
assert_no_match %r{create_table "schema_migrations"}, output
@@ -482,7 +482,7 @@ def test_schema_dump_with_table_name_prefix_and_suffix_regexp_escape
482482
migration = CreateDogMigration.new
483483
migration.migrate(:up)
484484

485-
output = dump_all_table_schema
485+
output = dump_table_schema("dog_owners", "dog")
486486
assert_no_match %r{create_table "foo\$.+\$bar"}, output
487487
assert_no_match %r{add_index "foo\$.+\$bar"}, output
488488
assert_no_match %r{create_table "schema_migrations"}, output
@@ -550,7 +550,7 @@ def down
550550
end
551551
migration.migrate(:up)
552552

553-
output = dump_all_table_schema
553+
output = dump_table_schema("timestamps")
554554
assert output.include?('t.datetime "this_should_remain_datetime"')
555555
assert output.include?('t.datetime "this_is_an_alias_of_datetime"')
556556
assert output.include?('t.datetime "without_time_zone"')
@@ -579,7 +579,7 @@ def down
579579
end
580580
migration.migrate(:up)
581581

582-
output = dump_all_table_schema
582+
output = dump_table_schema("timestamps")
583583
assert output.include?('t.datetime "this_should_remain_datetime"')
584584
assert output.include?('t.datetime "this_is_an_alias_of_datetime"')
585585
assert output.include?('t.timestamp "without_time_zone"')
@@ -607,7 +607,7 @@ def down
607607
end
608608
migration.migrate(:up)
609609

610-
output = dump_all_table_schema
610+
output = dump_table_schema("timestamps")
611611
assert output.include?('t.datetime "this_should_remain_datetime"')
612612
assert output.include?('t.datetime "this_is_an_alias_of_datetime"')
613613
assert output.include?('t.datetime "this_is_also_an_alias_of_datetime"')
@@ -634,7 +634,7 @@ def down
634634
end
635635
migration.migrate(:up)
636636

637-
output = dump_all_table_schema
637+
output = dump_table_schema("timestamps")
638638
# Normally we'd write `t.datetime` here. But because you've changed the `datetime_type`
639639
# to something else, `t.datetime` now means `:timestamptz`. To ensure that old columns
640640
# are still created as a `:timestamp` we need to change what is written to the schema dump.
@@ -668,15 +668,15 @@ def down
668668
end
669669
migration.migrate(:up)
670670

671-
output = dump_all_table_schema
671+
output = dump_table_schema("timestamps")
672672
assert output.include?('t.datetime "default_format"')
673673
assert output.include?('t.datetime "without_time_zone"')
674674
assert output.include?('t.timestamptz "with_time_zone"')
675675

676676
datetime_type_was = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type
677677
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type = :timestamptz
678678

679-
output = dump_all_table_schema
679+
output = dump_table_schema("timestamps")
680680
assert output.include?('t.timestamp "default_format"')
681681
assert output.include?('t.timestamp "without_time_zone"')
682682
assert output.include?('t.datetime "with_time_zone"')
@@ -704,7 +704,7 @@ def down
704704
end
705705
migration.migrate(:up)
706706

707-
output = dump_all_table_schema
707+
output = dump_table_schema("timestamps")
708708
assert output.include?('t.datetime "default_format"')
709709
assert output.include?('t.datetime "without_time_zone"')
710710
assert output.include?('t.datetime "also_without_time_zone"')
@@ -732,7 +732,7 @@ def down
732732
end
733733
migration.migrate(:up)
734734

735-
output = dump_all_table_schema
735+
output = dump_table_schema("timestamps")
736736
assert output.include?('t.datetime "default_format"')
737737
assert output.include?('t.datetime "without_time_zone"')
738738
assert output.include?('t.datetime "also_without_time_zone"')
@@ -759,7 +759,7 @@ def down
759759
end
760760
migration.migrate(:up)
761761

762-
output = dump_all_table_schema
762+
output = dump_table_schema("timestamps")
763763
assert output.include?('t.datetime "default_format"')
764764
assert output.include?('t.datetime "without_time_zone"')
765765
assert output.include?('t.datetime "also_without_time_zone"')
@@ -785,7 +785,7 @@ def down
785785
end
786786
migration.migrate(:up)
787787

788-
output = dump_all_table_schema
788+
output = dump_table_schema("timestamps")
789789
# Normally we'd write `t.datetime` here. But because you've changed the `datetime_type`
790790
# to something else, `t.datetime` now means `:timestamptz`. To ensure that old columns
791791
# are still created as a `:timestamp` we need to change what is written to the schema dump.
@@ -818,7 +818,7 @@ def down
818818
end
819819
migration.migrate(:up)
820820

821-
output = dump_all_table_schema
821+
output = dump_table_schema("timestamps")
822822
# Normally we'd write `t.datetime` here. But because you've changed the `datetime_type`
823823
# to something else, `t.datetime` now means `:timestamptz`. To ensure that old columns
824824
# are still created as a `:timestamp` we need to change what is written to the schema dump.

activerecord/test/support/connection.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ def self.test_configuration_hashes
2121
def self.connect
2222
ActiveRecord.async_query_executor = :global_thread_pool
2323
puts "Using #{connection_name}"
24-
ActiveRecord::Base.logger = ActiveSupport::Logger.new("debug.log", 1, 100 * 1024 * 1024)
24+
if ENV["CI"]
25+
ActiveRecord::Base.logger = nil
26+
else
27+
ActiveRecord::Base.logger = ActiveSupport::Logger.new("debug.log", 1, 100 * 1024 * 1024)
28+
end
2529
ActiveRecord::Base.configurations = test_configuration_hashes
2630
ActiveRecord::Base.establish_connection :arunit
2731
ARUnit2Model.establish_connection :arunit2

0 commit comments

Comments
 (0)