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

Merge pull request #21841 from yui-knk/fix_migration_status

Make `db:migrate:status` to render `1_some.rb` format migrate files.
This commit is contained in:
Andrew White 2015-11-02 15:12:12 +00:00
commit 38ddfefe54
4 changed files with 80 additions and 7 deletions

View file

@ -1,3 +1,44 @@
* Make `db:migrate:status` to render `1_some.rb` format migrate files.
These files are in `db/migrate`:
* 1_valid_people_have_last_names.rb
* 20150819202140_irreversible_migration.rb
* 20150823202140_add_admin_flag_to_users.rb
* 20150823202141_migration_tests.rb
* 2_we_need_reminders.rb
* 3_innocent_jointable.rb
Before:
$ bundle exec rake db:migrate:status
...
Status Migration ID Migration Name
--------------------------------------------------
up 001 ********** NO FILE **********
up 002 ********** NO FILE **********
up 003 ********** NO FILE **********
up 20150819202140 Irreversible migration
up 20150823202140 Add admin flag to users
up 20150823202141 Migration tests
After:
$ bundle exec rake db:migrate:status
...
Status Migration ID Migration Name
--------------------------------------------------
up 001 Valid people have last names
up 002 We need reminders
up 003 Innocent jointable
up 20150819202140 Irreversible migration
up 20150823202140 Add admin flag to users
up 20150823202141 Migration tests
*Yuichiro Kaneko*
* Define `ActiveRecord::Sanitization.sanitize_sql_for_order` and use it inside * Define `ActiveRecord::Sanitization.sanitize_sql_for_order` and use it inside
`preprocess_order_args`. `preprocess_order_args`.

View file

@ -477,6 +477,7 @@ module ActiveRecord
class Migration class Migration
autoload :CommandRecorder, 'active_record/migration/command_recorder' autoload :CommandRecorder, 'active_record/migration/command_recorder'
MigrationFilenameRegexp = /\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/ # :nodoc:
# This class is used to verify that all migrations have been run before # This class is used to verify that all migrations have been run before
# loading a web page if config.active_record.migration_error is set to :page_load # loading a web page if config.active_record.migration_error is set to :page_load
@ -995,14 +996,21 @@ module ActiveRecord
Array(@migrations_paths) Array(@migrations_paths)
end end
def match_to_migration_filename?(filename) # :nodoc:
File.basename(filename) =~ Migration::MigrationFilenameRegexp
end
def parse_migration_filename(filename) # :nodoc:
File.basename(filename).scan(Migration::MigrationFilenameRegexp).first
end
def migrations(paths) def migrations(paths)
paths = Array(paths) paths = Array(paths)
files = Dir[*paths.map { |p| "#{p}/**/[0-9]*_*.rb" }] files = Dir[*paths.map { |p| "#{p}/**/[0-9]*_*.rb" }]
migrations = files.map do |file| migrations = files.map do |file|
version, name, scope = file.scan(/([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/).first version, name, scope = parse_migration_filename(file)
raise IllegalMigrationNameError.new(file) unless version raise IllegalMigrationNameError.new(file) unless version
version = version.to_i version = version.to_i
name = name.camelize name = name.camelize

View file

@ -100,12 +100,14 @@ db_namespace = namespace :db do
file_list = file_list =
ActiveRecord::Tasks::DatabaseTasks.migrations_paths.flat_map do |path| ActiveRecord::Tasks::DatabaseTasks.migrations_paths.flat_map do |path|
# match "20091231235959_some_name.rb" and "001_some_name.rb" pattern Dir.foreach(path).map do |file|
Dir.foreach(path).grep(/^(\d{3,})_(.+)\.rb$/) do next unless ActiveRecord::Migrator.match_to_migration_filename?(file)
version = ActiveRecord::SchemaMigration.normalize_migration_number($1)
version, name, scope = ActiveRecord::Migrator.parse_migration_filename(file)
version = ActiveRecord::SchemaMigration.normalize_migration_number(version)
status = db_list.delete(version) ? 'up' : 'down' status = db_list.delete(version) ? 'up' : 'down'
[status, version, $2.humanize] [status, version, (name + scope).humanize]
end end.compact
end end
db_list.map! do |version| db_list.map! do |version|

View file

@ -154,6 +154,28 @@ module ApplicationTests
end end
end end
test 'running migrations with not timestamp head migration files' do
Dir.chdir(app_path) do
app_file "db/migrate/1_one_migration.rb", <<-MIGRATION
class OneMigration < ActiveRecord::Migration
end
MIGRATION
app_file "db/migrate/02_two_migration.rb", <<-MIGRATION
class TwoMigration < ActiveRecord::Migration
end
MIGRATION
`bin/rake db:migrate`
output = `bin/rake db:migrate:status`
assert_match(/up\s+001\s+One migration/, output)
assert_match(/up\s+002\s+Two migration/, output)
end
end
test 'schema generation when dump_schema_after_migration is set' do test 'schema generation when dump_schema_after_migration is set' do
add_to_config('config.active_record.dump_schema_after_migration = false') add_to_config('config.active_record.dump_schema_after_migration = false')