Class: ForeignKeyChecker::Checker
- Inherits:
-
Object
- Object
- ForeignKeyChecker::Checker
- 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
- #already_done_fk?(model, association) ⇒ Boolean
- #check ⇒ Object
- #check_foreign_key_bt_association(model, association) ⇒ Object
- #check_polymorphic_bt_association(model, association) ⇒ Object
- #check_types(model, association) ⇒ Object
- #excluded_model?(model) ⇒ Boolean
- #excluded_specification?(model) ⇒ Boolean
-
#initialize(options = {}) ⇒ Checker
constructor
A new instance of Checker.
- #specification_names=(value) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Checker
Returns a new instance of Checker.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/foreign_key_checker.rb', line 128 def initialize( = {}) = DEFAULT_OPTIONS.merge() .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
214 215 216 217 218 219 |
# File 'lib/foreign_key_checker.rb', line 214 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 |
#check ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/foreign_key_checker.rb', line 221 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.[: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
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 212 |
# File 'lib/foreign_key_checker.rb', line 168 def check_foreign_key_bt_association(model, association) return if model.name.starts_with?('HABTM_') = 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, .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
158 159 160 |
# File 'lib/foreign_key_checker.rb', line 158 def check_polymorphic_bt_association(model, association) end |
#check_types(model, association) ⇒ Object
162 163 164 165 166 |
# File 'lib/foreign_key_checker.rb', line 162 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
147 148 149 150 151 152 |
# File 'lib/foreign_key_checker.rb', line 147 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
154 155 156 |
# File 'lib/foreign_key_checker.rb', line 154 def excluded_specification?(model) !specification_names.include?(model.connection_specification_name.to_s) end |
#specification_names=(value) ⇒ Object
143 144 145 |
# File 'lib/foreign_key_checker.rb', line 143 def specification_names=(value) value.map(&:to_s) end |