提交 ded17a49 编写于 作者: Y Yves Senn

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.
上级 e2ce4c7a
* 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
......
......@@ -171,6 +171,7 @@ def purge_current(environment = env)
each_current_configuration(environment) { |configuration|
purge configuration
}
ActiveRecord::Base.establish_connection(environment.to_sym)
end
def structure_dump(*arguments)
......@@ -217,6 +218,7 @@ def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, e
each_current_configuration(environment) { |configuration|
load_schema_for configuration, format, file
}
ActiveRecord::Base.establish_connection(environment.to_sym)
end
def check_schema_file(filename)
......
......@@ -309,6 +309,7 @@ def test_purges_current_environment_database
ActiveRecord::Tasks::DatabaseTasks.expects(:purge).
with('database' => 'prod-db')
ActiveRecord::Base.expects(:establish_connection).with(:production)
ActiveRecord::Tasks::DatabaseTasks.purge_current('production')
end
......
......@@ -173,6 +173,32 @@ def db_test_load_structure
"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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册