diff --git a/.gitignore b/.gitignore index 3332f6c0..c556d858 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ rdoc *.gem .sass-cache +*.sqlite diff --git a/lib/mail_catcher.rb b/lib/mail_catcher.rb index b203bbc2..90365dae 100644 --- a/lib/mail_catcher.rb +++ b/lib/mail_catcher.rb @@ -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 @@ -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 diff --git a/lib/mail_catcher/mail.rb b/lib/mail_catcher/mail.rb index ee4ccd31..1feaacbb 100644 --- a/lib/mail_catcher/mail.rb +++ b/lib/mail_catcher/mail.rb @@ -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, @@ -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,