Skip to content

Commit ee94a5c

Browse files
committed
Move AR adapter related helper methods under AR::TestCase
These helpers are called from both test class definition and test execution, so we're defining them as both class and instance methods on AR::TestCase.
1 parent a18d90e commit ee94a5c

File tree

3 files changed

+83
-77
lines changed

3 files changed

+83
-77
lines changed

activerecord/test/cases/helper.rb

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -33,83 +33,6 @@
3333
ActiveRecord.raise_on_assign_to_attr_readonly = true
3434
ActiveRecord.belongs_to_required_validates_foreign_key = false
3535

36-
def current_adapter?(*types)
37-
types.any? do |type|
38-
ActiveRecord::ConnectionAdapters.const_defined?(type) &&
39-
ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters.const_get(type))
40-
end
41-
end
42-
43-
def in_memory_db?
44-
current_adapter?(:SQLite3Adapter) &&
45-
ActiveRecord::Base.connection_pool.db_config.database == ":memory:"
46-
end
47-
48-
def mysql_enforcing_gtid_consistency?
49-
current_adapter?(:Mysql2Adapter) && "ON" == ActiveRecord::Base.connection.show_variable("enforce_gtid_consistency")
50-
end
51-
52-
def supports_default_expression?
53-
if current_adapter?(:PostgreSQLAdapter)
54-
true
55-
elsif current_adapter?(:Mysql2Adapter)
56-
conn = ActiveRecord::Base.connection
57-
!conn.mariadb? && conn.database_version >= "8.0.13"
58-
end
59-
end
60-
61-
def supports_non_unique_constraint_name?
62-
if current_adapter?(:Mysql2Adapter)
63-
conn = ActiveRecord::Base.connection
64-
conn.mariadb?
65-
else
66-
false
67-
end
68-
end
69-
70-
def supports_text_column_with_default?
71-
if current_adapter?(:Mysql2Adapter)
72-
conn = ActiveRecord::Base.connection
73-
conn.mariadb? && conn.database_version >= "10.2.1"
74-
else
75-
true
76-
end
77-
end
78-
79-
%w[
80-
supports_savepoints?
81-
supports_partial_index?
82-
supports_partitioned_indexes?
83-
supports_expression_index?
84-
supports_insert_returning?
85-
supports_insert_on_duplicate_skip?
86-
supports_insert_on_duplicate_update?
87-
supports_insert_conflict_target?
88-
supports_optimizer_hints?
89-
supports_datetime_with_precision?
90-
].each do |method_name|
91-
define_method method_name do
92-
ActiveRecord::Base.connection.public_send(method_name)
93-
end
94-
end
95-
96-
def enable_extension!(extension, connection)
97-
return false unless connection.supports_extensions?
98-
return connection.reconnect! if connection.extension_enabled?(extension)
99-
100-
connection.enable_extension extension
101-
connection.commit_db_transaction if connection.transaction_open?
102-
connection.reconnect!
103-
end
104-
105-
def disable_extension!(extension, connection)
106-
return false unless connection.supports_extensions?
107-
return true unless connection.extension_enabled?(extension)
108-
109-
connection.disable_extension(extension, force: :cascade)
110-
connection.reconnect!
111-
end
112-
11336
def load_schema
11437
# silence verbose schema loading
11538
original_stdout = $stdout

activerecord/test/cases/test_case.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require "active_record/fixtures"
99

1010
require "cases/validations_repair_helper"
11+
require_relative "../support/adapter_helper"
1112

1213
module ActiveRecord
1314
# = Active Record Test Case
@@ -18,6 +19,8 @@ class TestCase < ActiveSupport::TestCase # :nodoc:
1819
include ActiveSupport::Testing::Stream
1920
include ActiveRecord::TestFixtures
2021
include ActiveRecord::ValidationsRepairHelper
22+
include AdapterHelper
23+
extend AdapterHelper
2124

2225
self.fixture_path = FIXTURES_ROOT
2326
self.use_instantiated_fixtures = false
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# frozen_string_literal: true
2+
3+
module AdapterHelper
4+
def current_adapter?(*types)
5+
types.any? do |type|
6+
ActiveRecord::ConnectionAdapters.const_defined?(type) &&
7+
ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters.const_get(type))
8+
end
9+
end
10+
11+
def in_memory_db?
12+
current_adapter?(:SQLite3Adapter) &&
13+
ActiveRecord::Base.connection_pool.db_config.database == ":memory:"
14+
end
15+
16+
def mysql_enforcing_gtid_consistency?
17+
current_adapter?(:Mysql2Adapter) && "ON" == ActiveRecord::Base.connection.show_variable("enforce_gtid_consistency")
18+
end
19+
20+
def supports_default_expression?
21+
if current_adapter?(:PostgreSQLAdapter)
22+
true
23+
elsif current_adapter?(:Mysql2Adapter)
24+
conn = ActiveRecord::Base.connection
25+
!conn.mariadb? && conn.database_version >= "8.0.13"
26+
end
27+
end
28+
29+
def supports_non_unique_constraint_name?
30+
if current_adapter?(:Mysql2Adapter)
31+
conn = ActiveRecord::Base.connection
32+
conn.mariadb?
33+
else
34+
false
35+
end
36+
end
37+
38+
def supports_text_column_with_default?
39+
if current_adapter?(:Mysql2Adapter)
40+
conn = ActiveRecord::Base.connection
41+
conn.mariadb? && conn.database_version >= "10.2.1"
42+
else
43+
true
44+
end
45+
end
46+
47+
%w[
48+
supports_savepoints?
49+
supports_partial_index?
50+
supports_partitioned_indexes?
51+
supports_expression_index?
52+
supports_insert_returning?
53+
supports_insert_on_duplicate_skip?
54+
supports_insert_on_duplicate_update?
55+
supports_insert_conflict_target?
56+
supports_optimizer_hints?
57+
supports_datetime_with_precision?
58+
].each do |method_name|
59+
define_method method_name do
60+
ActiveRecord::Base.connection.public_send(method_name)
61+
end
62+
end
63+
64+
def enable_extension!(extension, connection)
65+
return false unless connection.supports_extensions?
66+
return connection.reconnect! if connection.extension_enabled?(extension)
67+
68+
connection.enable_extension extension
69+
connection.commit_db_transaction if connection.transaction_open?
70+
connection.reconnect!
71+
end
72+
73+
def disable_extension!(extension, connection)
74+
return false unless connection.supports_extensions?
75+
return true unless connection.extension_enabled?(extension)
76+
77+
connection.disable_extension(extension, force: :cascade)
78+
connection.reconnect!
79+
end
80+
end

0 commit comments

Comments
 (0)