Skip to content

Commit

Permalink
Merge pull request #21841 from yui-knk/fix_migration_status
Browse files Browse the repository at this point in the history
Make `db:migrate:status` to render `1_some.rb` format migrate files.
  • Loading branch information
pixeltrix committed Nov 2, 2015
2 parents 22e1769 + a7beeb7 commit 38ddfef
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
41 changes: 41 additions & 0 deletions activerecord/CHANGELOG.md
@@ -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
`preprocess_order_args`.

Expand Down
12 changes: 10 additions & 2 deletions activerecord/lib/active_record/migration.rb
Expand Up @@ -477,6 +477,7 @@ def initialize(message = DEFAULT_MESSAGE)
class Migration
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
# loading a web page if config.active_record.migration_error is set to :page_load
Expand Down Expand Up @@ -995,14 +996,21 @@ def migrations_paths
Array(@migrations_paths)
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)
paths = Array(paths)

files = Dir[*paths.map { |p| "#{p}/**/[0-9]*_*.rb" }]

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
version = version.to_i
name = name.camelize
Expand Down
12 changes: 7 additions & 5 deletions activerecord/lib/active_record/railties/databases.rake
Expand Up @@ -100,12 +100,14 @@ db_namespace = namespace :db do

file_list =
ActiveRecord::Tasks::DatabaseTasks.migrations_paths.flat_map do |path|
# match "20091231235959_some_name.rb" and "001_some_name.rb" pattern
Dir.foreach(path).grep(/^(\d{3,})_(.+)\.rb$/) do
version = ActiveRecord::SchemaMigration.normalize_migration_number($1)
Dir.foreach(path).map do |file|
next unless ActiveRecord::Migrator.match_to_migration_filename?(file)

version, name, scope = ActiveRecord::Migrator.parse_migration_filename(file)
version = ActiveRecord::SchemaMigration.normalize_migration_number(version)
status = db_list.delete(version) ? 'up' : 'down'
[status, version, $2.humanize]
end
[status, version, (name + scope).humanize]
end.compact
end

db_list.map! do |version|
Expand Down
22 changes: 22 additions & 0 deletions railties/test/application/rake/migrations_test.rb
Expand Up @@ -154,6 +154,28 @@ class AMigration < ActiveRecord::Migration
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
add_to_config('config.active_record.dump_schema_after_migration = false')

Expand Down

0 comments on commit 38ddfef

Please sign in to comment.