mirror of
https://github.com/moparisthebest/mailcatcher
synced 2024-12-21 23:08:54 -05:00
Add --keep-num-emails to only keep that many emails and delete oldest
This commit is contained in:
parent
93dd5d1758
commit
0aa927ab50
@ -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"
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user