schema loading rake tasks maintain database connection for current env.

[Joshua Cody & Yves Senn]

Closes #16757.

Prior to this patch schema loading rake tasks had the potential to leak a
connection to a different database. This had side-effects when rake tasks
operating on the current connection (like `db:seed`) were chained.
This commit is contained in:
Yves Senn 2014-09-03 17:31:38 +02:00
parent e2ce4c7aa3
commit ded17a498a
4 changed files with 36 additions and 0 deletions

View File

@ -1,3 +1,10 @@
* Schema loading rake tasks (like `db:schema:load` and `db:setup`) maintain
the database connection to the current environment.
Fixes #16757.
*Joshua Cody*, *Yves Senn*
* MySQL: set the connection collation along with the charset.
Sets the connection collation to the database collation configured in

View File

@ -171,6 +171,7 @@ module ActiveRecord
each_current_configuration(environment) { |configuration|
purge configuration
}
ActiveRecord::Base.establish_connection(environment.to_sym)
end
def structure_dump(*arguments)
@ -217,6 +218,7 @@ module ActiveRecord
each_current_configuration(environment) { |configuration|
load_schema_for configuration, format, file
}
ActiveRecord::Base.establish_connection(environment.to_sym)
end
def check_schema_file(filename)

View File

@ -309,6 +309,7 @@ module ActiveRecord
ActiveRecord::Tasks::DatabaseTasks.expects(:purge).
with('database' => 'prod-db')
ActiveRecord::Base.expects(:establish_connection).with(:production)
ActiveRecord::Tasks::DatabaseTasks.purge_current('production')
end

View File

@ -173,6 +173,32 @@ module ApplicationTests
"your test schema automatically, see the release notes for details.\n", output
end
end
test 'db:setup loads schema and seeds database' do
begin
@old_env = ENV["RAILS_ENV"]
ENV.delete "RAILS_ENV"
app_file 'db/schema.rb', <<-RUBY
ActiveRecord::Schema.define(version: "1") do
create_table :users do |t|
t.string :name
end
end
RUBY
app_file 'db/seeds.rb', <<-RUBY
puts ActiveRecord::Base.connection_config[:database]
RUBY
Dir.chdir(app_path) do
database_path = `bundle exec rake db:setup`
assert_equal "development.sqlite3", File.basename(database_path.strip)
end
ensure
ENV["RAILS_ENV"] = @old_env
end
end
end
end
end