Skip to content

Commit fd7c773

Browse files
authored
Merge pull request rails#46067 from eileencodes/dont-delegate-tasks-to-base
Don't delegate tasks to ActiveRecord::Base
2 parents 6ffa34d + c74b290 commit fd7c773

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

activerecord/lib/active_record/tasks/mysql_database_tasks.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ module Tasks # :nodoc:
55
class MySQLDatabaseTasks # :nodoc:
66
ER_DB_CREATE_EXISTS = 1007
77

8-
delegate :connection, :establish_connection, to: ActiveRecord::Base
9-
108
def self.using_database_configurations?
119
true
1210
end
@@ -19,11 +17,11 @@ def initialize(db_config)
1917
def create
2018
establish_connection(configuration_hash_without_database)
2119
connection.create_database(db_config.database, creation_options)
22-
establish_connection(db_config)
20+
establish_connection
2321
end
2422

2523
def drop
26-
establish_connection(db_config)
24+
establish_connection
2725
connection.drop_database(db_config.database)
2826
end
2927

@@ -71,6 +69,14 @@ def structure_load(filename, extra_flags)
7169
private
7270
attr_reader :db_config, :configuration_hash
7371

72+
def connection
73+
ActiveRecord::Base.connection
74+
end
75+
76+
def establish_connection(config = db_config)
77+
ActiveRecord::Base.establish_connection(config)
78+
end
79+
7480
def configuration_hash_without_database
7581
configuration_hash.merge(database: nil)
7682
end

activerecord/lib/active_record/tasks/postgresql_database_tasks.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ class PostgreSQLDatabaseTasks # :nodoc:
99
ON_ERROR_STOP_1 = "ON_ERROR_STOP=1"
1010
SQL_COMMENT_BEGIN = "--"
1111

12-
delegate :connection, :establish_connection, :clear_active_connections!,
13-
to: ActiveRecord::Base
14-
1512
def self.using_database_configurations?
1613
true
1714
end
@@ -21,14 +18,14 @@ def initialize(db_config)
2118
@configuration_hash = db_config.configuration_hash
2219
end
2320

24-
def create(master_established = false)
25-
establish_master_connection unless master_established
21+
def create(connection_already_established = false)
22+
establish_connection(public_schema_config) unless connection_already_established
2623
connection.create_database(db_config.database, configuration_hash.merge(encoding: encoding))
27-
establish_connection(db_config)
24+
establish_connection
2825
end
2926

3027
def drop
31-
establish_master_connection
28+
establish_connection(public_schema_config)
3229
connection.drop_database(db_config.database)
3330
end
3431

@@ -41,7 +38,7 @@ def collation
4138
end
4239

4340
def purge
44-
clear_active_connections!
41+
ActiveRecord::Base.connection_handler.clear_active_connections!(:all)
4542
drop
4643
create true
4744
end
@@ -90,15 +87,20 @@ def structure_load(filename, extra_flags)
9087
private
9188
attr_reader :db_config, :configuration_hash
9289

90+
def connection
91+
ActiveRecord::Base.connection
92+
end
93+
94+
def establish_connection(config = db_config)
95+
ActiveRecord::Base.establish_connection(config)
96+
end
97+
9398
def encoding
9499
configuration_hash[:encoding] || DEFAULT_ENCODING
95100
end
96101

97-
def establish_master_connection
98-
establish_connection configuration_hash.merge(
99-
database: "postgres",
100-
schema_search_path: "public"
101-
)
102+
def public_schema_config
103+
configuration_hash.merge(database: "postgres", schema_search_path: "public")
102104
end
103105

104106
def psql_env

activerecord/lib/active_record/tasks/sqlite_database_tasks.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
module ActiveRecord
44
module Tasks # :nodoc:
55
class SQLiteDatabaseTasks # :nodoc:
6-
delegate :connection, :establish_connection, to: ActiveRecord::Base
7-
86
def self.using_database_configurations?
97
true
108
end
@@ -17,7 +15,7 @@ def initialize(db_config, root = ActiveRecord::Tasks::DatabaseTasks.root)
1715
def create
1816
raise DatabaseAlreadyExists if File.exist?(db_config.database)
1917

20-
establish_connection(db_config)
18+
establish_connection
2119
connection
2220
end
2321

@@ -68,6 +66,14 @@ def structure_load(filename, extra_flags)
6866
private
6967
attr_reader :db_config, :root
7068

69+
def connection
70+
ActiveRecord::Base.connection
71+
end
72+
73+
def establish_connection(config = db_config)
74+
ActiveRecord::Base.establish_connection(config)
75+
end
76+
7177
def run_cmd(cmd, args, out)
7278
fail run_cmd_error(cmd, args) unless Kernel.system(cmd, *args, out: out)
7379
end

activerecord/test/cases/tasks/postgresql_rake_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ def drop_database(*); end
208208

209209
def test_clears_active_connections
210210
with_stubbed_connection do
211-
ActiveRecord::Base.stub(:establish_connection, nil) do
212-
assert_called(ActiveRecord::Base, :clear_active_connections!) do
211+
ActiveRecord::Base.connection_handler.stub(:establish_connection, nil) do
212+
assert_called(ActiveRecord::Base.connection_handler, :clear_active_connections!) do
213213
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
214214
end
215215
end

0 commit comments

Comments
 (0)