package db import ( "database/sql" "fmt" "git.pengzhan.dev/haopengzhan/hireme/internal/logger" _ "github.com/mattn/go-sqlite3" ) type DB struct { *sql.DB } func InitDB(dbPath string) (*DB, error) { if dbPath == "" { dbPath = "data/db/hireme.db" } conn, err := sql.Open("sqlite3", dbPath+"?_foreign_keys=on&_busy_timeout=5000&_journal_mode=WAL&_txlock=immediate") if err != nil { return nil, fmt.Errorf("failed to open sqlite database: %w", err) } if err := conn.Ping(); err != nil { return nil, fmt.Errorf("failed to ping sqlite database: %w", err) } database := &DB{DB: conn} if err := database.migrate(); err != nil { return nil, fmt.Errorf("failed to run sqlite migrations: %w", err) } logger.Log.Infow("SQLite database initialized successfully", "component", "DB", "dbPath", dbPath) return database, nil } func (db *DB) migrate() error { schema := ` CREATE TABLE IF NOT EXISTS search_targets ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, target_url TEXT NOT NULL, platform TEXT NOT NULL, is_active BOOLEAN NOT NULL DEFAULT 1, last_crawled_at DATETIME ); CREATE TABLE IF NOT EXISTS raw_jobs ( id INTEGER PRIMARY KEY AUTOINCREMENT, canonical_id TEXT UNIQUE NOT NULL, target_id INTEGER REFERENCES search_targets(id), title TEXT NOT NULL, company TEXT NOT NULL, location TEXT, apply_url TEXT NOT NULL, raw_content TEXT NOT NULL, crawled_at DATETIME DEFAULT CURRENT_TIMESTAMP, last_seen_at DATETIME DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS job_analyses ( id INTEGER PRIMARY KEY AUTOINCREMENT, raw_job_id INTEGER UNIQUE NOT NULL REFERENCES raw_jobs(id) ON DELETE CASCADE, filter_score REAL DEFAULT 0.0, score REAL DEFAULT 0.0, is_qualified BOOLEAN NOT NULL DEFAULT 0, summary TEXT, matched_highlights JSON, missing_highlights JSON, tailoring_suggestions JSON, status TEXT NOT NULL DEFAULT 'PENDING', analyzed_at DATETIME ); CREATE TABLE IF NOT EXISTS job_applications ( id INTEGER PRIMARY KEY AUTOINCREMENT, raw_job_id INTEGER UNIQUE NOT NULL REFERENCES raw_jobs(id) ON DELETE CASCADE, status TEXT NOT NULL DEFAULT 'SAVED', notes TEXT, applied_at DATETIME, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS tasks ( id INTEGER PRIMARY KEY AUTOINCREMENT, task_type TEXT NOT NULL, payload TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'PENDING', error_message TEXT, retry_count INTEGER DEFAULT 0, next_retry_at DATETIME DEFAULT CURRENT_TIMESTAMP, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ); ` if _, err := db.Exec(schema); err != nil { return err } // Dynamic migration for existing databases: check if last_seen_at exists in raw_jobs var hasLastSeen bool rows, err := db.Query(`PRAGMA table_info(raw_jobs)`) if err == nil { for rows.Next() { var cid int var name, ctype string var notnull, pk int var dfltValue sql.NullString if err := rows.Scan(&cid, &name, &ctype, ¬null, &dfltValue, &pk); err == nil { if name == "last_seen_at" { hasLastSeen = true break } } } _ = rows.Close() } if !hasLastSeen { if _, err := db.Exec(`ALTER TABLE raw_jobs ADD COLUMN last_seen_at DATETIME`); err != nil { logger.Log.Warnw("Failed to add last_seen_at column", "component", "DB", "error", err) } else { _, _ = db.Exec(`UPDATE raw_jobs SET last_seen_at = crawled_at WHERE last_seen_at IS NULL`) _, _ = db.Exec(`UPDATE raw_jobs SET last_seen_at = CURRENT_TIMESTAMP WHERE last_seen_at IS NULL`) } } // Dynamic migration for job_analyses: check if filter_score and summary exist var hasFilterScore, hasSummary bool rowsA, err := db.Query(`PRAGMA table_info(job_analyses)`) if err == nil { for rowsA.Next() { var cid int var name, ctype string var notnull, pk int var dfltValue sql.NullString if err := rowsA.Scan(&cid, &name, &ctype, ¬null, &dfltValue, &pk); err == nil { if name == "filter_score" { hasFilterScore = true } if name == "summary" { hasSummary = true } } } _ = rowsA.Close() } if !hasFilterScore { if _, err := db.Exec(`ALTER TABLE job_analyses ADD COLUMN filter_score REAL DEFAULT 0.0`); err != nil { logger.Log.Warnw("Failed to add filter_score column", "component", "DB", "error", err) } else { _, _ = db.Exec(`UPDATE job_analyses SET filter_score = score WHERE filter_score IS NULL OR filter_score = 0.0`) } } if !hasSummary { if _, err := db.Exec(`ALTER TABLE job_analyses ADD COLUMN summary TEXT`); err != nil { logger.Log.Warnw("Failed to add summary column", "component", "DB", "error", err) } } return nil }