Skip to content

Commit

Permalink
Merge pull request #16349 from jmcnevin/master
Browse files Browse the repository at this point in the history
Correctly determine if migration is needed.
  • Loading branch information
rafaelfranca committed Aug 19, 2014
1 parent 06b185c commit 76b141b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
17 changes: 8 additions & 9 deletions activerecord/lib/active_record/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -826,21 +826,20 @@ def schema_migrations_table_name
SchemaMigration.table_name
end

def get_all_versions
SchemaMigration.all.map { |x| x.version.to_i }.sort
def get_all_versions(connection = Base.connection)
if connection.table_exists?(schema_migrations_table_name)
SchemaMigration.all.map { |x| x.version.to_i }.sort
else
[]
end
end

def current_version(connection = Base.connection)
sm_table = schema_migrations_table_name
if connection.table_exists?(sm_table)
get_all_versions.max || 0
else
0
end
get_all_versions(connection).max || 0
end

def needs_migration?(connection = Base.connection)
current_version(connection) < last_version
(migrations(migrations_paths).collect(&:version) - get_all_versions(connection)).size > 0
end

def last_version
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ def test_migrator_versions
assert_equal 0, ActiveRecord::Migrator.current_version
assert_equal 3, ActiveRecord::Migrator.last_version
assert_equal true, ActiveRecord::Migrator.needs_migration?

ActiveRecord::SchemaMigration.create!(:version => ActiveRecord::Migrator.last_version)
assert_equal true, ActiveRecord::Migrator.needs_migration?
ensure
ActiveRecord::Migrator.migrations_paths = old_path
end

def test_migration_detection_without_schema_migration_table
ActiveRecord::Base.connection.drop_table :schema_migrations

migrations_path = MIGRATIONS_ROOT + "/valid"
old_path = ActiveRecord::Migrator.migrations_paths
ActiveRecord::Migrator.migrations_paths = migrations_path

assert_equal true, ActiveRecord::Migrator.needs_migration?
ensure
ActiveRecord::Migrator.migrations_paths = old_path
end
Expand Down

0 comments on commit 76b141b

Please sign in to comment.