Class: ForeignKeyChecker::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/foreign_key_checker.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  excluded_modules: [],
  specification_names: ['primary'],
  foreign_keys: true,
  indexes: true,
  zombies: true,
  polymorphic_zombies: true,
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Checker



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/foreign_key_checker.rb', line 127

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  @options.each do |key, value|
    if DEFAULT_OPTIONS.has_key?(key)
      instance_variable_set("@#{key}", value)
    end
  end
  @result = {
    zombies: [],
    foreign_keys: [],
    indexes: [],
    broken: [],
  }
end

Instance Method Details

#already_done_fk?(model, association) ⇒ Boolean



213
214
215
216
217
218
# File 'lib/foreign_key_checker.rb', line 213

def already_done_fk?(model, association)
  @_done ||= {}
  ret = @_done[[model.table_name, association.foreign_key]]
  @_done[[model.table_name, association.foreign_key]] = true
  ret
end

#checkObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/foreign_key_checker.rb', line 220

def check
  Rails.application.eager_load!
  ActiveRecord::Base.descendants.each do |model|
    next if excluded_model?(model)
    next if excluded_specification?(model)

    model.reflect_on_all_associations(:belongs_to).each do |association|

      if association.options[:polymorphic] && polymorphic_zombies
        check_polymorphic_bt_association(model, association)
        next
      end

      next if already_done_fk?(model, association)
      check_foreign_key_bt_association(model, association)
    end
  end
  @result
end

#check_foreign_key_bt_association(model, association) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/foreign_key_checker.rb', line 167

def check_foreign_key_bt_association(model, association)
  return if model.name.starts_with?('HABTM_')
  related = association.klass

  column_name = model.connection.quote_column_name(association.foreign_key)
  scope = model.left_outer_joins(association.name).where(
    "#{related.quoted_table_name}.#{related.quoted_primary_key} IS NULL AND #{model.quoted_table_name}.#{column_name} IS NOT NULL"
  )

  check_types(model, association)

  if zombies
    number = scope.count
    if number > 0
      @result[:zombies] << ZombieResult.new(
        model: model,
        association: association,
        scope: scope,
        zombies: number,
      )
    end
  end

  if foreign_keys && !model.connection.foreign_key_exists?(model.table_name, related.table_name)
    scope.first
    @result[:foreign_keys] << ForeignKeyResult.new(
      model: model,
      association: association,
      scope: scope,
    )
  end

  if indexes && !model.connection.index_exists?(model.table_name, association.foreign_key)
    @result[:indexes] << IndexResult.new(
      model: model,
      association: association,
    )
  end
rescue ActiveRecord::InverseOfAssociationNotFoundError, ActiveRecord::StatementInvalid, TypeMismatch => error
  @result[:broken] << BrokenRelationResult.new(
    model: model,
    association: association,
    error: error,
  )
end

#check_polymorphic_bt_association(model, association) ⇒ Object



157
158
159
# File 'lib/foreign_key_checker.rb', line 157

def check_polymorphic_bt_association(model, association)

end

#check_types(model, association) ⇒ Object

Raises:



161
162
163
164
165
# File 'lib/foreign_key_checker.rb', line 161

def check_types(model, association)
  type_from = model.columns_hash[association.foreign_key.to_s].sql_type
  type_to = association.klass.columns_hash[association.klass.primary_key.to_s].sql_type
  raise TypeMismatch, "TypeMissMatch #{type_from} != #{type_to}" if type_from != type_to
end

#excluded_model?(model) ⇒ Boolean



146
147
148
149
150
151
# File 'lib/foreign_key_checker.rb', line 146

def excluded_model?(model)
  excluded_modules.each do |mod_name|
    return true if model.to_s.starts_with?(mod_name)
  end
  false
end

#excluded_specification?(model) ⇒ Boolean



153
154
155
# File 'lib/foreign_key_checker.rb', line 153

def excluded_specification?(model)
  !specification_names.include?(model.connection_specification_name.to_s)
end

#specification_names=(value) ⇒ Object



142
143
144
# File 'lib/foreign_key_checker.rb', line 142

def specification_names=(value)
  value.map(&:to_s)
end