Full pipeline: IMAP ingress -> OpenClaw dispatch -> callback -> SMTP reply. SQLite stateful storage with idempotency, threading, and retry logic. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
36 lines
866 B
Go
36 lines
866 B
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
// Open initializes the SQLite database and runs auto-migration.
|
|
func Open(dbPath string) (*gorm.DB, error) {
|
|
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{
|
|
Logger: logger.Default.LogMode(logger.Silent), // We use our own structured logger.
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("database open: %w", err)
|
|
}
|
|
|
|
// Enable WAL mode for better concurrent read performance.
|
|
if err := db.Exec("PRAGMA journal_mode=WAL").Error; err != nil {
|
|
return nil, fmt.Errorf("database pragma WAL: %w", err)
|
|
}
|
|
|
|
if err := db.AutoMigrate(&Task{}); err != nil {
|
|
return nil, fmt.Errorf("database migrate: %w", err)
|
|
}
|
|
|
|
return db, nil
|
|
}
|
|
|
|
// Healthy checks if the database is writable.
|
|
func Healthy(db *gorm.DB) error {
|
|
return db.Exec("SELECT 1").Error
|
|
}
|