Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Option to persist mails in sqlite database at path #109

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ rdoc
*.gem

.sass-cache
*.sqlite
6 changes: 6 additions & 0 deletions lib/mail_catcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ def parse! arguments=ARGV, defaults=@defaults
end
end

parser.on('-p', '--persist [PATH]', 'Persist messages in database at path (messages.sqlite by default)') do |path|
options[:persist] = path || 'messages.sqlite'
end

parser.on('-v', '--verbose', 'Be more verbose') do
options[:verbose] = true
end
Expand All @@ -130,6 +134,8 @@ def run! options=nil

Thin::Logging.silent = true

MailCatcher::Mail.db_path = options[:persist]

# One EventMachine loop...
EventMachine.run do
# Get our lion on if asked
Expand Down
8 changes: 5 additions & 3 deletions lib/mail_catcher/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
require 'eventmachine'

module MailCatcher::Mail extend self
attr_accessor :db_path

def db
@__db ||= begin
SQLite3::Database.new(':memory:', :type_translation => true).tap do |db|
SQLite3::Database.new(db_path || ':memory:', :type_translation => true).tap do |db|
db.execute(<<-SQL)
CREATE TABLE message (
CREATE TABLE IF NOT EXISTS message (
id INTEGER PRIMARY KEY ASC,
sender TEXT,
recipients TEXT,
Expand All @@ -20,7 +22,7 @@ def db
)
SQL
db.execute(<<-SQL)
CREATE TABLE message_part (
CREATE TABLE IF NOT EXISTS message_part (
id INTEGER PRIMARY KEY ASC,
message_id INTEGER NOT NULL,
cid TEXT,
Expand Down