Mail access: IMAP + app password (IPv4 only), not Gmail API
This commit is contained in:
101
internal/store/schema.sql
Normal file
101
internal/store/schema.sql
Normal file
@@ -0,0 +1,101 @@
|
||||
-- Email Agent schema (AGENTS.md §7).
|
||||
--
|
||||
-- Every statement is idempotent (CREATE ... IF NOT EXISTS) so this file can be
|
||||
-- executed on every start. Retention/capping jobs belong to later phases.
|
||||
--
|
||||
-- Attribution: created for the Email Agent Phase 1 skeleton.
|
||||
|
||||
PRAGMA foreign_keys = ON;
|
||||
|
||||
-- Records the applied schema version for future migrations.
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version INTEGER PRIMARY KEY,
|
||||
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
-- FR-1, FR-2: mail metadata and body, plus the forwarding address it arrived at.
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
gmail_id TEXT NOT NULL UNIQUE,
|
||||
thread_id TEXT,
|
||||
from_addr TEXT,
|
||||
to_addr TEXT,
|
||||
subject TEXT,
|
||||
body_text TEXT,
|
||||
source_address TEXT,
|
||||
date_sent TEXT,
|
||||
received_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_received_at ON messages (received_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_thread_id ON messages (thread_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_source ON messages (source_address);
|
||||
|
||||
-- FR-3, FR-14: classifier output per message.
|
||||
CREATE TABLE IF NOT EXISTS classifications (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
message_id INTEGER NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
|
||||
category TEXT NOT NULL,
|
||||
importance TEXT,
|
||||
action TEXT,
|
||||
confidence REAL,
|
||||
model TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_classifications_message ON classifications (message_id);
|
||||
|
||||
-- FR-4, FR-11: versioned rules text.
|
||||
CREATE TABLE IF NOT EXISTS rules (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
text TEXT NOT NULL,
|
||||
version INTEGER NOT NULL,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
-- FR-13: append-only action log.
|
||||
CREATE TABLE IF NOT EXISTS actions_log (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
message_id INTEGER REFERENCES messages (id) ON DELETE SET NULL,
|
||||
action TEXT NOT NULL,
|
||||
actor TEXT,
|
||||
rule_id INTEGER REFERENCES rules (id) ON DELETE SET NULL,
|
||||
detail TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_actions_log_message ON actions_log (message_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_actions_log_created ON actions_log (created_at);
|
||||
|
||||
-- FR-7: unsubscribe tracker and ignore list.
|
||||
CREATE TABLE IF NOT EXISTS unsubscribe (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
domain TEXT NOT NULL UNIQUE,
|
||||
status TEXT NOT NULL DEFAULT 'done' CHECK (status IN ('done', 'ignored')),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
-- FR-8: reply drafts.
|
||||
CREATE TABLE IF NOT EXISTS drafts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
message_id INTEGER NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
|
||||
body TEXT NOT NULL,
|
||||
template_id TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'draft',
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_drafts_message ON drafts (message_id);
|
||||
|
||||
-- FR-10: daily summary and its delivery target.
|
||||
CREATE TABLE IF NOT EXISTS summaries (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
date TEXT NOT NULL UNIQUE,
|
||||
text TEXT NOT NULL,
|
||||
delivered_to TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
INSERT OR IGNORE INTO schema_migrations (version) VALUES (1);
|
||||
117
internal/store/store.go
Normal file
117
internal/store/store.go
Normal file
@@ -0,0 +1,117 @@
|
||||
// Package store owns the SQLite database: opening it, applying the schema, and
|
||||
// exposing a small query surface to the rest of the service.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
// Pure-Go SQLite driver. Keeps the container CGO-free.
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
//go:embed schema.sql
|
||||
var schemaSQL string
|
||||
|
||||
// Store wraps the SQLite connection pool.
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
path string
|
||||
}
|
||||
|
||||
// Open creates (if needed) and connects to the SQLite database at path, then
|
||||
// applies the schema. The parent directory is created when missing.
|
||||
func Open(ctx context.Context, path string) (*Store, error) {
|
||||
if dir := filepath.Dir(path); dir != "" && dir != "." {
|
||||
if err := os.MkdirAll(dir, 0o750); err != nil {
|
||||
return nil, fmt.Errorf("create database directory %q: %w", dir, err)
|
||||
}
|
||||
}
|
||||
|
||||
dsn := buildDSN(path)
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open sqlite database: %w", err)
|
||||
}
|
||||
|
||||
// SQLite handles one writer at a time; keep the pool small and give the
|
||||
// busy handler room. Reads and writes share the connection here.
|
||||
db.SetMaxOpenConns(1)
|
||||
db.SetMaxIdleConns(1)
|
||||
db.SetConnMaxLifetime(0)
|
||||
|
||||
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
if err := db.PingContext(pingCtx); err != nil {
|
||||
_ = db.Close()
|
||||
return nil, fmt.Errorf("ping sqlite database: %w", err)
|
||||
}
|
||||
|
||||
s := &Store{db: db, path: path}
|
||||
if err := s.migrate(ctx); err != nil {
|
||||
_ = db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// buildDSN returns a modernc.org/sqlite DSN with safe pragmas applied on every
|
||||
// new connection.
|
||||
func buildDSN(path string) string {
|
||||
abs, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
abs = path
|
||||
}
|
||||
q := url.Values{}
|
||||
q.Add("_pragma", "busy_timeout(5000)")
|
||||
q.Add("_pragma", "journal_mode(WAL)")
|
||||
q.Add("_pragma", "foreign_keys(1)")
|
||||
q.Add("_pragma", "synchronous(NORMAL)")
|
||||
return "file:" + abs + "?" + q.Encode()
|
||||
}
|
||||
|
||||
// migrate applies the embedded schema. It is safe to run on every start.
|
||||
func (s *Store) migrate(ctx context.Context) error {
|
||||
if _, err := s.db.ExecContext(ctx, schemaSQL); err != nil {
|
||||
return fmt.Errorf("apply schema: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DB exposes the underlying pool for callers that need it.
|
||||
func (s *Store) DB() *sql.DB { return s.db }
|
||||
|
||||
// Path returns the database file path.
|
||||
func (s *Store) Path() string { return s.path }
|
||||
|
||||
// Ping verifies the database is reachable.
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
return s.db.PingContext(ctx)
|
||||
}
|
||||
|
||||
// Close releases the connection pool.
|
||||
func (s *Store) Close() error {
|
||||
if s == nil || s.db == nil {
|
||||
return nil
|
||||
}
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
// SchemaVersion returns the highest applied migration version.
|
||||
func (s *Store) SchemaVersion(ctx context.Context) (int, error) {
|
||||
var v sql.NullInt64
|
||||
err := s.db.QueryRowContext(ctx, `SELECT MAX(version) FROM schema_migrations`).Scan(&v)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if !v.Valid {
|
||||
return 0, nil
|
||||
}
|
||||
return int(v.Int64), nil
|
||||
}
|
||||
85
internal/store/store_test.go
Normal file
85
internal/store/store_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOpenCreatesSchema(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "nested", "email_agent.db")
|
||||
|
||||
s, err := Open(ctx, path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open() error = %v", err)
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
v, err := s.SchemaVersion(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("SchemaVersion() error = %v", err)
|
||||
}
|
||||
if v < 1 {
|
||||
t.Fatalf("SchemaVersion() = %d, want >= 1", v)
|
||||
}
|
||||
|
||||
wantTables := []string{
|
||||
"messages", "classifications", "rules", "actions_log",
|
||||
"unsubscribe", "drafts", "summaries",
|
||||
}
|
||||
for _, table := range wantTables {
|
||||
var name string
|
||||
err := s.DB().QueryRowContext(ctx,
|
||||
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`, table).Scan(&name)
|
||||
if err != nil {
|
||||
t.Errorf("table %q missing: %v", table, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenIsIdempotent(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "email_agent.db")
|
||||
|
||||
s1, err := Open(ctx, path)
|
||||
if err != nil {
|
||||
t.Fatalf("first Open() error = %v", err)
|
||||
}
|
||||
_ = s1.Close()
|
||||
|
||||
s2, err := Open(ctx, path)
|
||||
if err != nil {
|
||||
t.Fatalf("second Open() error = %v", err)
|
||||
}
|
||||
defer s2.Close()
|
||||
|
||||
if err := s2.Ping(ctx); err != nil {
|
||||
t.Fatalf("Ping() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessagesInsertAndRead(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := Open(ctx, filepath.Join(t.TempDir(), "email_agent.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("Open() error = %v", err)
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
_, err = s.DB().ExecContext(ctx,
|
||||
`INSERT INTO messages (gmail_id, subject, source_address) VALUES (?, ?, ?)`,
|
||||
"abc123", "Hello", "hello@where-woof.com")
|
||||
if err != nil {
|
||||
t.Fatalf("insert message: %v", err)
|
||||
}
|
||||
|
||||
var subject string
|
||||
if err := s.DB().QueryRowContext(ctx,
|
||||
`SELECT subject FROM messages WHERE gmail_id = ?`, "abc123").Scan(&subject); err != nil {
|
||||
t.Fatalf("select message: %v", err)
|
||||
}
|
||||
if subject != "Hello" {
|
||||
t.Errorf("subject = %q, want %q", subject, "Hello")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user