Module: OnlineMigrations::Migration

Defined in:
lib/online_migrations/migration.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/online_migrations/migration.rb', line 19

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

Instance Method Details

#migrate(direction) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# 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?
  OnlineMigrations.current_migration = nil
end

#revert(*args) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/online_migrations/migration.rb', line 35

def revert(*args)
  if command_checker.version_safe?
    safety_assured { super }
  else
    super
  end
end

#safety_assured(&block) ⇒ Object

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

Examples:

safety_assured { remove_column(:users, :some_column) }


48
49
50
# File 'lib/online_migrations/migration.rb', line 48

def safety_assured(&block)
  command_checker.class.safety_assured(&block)
end

#stop!(message, header: "Custom check") ⇒ Object

Stop running migrations.

It is intended for use in custom checks.

Examples:

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

Raises:



63
64
65
# File 'lib/online_migrations/migration.rb', line 63

def stop!(message, header: "Custom check")
  raise OnlineMigrations::UnsafeMigration, "⚠️  [online_migrations] #{header} ⚠️\n\n#{message}\n\n"
end