module OnlineMigrations::Migration

Public Instance Methods

method_missing(method, *args, &block) click to toggle source

@private

Calls superclass method
# File lib/online_migrations/migration.rb, line 18
def method_missing(method, *args, &block)
  if ar_schema?
    super
  elsif command_checker.check(method, *args, &block)
    if in_transaction?
      super
    elsif method == :with_lock_retries
      connection.with_lock_retries(*args, &block)
    else
      connection.with_lock_retries { super }
    end
  end
end
migrate(direction) click to toggle source

@private

Calls superclass method
# File lib/online_migrations/migration.rb, line 6
def migrate(direction)
  VerboseSqlLogs.enable if verbose_sql_logs?

  OnlineMigrations.current_migration = self
  command_checker.direction = direction

  super
ensure
  VerboseSqlLogs.disable if verbose_sql_logs?
end
safety_assured(&block) click to toggle source

Mark a command in the migration as safe, despite using a method that might otherwise be dangerous.

@example

safety_assured { remove_column(:users, :some_column) }
# File lib/online_migrations/migration.rb, line 38
def safety_assured(&block)
  command_checker.safety_assured(&block)
end
stop!(message, header: "Custom check") click to toggle source

Stop running migrations.

It is intended for use in custom checks.

@example

OnlineMigrations.config.add_check do |method, args|
  if method == :add_column && args[0].to_s == "users"
    stop!("No more columns on the users table")
  end
end
# File lib/online_migrations/migration.rb, line 53
def stop!(message, header: "Custom check")
  raise OnlineMigrations::UnsafeMigration, "⚠️  [online_migrations] #{header} ⚠️\n\n#{message}\n\n"
end

Private Instance Methods

ar_schema?() click to toggle source
# File lib/online_migrations/migration.rb, line 66
def ar_schema?
  is_a?(ActiveRecord::Schema) ||
    (defined?(ActiveRecord::Schema::Definition) && is_a?(ActiveRecord::Schema::Definition))
end
command_checker() click to toggle source
# File lib/online_migrations/migration.rb, line 71
def command_checker
  @command_checker ||= CommandChecker.new(self)
end
in_transaction?() click to toggle source
# File lib/online_migrations/migration.rb, line 75
def in_transaction?
  connection.open_transactions > 0
end
verbose_sql_logs?() click to toggle source
# File lib/online_migrations/migration.rb, line 58
def verbose_sql_logs?
  if (verbose = ENV["ONLINE_MIGRATIONS_VERBOSE_SQL_LOGS"])
    Utils.to_bool(verbose)
  else
    OnlineMigrations.config.verbose_sql_logs
  end
end