Skip to content

Commit

Permalink
Make Migrator.current_version work without a current database
Browse files Browse the repository at this point in the history
This is necessary in order to make the processing dependent on
`Migrator.current_version` work even without database.

Context: rails#31135 (comment)
  • Loading branch information
y-yagi committed Dec 3, 2017
1 parent 33a3f71 commit dbee80b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
10 changes: 9 additions & 1 deletion activerecord/lib/active_record/migration.rb
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions activerecord/lib/active_record/railtie.rb
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions railties/test/application/rake/dbs_test.rb
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions railties/test/isolation/abstract_unit.rb
Expand Up @@ -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

This comment has been minimized.

Copy link
@bogdanvlviv

bogdanvlviv Apr 23, 2018

@y-yagi wouldn't it be the same if use sqlite3 instead of postgresql since on some environments it can't work without applying changes here(that isn't ok since it creates git-diff) because it doesn't rely on activerecord/test/config.yml ?

pool: 5
database: railties_test
development:
<<: *default
test:
<<: *default
YAML
end
end
end
end

Expand Down

0 comments on commit dbee80b

Please sign in to comment.