New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to run migrations in check mode (dry run) #31630

Closed
wants to merge 1 commit into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+70 −23
Diff settings

Always

Just for now

@@ -983,7 +983,9 @@ def migration
def load_migration
require(File.expand_path(filename))
name.constantize.new(name, version)
migration = name.constantize.new(name, version)
migration.extend(DryRunMigration) if Migrator.dry_run
migration
end
end
@@ -997,7 +999,37 @@ def mtime
end
end
module DryRunMigration
def migrate(direction)
return unless respond_to?(direction)
case direction
when :up then announce "migrating"
when :down then announce "reverting"
end
ActiveRecord::Base.connection_pool.with_connection do |conn|
exec_migration(conn, direction)
end
case direction
when :up then announce "migrated"; write
when :down then announce "reverted"; write
end
end
def method_missing(method, *arguments, &block)
arg_list = arguments.map(&:inspect) * ", "
say "#{method}(#{arg_list})"
return super unless connection.respond_to?(method)
connection.send(method, *arguments, &block) if connection.is_a?(Migration::CommandRecorder)
end
end
class Migrator#:nodoc:
cattr_accessor :dry_run, default: false
class << self
attr_writer :migrations_paths
alias :migrations_path= :migrations_paths=
@@ -1255,7 +1287,7 @@ def execute_migration_in_transaction(migration, direction)
ddl_transaction(migration) do
migration.migrate(direction)
record_version_state_after_migrating(migration.version)
record_version_state_after_migrating(migration.version) unless dry_run
end
rescue => e
msg = "An error has occurred, ".dup
@@ -63,7 +63,7 @@ db_namespace = namespace :db do
# IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
task :_dump do
if ActiveRecord::Base.dump_schema_after_migration
if ActiveRecord::Base.dump_schema_after_migration && !ActiveRecord::Migrator.dry_run
case ActiveRecord::Base.schema_format
when :ruby then db_namespace["schema:dump"].invoke
when :sql then db_namespace["structure:dump"].invoke
@@ -95,29 +95,13 @@ db_namespace = namespace :db do
# desc 'Runs the "up" for a given migration VERSION.'
task up: :load_config do
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
ActiveRecord::Tasks::DatabaseTasks.check_target_version
ActiveRecord::Migrator.run(
:up,
ActiveRecord::Tasks::DatabaseTasks.migrations_paths,
ActiveRecord::Tasks::DatabaseTasks.target_version
)
ActiveRecord::Tasks::DatabaseTasks.run(:up)
db_namespace["_dump"].invoke
end
# desc 'Runs the "down" for a given migration VERSION.'
task down: :load_config do
raise "VERSION is required - To go down one migration, use db:rollback" if !ENV["VERSION"] || ENV["VERSION"].empty?
ActiveRecord::Tasks::DatabaseTasks.check_target_version
ActiveRecord::Migrator.run(
:down,
ActiveRecord::Tasks::DatabaseTasks.migrations_paths,
ActiveRecord::Tasks::DatabaseTasks.target_version
)
ActiveRecord::Tasks::DatabaseTasks.run(:down)
db_namespace["_dump"].invoke
end
@@ -141,8 +125,7 @@ db_namespace = namespace :db do
desc "Rolls the schema back to the previous version (specify steps w/ STEP=n)."
task rollback: :load_config do
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
ActiveRecord::Migrator.rollback(ActiveRecord::Tasks::DatabaseTasks.migrations_paths, step)
ActiveRecord::Tasks::DatabaseTasks.rollback
db_namespace["_dump"].invoke
end
@@ -169,12 +169,40 @@ def migrate
verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] != "false" : true
scope = ENV["SCOPE"]
verbose_was, Migration.verbose = Migration.verbose, verbose
dry_run_was, Migrator.dry_run = Migrator.dry_run, dry_run
Migrator.migrate(migrations_paths, target_version) do |migration|
scope.blank? || scope == migration.scope
end
ActiveRecord::Base.clear_cache!
ensure
Migration.verbose = verbose_was
Migrator.dry_run = dry_run_was
end
def rollback
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
dry_run_was, Migrator.dry_run = Migrator.dry_run, dry_run
Migrator.rollback(migrations_paths, step)
ensure
Migrator.dry_run = dry_run_was
end
def run(direction)
case direction
when :up
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
when :down
raise "VERSION is required - To go down one migration, use db:rollback" if !ENV["VERSION"] || ENV["VERSION"].empty?
end
check_target_version
dry_run_was, Migrator.dry_run = Migrator.dry_run, dry_run
Migrator.run(direction, migrations_paths, target_version)
ensure
Migrator.dry_run = dry_run_was
end
def check_target_version
@@ -187,6 +215,10 @@ def target_version
ENV["VERSION"].to_i if ENV["VERSION"] && !ENV["VERSION"].empty?
end
def dry_run
ENV["DRY_RUN"] ? ENV["DRY_RUN"] == "true" : false
end
def charset_current(environment = env)
charset ActiveRecord::Base.configurations[environment]
end
ProTip! Use n and p to navigate between commits in a pull request.