+22
Rakefile
+22
Rakefile
···
155
155
end
156
156
end
157
157
end
158
+
159
+
desc "Delete posts older than N days that aren't included in a feed"
160
+
task :cleanup_posts do
161
+
days = ENV['DAYS'].to_i
162
+
if days <= 0
163
+
puts "Please specify number of days as e.g. DAYS=30 to delete posts older than that"
164
+
exit 1
165
+
end
166
+
167
+
result = ActiveRecord::Base.connection.execute("SELECT DATETIME('now', '-#{days} days') AS time_limit")
168
+
time_limit = result.first['time_limit']
169
+
170
+
subquery = %{
171
+
SELECT posts.id FROM posts
172
+
LEFT JOIN feed_posts ON (feed_posts.post_id = posts.id)
173
+
WHERE feed_posts.id IS NULL AND posts.time < DATETIME('now', '-#{days} days')
174
+
}
175
+
176
+
result = Post.where("id IN (#{subquery})").delete_all
177
+
178
+
puts "Deleted #{result} posts older than #{time_limit}"
179
+
end