diff options
author | yuuji.yaginuma <yuuji.yaginuma@gmail.com> | 2017-12-01 18:16:06 +0900 |
---|---|---|
committer | yuuji.yaginuma <yuuji.yaginuma@gmail.com> | 2017-12-03 09:50:28 +0900 |
commit | dbee80bca0ef504120219e6c7686437456511060 (patch) | |
tree | 0b7db3246f94a7df6ca928c1a8a7bf8515e6bc97 | |
parent | 33a3f7123bc4cc49b99b01a40bbfd463b2e73f76 (diff) | |
download | rails-dbee80bca0ef504120219e6c7686437456511060.tar.gz rails-dbee80bca0ef504120219e6c7686437456511060.tar.bz2 rails-dbee80bca0ef504120219e6c7686437456511060.zip |
Make `Migrator.current_version` work without a current database
This is necessary in order to make the processing dependent on
`Migrator.current_version` work even without database.
Context: https://github.com/rails/rails/pull/31135#issuecomment-348404326
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/railtie.rb | 7 | ||||
-rw-r--r-- | railties/test/application/rake/dbs_test.rb | 14 | ||||
-rw-r--r-- | railties/test/isolation/abstract_unit.rb | 15 |
4 files changed, 43 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 15e9c09ffb..4d4b0dc67a 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -1053,7 +1053,15 @@ module ActiveRecord end end - def current_version(connection = Base.connection) + def current_version(connection = nil) + if connection.nil? + begin + connection = Base.connection + rescue ActiveRecord::NoDatabaseError + return nil + end + end + get_all_versions(connection).max || 0 end diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 9ee8425e1b..4538ed6a5f 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -90,12 +90,15 @@ module ActiveRecord filename = File.join(app.config.paths["db"].first, "schema_cache.yml") if File.file?(filename) + current_version = ActiveRecord::Migrator.current_version + next if current_version.nil? + cache = YAML.load(File.read(filename)) - if cache.version == ActiveRecord::Migrator.current_version + if cache.version == current_version connection.schema_cache = cache connection_pool.schema_cache = cache.dup else - warn "Ignoring db/schema_cache.yml because it has expired. The current schema version is #{ActiveRecord::Migrator.current_version}, but the one in the cache is #{cache.version}." + warn "Ignoring db/schema_cache.yml because it has expired. The current schema version is #{current_version}, but the one in the cache is #{cache.version}." end end end diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index 0235210fdd..2082e9fa9f 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -98,6 +98,20 @@ module ApplicationTests end end + test "db:create works when schema cache exists and database does not exist" do + use_postgresql + + begin + rails %w(db:create db:migrate db:schema:cache:dump) + + rails "db:drop" + rails "db:create" + assert_equal 0, $?.exitstatus + ensure + rails "db:drop" rescue nil + end + end + test "db:drop failure because database does not exist" do output = rails("db:drop:_unsafe", "--trace") assert_match(/does not exist/, output) diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 7522237a38..5b1c06d4e5 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -381,6 +381,21 @@ module TestHelpers $:.reject! { |path| path =~ %r'/(#{to_remove.join('|')})/' } end + + def use_postgresql + File.open("#{app_path}/config/database.yml", "w") do |f| + f.puts <<-YAML + default: &default + adapter: postgresql + pool: 5 + database: railties_test + development: + <<: *default + test: + <<: *default + YAML + end + end end end |