@@ -116,10 +116,10 @@ def purge_by_scope(older_than = nil, window = nil, &block)
116
116
total
117
117
end
118
118
119
- def purge_by_orphaned ( fk_name , window = purge_window_size )
119
+ def purge_by_orphaned ( fk_name , window = purge_window_size , purge_mode = :purge )
120
120
_log . info ( "Purging orphans in #{ table_name } ..." )
121
- total = purge_orphans ( fk_name , window )
122
- _log . info ( "Purging orphans in #{ table_name } ...Complete - Deleted #{ total } records" )
121
+ total = purge_orphans ( fk_name , window , purge_mode )
122
+ _log . info ( "Purging orphans in #{ table_name } ...Complete - #{ purge_mode . to_sym == :count ? 'Would delete' : ' Deleted' } #{ total } records" )
123
123
total
124
124
end
125
125
@@ -133,40 +133,38 @@ def purge_by_date_and_orphaned(older_than, fk_name, window = purge_window_size)
133
133
134
134
private
135
135
136
- def purge_orphans ( fk_name , window )
136
+ def purge_orphans ( fk_name , window , purge_mode = :purge )
137
137
reflection = reflect_on_association ( fk_name )
138
- if reflection . polymorphic?
139
- purge_polymorphic_orphans ( fk_name , window )
138
+ scopes = reflection . polymorphic? ? polymorphic_orphan_scopes ( fk_name ) : non_polymorphic_orphan_scopes ( fk_name , reflection . klass )
139
+
140
+ if purge_mode . to_sym == :count
141
+ scopes . sum ( &:count )
140
142
else
141
- purge_non_polymorphic_orphans ( fk_name , window , reflection . klass )
143
+ scopes . sum { | s | purge_in_batches ( s , window ) }
142
144
end
143
145
end
144
146
145
- def purge_polymorphic_orphans ( fk_name , window )
147
+ def polymorphic_orphan_scopes ( fk_name )
146
148
polymorphic_type_column = "#{ fk_name } _type"
147
149
polymorphic_id_column = connection . quote_column_name ( "#{ fk_name } _id" )
148
- total = 0
149
150
150
- polymorphic_classes ( polymorphic_type_column ) . each do |klass |
151
+ polymorphic_classes ( polymorphic_type_column ) . collect do |klass |
151
152
resource_table = connection . quote_table_name ( klass . table_name )
152
153
q_table_name = connection . quote_table_name ( table_name )
153
154
154
- scope = joins ( "LEFT OUTER JOIN #{ resource_table } ON #{ q_table_name } .#{ polymorphic_id_column } = #{ resource_table } .id" )
155
- . where ( resource_table => { :id => nil } )
156
- . where ( "#{ q_table_name } .#{ connection . quote_column_name ( polymorphic_type_column ) } = #{ connection . quote ( klass . name ) } " )
157
- total += purge_in_batches ( scope , window )
155
+ joins ( "LEFT OUTER JOIN #{ resource_table } ON #{ q_table_name } .#{ polymorphic_id_column } = #{ resource_table } .id" )
156
+ . where ( resource_table => { :id => nil } )
157
+ . where ( "#{ q_table_name } .#{ connection . quote_column_name ( polymorphic_type_column ) } = #{ connection . quote ( klass . name ) } " )
158
158
end
159
- total
160
159
end
161
160
162
- def purge_non_polymorphic_orphans ( fk_name , window , reflection_klass )
161
+ def non_polymorphic_orphan_scopes ( fk_name , reflection_klass )
163
162
resource_table = connection . quote_table_name ( reflection_klass . table_name )
164
163
assoc_id = connection . quote_column_name ( "#{ fk_name } _id" )
165
164
q_table_name = connection . quote_table_name ( table_name )
166
165
167
- scope = joins ( "LEFT OUTER JOIN #{ resource_table } ON #{ q_table_name } .#{ assoc_id } = #{ resource_table } .id" )
168
- . where ( resource_table => { :id => nil } )
169
- purge_in_batches ( scope , window )
166
+ [ joins ( "LEFT OUTER JOIN #{ resource_table } ON #{ q_table_name } .#{ assoc_id } = #{ resource_table } .id" )
167
+ . where ( resource_table => { :id => nil } ) ]
170
168
end
171
169
172
170
def polymorphic_classes ( polymorphic_type_column )
0 commit comments