未验证 提交 dde620ac 编写于 作者: K Kasper Timm Hansen 提交者: GitHub

Merge pull request #31311 from y-yagi/ignore_no_database_error_when_loading_schema_cache

Ignore `NoDatabaseError` when loading schema cache
......@@ -1053,7 +1053,15 @@ def get_all_versions(connection = Base.connection)
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
......
......@@ -90,12 +90,15 @@ class Railtie < Rails::Railtie # :nodoc:
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
......
......@@ -98,6 +98,20 @@ def with_bad_permissions
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)
......
......@@ -381,6 +381,21 @@ def use_frameworks(arr)
$:.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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册