Skip to content

Commit

Permalink
Add additional raise UnknownMigrationVersionError
Browse files Browse the repository at this point in the history
Raise error on the movement of migrations
when the current migration does not exist.
  • Loading branch information
bogdanvlviv committed Apr 19, 2017
1 parent b77d2aa commit bb9d6eb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Raise error `UnknownMigrationVersionError` on the movement of migrations
when the current migration does not exist.

*bogdanvlviv*

* Fix `bin/rails db:forward` first migration.

*bogdanvlviv*
Expand Down
12 changes: 7 additions & 5 deletions activerecord/lib/active_record/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1105,18 +1105,20 @@ def migration_files(paths)
def move(direction, migrations_paths, steps)
migrator = new(direction, migrations(migrations_paths))

if current_version != 0 && !migrator.current_migration
raise UnknownMigrationVersionError.new(current_version)
end

start_index =
if current_version == 0
0
else
migrator.migrations.index(migrator.current_migration)
end

if start_index
finish = migrator.migrations[start_index + steps]
version = finish ? finish.version : 0
send(direction, migrations_paths, version)
end
finish = migrator.migrations[start_index + steps]
version = finish ? finish.version : 0
send(direction, migrations_paths, version)
end
end

Expand Down
32 changes: 31 additions & 1 deletion railties/test/application/rake/migrations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,37 @@ class AMigration < ActiveRecord::Migration::Current
end
end

test "raise error on any move when current migration does not exist" do
Dir.chdir(app_path) do
`bin/rails generate model user username:string password:string;
bin/rails generate migration add_email_to_users email:string;
bin/rails db:migrate
rm db/migrate/*email*.rb`

output = `bin/rails db:migrate:status`
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+\** NO FILE \**/, output)

output = `bin/rails db:rollback 2>&1`
assert_match(/rails aborted!/, output)
assert_match(/ActiveRecord::UnknownMigrationVersionError:/, output)
assert_match(/No migration with version number\s\d{14}\./, output)

output = `bin/rails db:migrate:status`
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+\** NO FILE \**/, output)

output = `bin/rails db:forward 2>&1`
assert_match(/rails aborted!/, output)
assert_match(/ActiveRecord::UnknownMigrationVersionError:/, output)
assert_match(/No migration with version number\s\d{14}\./, output)

output = `bin/rails db:migrate:status`
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+\** NO FILE \**/, output)
end
end

test "migration status after rollback and redo without timestamps" do
add_to_config("config.active_record.timestamped_migrations = false")

Expand Down Expand Up @@ -257,7 +288,6 @@ class TwoMigration < ActiveRecord::Migration::Current
rm db/migrate/*email*.rb`

output = `bin/rails db:migrate:status`
File.write("test.txt", output)

assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+\** NO FILE \**/, output)
Expand Down

0 comments on commit bb9d6eb

Please sign in to comment.