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

Move db:migrate:status to DatabaseTasks method

This commit is contained in:
Gannon McGibbon 2018-10-04 16:34:43 -04:00
parent 3882a4d0d4
commit 0d435c17b7
3 changed files with 39 additions and 13 deletions

View file

@ -149,18 +149,7 @@ db_namespace = namespace :db do
desc "Display status of migrations"
task status: :load_config do
unless ActiveRecord::SchemaMigration.table_exists?
abort "Schema migrations table does not exist yet."
end
# output
puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
puts "-" * 50
ActiveRecord::Base.connection.migration_context.migrations_status.each do |status, version, name|
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
end
puts
ActiveRecord::Tasks::DatabaseTasks.migrate_status
end
end

View file

@ -197,6 +197,21 @@ module ActiveRecord
Migration.verbose = verbose_was
end
def migrate_status
unless ActiveRecord::SchemaMigration.table_exists?
Kernel.abort "Schema migrations table does not exist yet."
end
# output
puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
puts "-" * 50
ActiveRecord::Base.connection.migration_context.migrations_status.each do |status, version, name|
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
end
puts
end
def check_target_version
if target_version && !(Migration::MigrationFilenameRegexp.match?(ENV["VERSION"]) || /\A\d+\z/.match?(ENV["VERSION"]))
raise "Invalid format of target version: `VERSION=#{ENV['VERSION']}`"

View file

@ -731,7 +731,7 @@ module ActiveRecord
end
if current_adapter?(:SQLite3Adapter) && !in_memory_db?
class DatabaseTasksMigrateTest < ActiveRecord::TestCase
class DatabaseTasksMigrationTestCase < ActiveRecord::TestCase
self.use_transactional_tests = false
# Use a memory db here to avoid having to rollback at the end
@ -751,7 +751,9 @@ module ActiveRecord
@conn.release_connection if @conn
ActiveRecord::Base.establish_connection :arunit
end
end
class DatabaseTasksMigrateTest < DatabaseTasksMigrationTestCase
def test_migrate_set_and_unset_verbose_and_version_env_vars
verbose, version = ENV["VERBOSE"], ENV["VERSION"]
ENV["VERSION"] = "2"
@ -812,6 +814,26 @@ module ActiveRecord
end
end
end
class DatabaseTasksMigrateStatusTest < DatabaseTasksMigrationTestCase
def test_migrate_status_table
ActiveRecord::SchemaMigration.create_table
output = capture_migration_status
assert_match(/database: :memory:/, output)
assert_match(/down 001 Valid people have last names/, output)
assert_match(/down 002 We need reminders/, output)
assert_match(/down 003 Innocent jointable/, output)
ActiveRecord::SchemaMigration.drop_table
end
private
def capture_migration_status
capture(:stdout) do
ActiveRecord::Tasks::DatabaseTasks.migrate_status
end
end
end
end
class DatabaseTasksMigrateErrorTest < ActiveRecord::TestCase