Class: StrongMigrations::Checker

Inherits:
Object
  • Object
show all
Includes:
Checks, SafeMethods
Defined in:
lib/strong_migrations/checker.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SafeMethods

#disable_transaction, #in_transaction?, #safe_add_check_constraint, #safe_add_foreign_key, #safe_add_index, #safe_add_reference, #safe_by_default_method?, #safe_change_column_null, #safe_remove_index

Constructor Details

#initialize(migration) ⇒ Checker

Returns a new instance of Checker.



12
13
14
15
# File 'lib/strong_migrations/checker.rb', line 12

def initialize(migration)
  @migration = migration
  reset
end

Class Attribute Details

.safeObject

Returns the value of attribute safe.



9
10
11
# File 'lib/strong_migrations/checker.rb', line 9

def safe
  @safe
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



6
7
8
# File 'lib/strong_migrations/checker.rb', line 6

def direction
  @direction
end

#timeouts_setObject

Returns the value of attribute timeouts_set.



6
7
8
# File 'lib/strong_migrations/checker.rb', line 6

def timeouts_set
  @timeouts_set
end

#transaction_disabledObject

Returns the value of attribute transaction_disabled.



6
7
8
# File 'lib/strong_migrations/checker.rb', line 6

def transaction_disabled
  @transaction_disabled
end

Class Method Details

.safety_assuredObject



26
27
28
29
30
31
32
33
34
# File 'lib/strong_migrations/checker.rb', line 26

def self.safety_assured
  previous_value = safe
  begin
    self.safe = true
    yield
  ensure
    self.safe = previous_value
  end
end

Instance Method Details

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/strong_migrations/checker.rb', line 36

def perform(method, *args, &block)
  return yield if skip?

  check_adapter
  check_version_supported
  set_timeouts
  check_lock_timeout

  if !safe? || safe_by_default_method?(method)
    # TODO better pattern
    # see checks.rb for methods
    case method
    when :add_check_constraint
      check_add_check_constraint(*args)
    when :add_column
      check_add_column(*args)
    when :add_exclusion_constraint
      check_add_exclusion_constraint(*args)
    when :add_foreign_key
      check_add_foreign_key(*args)
    when :add_index
      check_add_index(*args)
    when :add_reference, :add_belongs_to
      check_add_reference(method, *args)
    when :add_unique_constraint
      check_add_unique_constraint(*args)
    when :change_column
      check_change_column(*args)
    when :change_column_default
      check_change_column_default(*args)
    when :change_column_null
      check_change_column_null(*args)
    when :change_table
      check_change_table
    when :create_join_table
      check_create_join_table(*args)
    when :create_table
      check_create_table(*args)
    when :execute
      check_execute
    when :remove_column, :remove_columns, :remove_timestamps, :remove_reference, :remove_belongs_to
      check_remove_column(method, *args)
    when :remove_index
      check_remove_index(*args)
    when :rename_column
      check_rename_column
    when :rename_table
      check_rename_table
    when :validate_check_constraint
      check_validate_check_constraint
    when :validate_foreign_key
      check_validate_foreign_key
    when :commit_db_transaction
      # if committed, likely no longer in DDL transaction
      # and no longer eligible to be retried at migration level
      # okay to have false positives
      @committed = true
    end

    if !safe?
      # custom checks
      StrongMigrations.checks.each do |check|
        @migration.instance_exec(method, args, &check)
      end
    end
  end

  result =
    if retry_lock_timeouts?(method)
      # TODO figure out how to handle methods that generate multiple statements
      # like add_reference(table, ref, index: {algorithm: :concurrently})
      # lock timeout after first statement will cause retry to fail
      retry_lock_timeouts { perform_method(method, *args, &block) }
    else
      perform_method(method, *args, &block)
    end

  # outdated statistics + a new index can hurt performance of existing queries
  if StrongMigrations.auto_analyze && direction == :up && method == :add_index
    adapter.analyze_table(args[0])
  end

  result
end

#perform_method(method, *args) ⇒ Object



121
122
123
124
125
126
# File 'lib/strong_migrations/checker.rb', line 121

def perform_method(method, *args)
  if StrongMigrations.remove_invalid_indexes && direction == :up && method == :add_index && postgresql?
    remove_invalid_index_if_needed(*args)
  end
  yield
end

#resetObject



17
18
19
20
21
22
23
24
# File 'lib/strong_migrations/checker.rb', line 17

def reset
  @new_tables = []
  @new_columns = []
  @timeouts_set = false
  @committed = false
  @transaction_disabled = false
  @skip_retries = false
end

#retry_lock_timeouts(check_committed: false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/strong_migrations/checker.rb', line 128

def retry_lock_timeouts(check_committed: false)
  retries = 0
  begin
    yield
  rescue ActiveRecord::LockWaitTimeout => e
    if retries < StrongMigrations.lock_timeout_retries && !(check_committed && @committed)
      retries += 1
      delay = StrongMigrations.lock_timeout_retry_delay
      @migration.say("Lock timeout. Retrying in #{delay} seconds...")
      sleep(delay)
      retry
    end
    raise e
  end
end

#skip?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/strong_migrations/checker.rb', line 148

def skip?
  StrongMigrations.skipped_databases.map(&:to_s).include?(db_config_name)
end

#version_safe?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/strong_migrations/checker.rb', line 144

def version_safe?
  version && version <= StrongMigrations.start_after
end