diff options
author | Kir Shatrov <shatrov@me.com> | 2016-12-31 12:06:15 -0500 |
---|---|---|
committer | Kir Shatrov <shatrov@me.com> | 2017-01-01 19:23:23 -0500 |
commit | 6cd757963d7f6b9bb33704c82bd9ee2bacaa1e28 (patch) | |
tree | 18d015b92654915188f53a5e4c7ccde340360ff3 | |
parent | 233499b6d6f7df1b2b367ab2e0117021ac1e419b (diff) | |
download | rails-6cd757963d7f6b9bb33704c82bd9ee2bacaa1e28.tar.gz rails-6cd757963d7f6b9bb33704c82bd9ee2bacaa1e28.tar.bz2 rails-6cd757963d7f6b9bb33704c82bd9ee2bacaa1e28.zip |
Dump schema cache for custom connection
Today `rake db:schema:cache:dump` only supports dumping cache for a
single connection (`ActiveRecord::Base.connection`). This doesn't work
for apps with multiple databases.
This PR makes `DatabaseTasks` to provide an API for dumping schema cache
for any connection.
-rw-r--r-- | activerecord/lib/active_record/railties/databases.rake | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/tasks/database_tasks.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/tasks/database_tasks_test.rb | 10 |
3 files changed, 21 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 25d79a6c7d..cf1c0fb423 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -269,10 +269,7 @@ db_namespace = namespace :db do task dump: [:environment, :load_config] do conn = ActiveRecord::Base.connection filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml") - - conn.schema_cache.clear! - conn.data_sources.each { |table| conn.schema_cache.add(table) } - open(filename, "wb") { |f| f.write(YAML.dump(conn.schema_cache)) } + ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(conn, filename) end desc "Clears a db/schema_cache.yml file." diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 6f868b6b7e..1423e6008f 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -275,6 +275,16 @@ module ActiveRecord end end + # Dumps the schema cache in YAML format for the connection into the file + # + # ==== Examples: + # ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(ActiveRecord::Base.connection, "tmp/schema_dump.yaml") + def dump_schema_cache(conn, filename) + conn.schema_cache.clear! + conn.data_sources.each { |table| conn.schema_cache.add(table) } + open(filename, "wb") { |f| f.write(YAML.dump(conn.schema_cache)) } + end + private def class_for_adapter(adapter) diff --git a/activerecord/test/cases/tasks/database_tasks_test.rb b/activerecord/test/cases/tasks/database_tasks_test.rb index 0c3ca41c9b..f7c53b5801 100644 --- a/activerecord/test/cases/tasks/database_tasks_test.rb +++ b/activerecord/test/cases/tasks/database_tasks_test.rb @@ -85,6 +85,16 @@ module ActiveRecord end end + class DatabaseTasksDumpSchemaCacheTest < ActiveRecord::TestCase + def test_dump_schema_cache + path = "/tmp/my_schema_cache.yml" + ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(ActiveRecord::Base.connection, path) + assert File.file?(path) + ensure + FileUtils.rm_rf(path) + end + end + class DatabaseTasksCreateAllTest < ActiveRecord::TestCase def setup @configurations = { "development" => { "database" => "my-db" } } |