Skip to content

Commit

Permalink
Merge pull request #5076 from petmit/migrate_status_without_timestamps
Browse files Browse the repository at this point in the history
Correctly print names of non-timestamped migrations with db:migrate:status
  • Loading branch information
tenderlove committed Feb 17, 2012
2 parents b9ad097 + 910c59f commit 663c9f3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
5 changes: 3 additions & 2 deletions activerecord/lib/active_record/railties/databases.rake
Expand Up @@ -207,11 +207,12 @@ db_namespace = namespace :db do
next # means "return" for rake task
end
db_list = ActiveRecord::Base.connection.select_values("SELECT version FROM #{ActiveRecord::Migrator.schema_migrations_table_name}")
db_list.map! { |version| "%.3d" % version }
file_list = []
ActiveRecord::Migrator.migrations_paths.each do |path|
Dir.foreach(path) do |file|
# only files matching "20091231235959_some_name.rb" pattern
if match_data = /^(\d{14})_(.+)\.rb$/.match(file)
# match "20091231235959_some_name.rb" and "001_some_name.rb" pattern
if match_data = /^(\d{3,})_(.+)\.rb$/.match(file)
status = db_list.delete(match_data[1]) ? 'up' : 'down'
file_list << [status, match_data[1], match_data[2].humanize]
end
Expand Down
50 changes: 49 additions & 1 deletion railties/test/application/rake/migrations_test.rb
Expand Up @@ -67,7 +67,7 @@ class AMigration < ActiveRecord::Migration
`rails generate migration add_email_to_users email:string`
end

Dir.chdir(app_path) { `rake db:migrate`}
Dir.chdir(app_path) { `rake db:migrate` }
output = Dir.chdir(app_path) { `rake db:migrate:status` }

assert_match(/up\s+\d{14}\s+Create users/, output)
Expand All @@ -80,6 +80,27 @@ class AMigration < ActiveRecord::Migration
assert_match(/down\s+\d{14}\s+Add email to users/, output)
end

test 'migration status without timestamps' do
add_to_config('config.active_record.timestamped_migrations = false')

Dir.chdir(app_path) do
`rails generate model user username:string password:string`
`rails generate migration add_email_to_users email:string`
end

Dir.chdir(app_path) { `rake db:migrate` }
output = Dir.chdir(app_path) { `rake db:migrate:status` }

assert_match(/up\s+\d{3,}\s+Create users/, output)
assert_match(/up\s+\d{3,}\s+Add email to users/, output)

Dir.chdir(app_path) { `rake db:rollback STEP=1` }
output = Dir.chdir(app_path) { `rake db:migrate:status` }

assert_match(/up\s+\d{3,}\s+Create users/, output)
assert_match(/down\s+\d{3,}\s+Add email to users/, output)
end

test 'test migration status after rollback and redo' do
Dir.chdir(app_path) do
`rails generate model user username:string password:string`
Expand All @@ -104,6 +125,33 @@ class AMigration < ActiveRecord::Migration
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+Add email to users/, output)
end

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

Dir.chdir(app_path) do
`rails generate model user username:string password:string`
`rails generate migration add_email_to_users email:string`
end

Dir.chdir(app_path) { `rake db:migrate` }
output = Dir.chdir(app_path) { `rake db:migrate:status` }

assert_match(/up\s+\d{3,}\s+Create users/, output)
assert_match(/up\s+\d{3,}\s+Add email to users/, output)

Dir.chdir(app_path) { `rake db:rollback STEP=2` }
output = Dir.chdir(app_path) { `rake db:migrate:status` }

assert_match(/down\s+\d{3,}\s+Create users/, output)
assert_match(/down\s+\d{3,}\s+Add email to users/, output)

Dir.chdir(app_path) { `rake db:migrate:redo` }
output = Dir.chdir(app_path) { `rake db:migrate:status` }

assert_match(/up\s+\d{3,}\s+Create users/, output)
assert_match(/up\s+\d{3,}\s+Add email to users/, output)
end
end
end
end

0 comments on commit 663c9f3

Please sign in to comment.