Add --keep-num-emails to only keep that many emails and delete oldest

This commit is contained in:
Travis Burtrum 2016-08-10 16:35:05 -04:00
parent 93dd5d1758
commit 0aa927ab50
2 changed files with 15 additions and 0 deletions

View File

@ -82,6 +82,7 @@ module MailCatcher extend self
:quit => true,
:sqlite_db => ':memory:',
:delete_older_than => nil,
:keep_num_emails => nil,
}
def options
@ -130,6 +131,10 @@ module MailCatcher extend self
options[:delete_older_than] = db
end
parser.on("--keep-num-emails MAX_EMAILS", "Only keep this many emails, deletes oldest") do |db|
options[:keep_num_emails] = db
end
if mac?
parser.on("--[no-]growl") do |growl|
puts "Growl is no longer supported"

View File

@ -60,6 +60,9 @@ module MailCatcher::Mail extend self
if MailCatcher.options[:delete_older_than]
MailCatcher::Mail.delete_messages_older_than!(MailCatcher.options[:delete_older_than])
end
if MailCatcher.options[:keep_num_emails]
MailCatcher::Mail.delete_messages_keep!(MailCatcher.options[:keep_num_emails])
end
end
end
@ -169,4 +172,11 @@ module MailCatcher::Mail extend self
@delete_messages_older_than_query.execute(modifier) and
@delete_message_parts_older_than_query.execute(modifier)
end
def delete_messages_keep!(keep_num_emails)
@delete_messages_query ||= db.prepare "DELETE FROM message WHERE id IN (SELECT id FROM message ORDER BY id DESC LIMIT -1 OFFSET ?);"
@delete_message_parts_query ||= db.prepare "DELETE FROM message_part WHERE id NOT IN (SELECT id FROM message)"
@delete_messages_query.execute(keep_num_emails) and
@delete_message_parts_query.execute()
end
end