Skip to content

Commit 1df47b8

Browse files
committed
Extract AbstractAdapter#schema_version
We don't want migration_context in general to be public API, but this is a reasonable question for a user to ask. Similarly, schema cache shouldn't need to reach into private details of the migrations API, and this way it doesn't have to.
1 parent d4d16e2 commit 1df47b8

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,14 @@ def database_version # :nodoc:
641641
def check_version # :nodoc:
642642
end
643643

644+
# Returns the version identifier of the schema currently available in
645+
# the database. This is generally equal to the number of the highest-
646+
# numbered migration that has been executed, or 0 if no schema
647+
# information is present / the database is empty.
648+
def schema_version
649+
migration_context.current_version
650+
end
651+
644652
def field_ordered_value(column, values) # :nodoc:
645653
node = Arel::Nodes::Case.new(column)
646654
values.each.with_index(1) do |value, order|

activerecord/lib/active_record/connection_adapters/schema_cache.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def ignored_table?(table_name)
208208
end
209209

210210
def reset_version!
211-
@version = connection.migration_context.current_version
211+
@version = connection.schema_version
212212
end
213213

214214
def derive_columns_hash_and_deduplicate_values

activerecord/lib/active_record/railties/databases.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ db_namespace = namespace :db do
332332

333333
desc "Retrieves the current schema version number"
334334
task version: :load_config do
335-
puts "Current version: #{ActiveRecord::Base.connection.migration_context.current_version}"
335+
puts "Current version: #{ActiveRecord::Base.connection.schema_version}"
336336
end
337337

338338
# desc "Raises an error if there are pending migrations"

activerecord/test/cases/ar_schema_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_schema_define
4949

5050
assert_nothing_raised { @connection.select_all "SELECT * FROM fruits" }
5151
assert_nothing_raised { @connection.select_all "SELECT * FROM schema_migrations" }
52-
assert_equal 7, @connection.migration_context.current_version
52+
assert_equal 7, @connection.schema_version
5353
end
5454

5555
def test_schema_define_with_table_name_prefix

0 commit comments

Comments
 (0)