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.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/foreign_key_checker.rb', line 133 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
228 229 230 231 232 233 |
# File 'lib/foreign_key_checker.rb', line 228 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
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/foreign_key_checker.rb', line 235 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
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/foreign_key_checker.rb', line 173 def check_foreign_key_bt_association(model, association) return if model.name.starts_with?('HABTM_') begin = association.klass rescue NameError => error @result[:broken] << BrokenRelationResult.new( model: model, association: association, error: error, ) return end 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, column: association.foreign_key, primary_key: .primary_key) 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
163 164 165 |
# File 'lib/foreign_key_checker.rb', line 163 def check_polymorphic_bt_association(model, association) end |
#check_types(model, association) ⇒ Object
167 168 169 170 171 |
# File 'lib/foreign_key_checker.rb', line 167 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 for relation #{model}##{association.name} #{type_from} != #{type_to}" if type_from != type_to end |
#excluded_model?(model) ⇒ Boolean
152 153 154 155 156 157 |
# File 'lib/foreign_key_checker.rb', line 152 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
159 160 161 |
# File 'lib/foreign_key_checker.rb', line 159 def excluded_specification?(model) !specification_names.include?(model.connection_specification_name.to_s) end |
#specification_names=(value) ⇒ Object
148 149 150 |
# File 'lib/foreign_key_checker.rb', line 148 def specification_names=(value) value.map(&:to_s) end |