1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #28166 from bogdanvlviv/fix_migration_tasks

Fix migration tasks
This commit is contained in:
Rafael França 2017-04-19 19:33:37 -04:00 committed by GitHub
commit 7ce850cd62
3 changed files with 78 additions and 6 deletions

View file

@ -1,3 +1,12 @@
* Raise error `UnknownMigrationVersionError` on the movement of migrations
when the current migration does not exist.
*bogdanvlviv*
* Fix `bin/rails db:forward` first migration.
*bogdanvlviv*
* Support Descending Indexes for MySQL.
MySQL 8.0.1 and higher supports descending indexes: `DESC` in an index definition is no longer ignored.

View file

@ -1104,13 +1104,21 @@ module ActiveRecord
def move(direction, migrations_paths, steps)
migrator = new(direction, migrations(migrations_paths))
start_index = migrator.migrations.index(migrator.current_migration)
if start_index
finish = migrator.migrations[start_index + steps]
version = finish ? finish.version : 0
send(direction, migrations_paths, version)
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
finish = migrator.migrations[start_index + steps]
version = finish ? finish.version : 0
send(direction, migrations_paths, version)
end
end

View file

@ -142,6 +142,62 @@ module ApplicationTests
end
end
test "migration status after rollback and forward" 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`
output = `bin/rails db:migrate:status`
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+Add email to users/, output)
`bin/rails db:rollback STEP=2`
output = `bin/rails db:migrate:status`
assert_match(/down\s+\d{14}\s+Create users/, output)
assert_match(/down\s+\d{14}\s+Add email to users/, output)
`bin/rails db:forward STEP=2`
output = `bin/rails db:migrate:status`
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+Add email to users/, output)
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")
@ -232,7 +288,6 @@ module ApplicationTests
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)