diff --git a/.gitignore b/.gitignore index 0186ae4..43d1884 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,11 @@ /.claude agent.md -config.json \ No newline at end of file +config.json + +# SQLite database files. They live on a network share (Y:), and git/cloud +# sync touching them while a WAL database is open corrupts the WAL index and +# crashes the process (EXCEPTION_IN_PAGE_ERROR). Never track these. +db/*.db +db/*.db-wal +db/*.db-shm +db/*.db-journal \ No newline at end of file diff --git a/config/config.go b/config/config.go index 2966aab..c362065 100644 --- a/config/config.go +++ b/config/config.go @@ -78,7 +78,7 @@ func LoadConfig(configPath string) (*Config, error) { cfg.DataPath = "./data" } if cfg.DBPath == "" { - cfg.DBPath = "./logs.db" + cfg.DBPath = "./db" } // Apply default message templates if not specified. diff --git a/database/user.go b/database/user.go index 663da77..2013609 100644 --- a/database/user.go +++ b/database/user.go @@ -38,6 +38,23 @@ func InitUserDB(dbPath string) error { return fmt.Errorf("failed to ping user database: %w", err) } + // Use the rollback journal (DELETE) instead of WAL. + // + // WAL memory-maps a -shm index file. modernc.org/sqlite (a C-to-Go + // transpile) is built without SEH on Windows, so an I/O page fault on that + // mapping (common when the database lives on an SMB/network share) + // terminates the process with EXCEPTION_IN_PAGE_ERROR instead of being + // caught and retried. With SetMaxOpenConns(1) above, WAL provides no + // concurrency benefit either, so the rollback journal is strictly better. + if _, err := UserDB.Exec("PRAGMA journal_mode=DELETE"); err != nil { + return fmt.Errorf("failed to set journal mode: %w", err) + } + + // Wait up to 5 seconds for a busy database instead of immediately failing with SQLITE_BUSY. + if _, err := UserDB.Exec("PRAGMA busy_timeout=5000"); err != nil { + return fmt.Errorf("failed to set busy timeout: %w", err) + } + if err := initUserTable(); err != nil { return fmt.Errorf("failed to initialize user table: %w", err) } diff --git a/db/log.db b/db/log.db deleted file mode 100644 index e69de29..0000000 diff --git a/db/user.db b/db/user.db deleted file mode 100644 index c341f40..0000000 Binary files a/db/user.db and /dev/null differ diff --git a/logs.db/log.db b/logs.db/log.db deleted file mode 100644 index e69de29..0000000 diff --git a/logs.db/user.db b/logs.db/user.db deleted file mode 100644 index c341f40..0000000 Binary files a/logs.db/user.db and /dev/null differ diff --git a/postLog/database.go b/postLog/database.go index 8b14db4..e98acc5 100644 --- a/postLog/database.go +++ b/postLog/database.go @@ -4,6 +4,7 @@ import ( "database/sql" "fmt" "os" + "path/filepath" "time" ) @@ -13,16 +14,45 @@ var ( ) func InitLogsDatabase(dbPath string) error { + dir := filepath.Dir(dbPath) + if dir != "." { + if err := os.MkdirAll(dir, 0755); err != nil { + return fmt.Errorf("failed to create database directory: %w", err) + } + } + var err error logsDB, err = sql.Open("sqlite", dbPath) if err != nil { return fmt.Errorf("failed to open logs database: %w", err) } + // SQLite serializes writes — limit to one connection to avoid SQLITE_BUSY. + logsDB.SetMaxOpenConns(1) + logsDB.SetMaxIdleConns(1) + logsDB.SetConnMaxLifetime(5 * time.Minute) + if err = logsDB.Ping(); err != nil { return fmt.Errorf("failed to ping logs database: %w", err) } + // Use the rollback journal (DELETE) instead of WAL. + // + // WAL memory-maps a -shm index file. modernc.org/sqlite (a C-to-Go + // transpile) is built without SEH on Windows, so an I/O page fault on that + // mapping (common when the database lives on an SMB/network share) + // terminates the process with EXCEPTION_IN_PAGE_ERROR instead of being + // caught and retried. With SetMaxOpenConns(1) above, WAL provides no + // concurrency benefit either, so the rollback journal is strictly better. + if _, err = logsDB.Exec("PRAGMA journal_mode=DELETE"); err != nil { + return fmt.Errorf("failed to set journal mode: %w", err) + } + + // Wait up to 5 seconds for a busy database instead of immediately failing with SQLITE_BUSY. + if _, err = logsDB.Exec("PRAGMA busy_timeout=5000"); err != nil { + return fmt.Errorf("failed to set busy timeout: %w", err) + } + timestamp := time.Now().Format("20060102_150405") tableName = fmt.Sprintf("logs_%s", timestamp) @@ -44,9 +74,8 @@ func InitLogsDatabase(dbPath string) error { func insertLogToDB(db *sql.DB, level int, content string, timestamp string) { if db != nil { - _, err := db.Exec(fmt.Sprintf(`INSERT INTO %s (level, content, timestamp) VALUES (%d, '%s', '%s')`, tableName, - level, content, timestamp)) - // fmt.Fprintf(os.Stderr, `INSERT INTO %s (level, content, timestamp) VALUES (%d, '%s', '%s')`, tableName, level, content, timestamp) + query := fmt.Sprintf(`INSERT INTO %s (level, content, timestamp) VALUES (?, ?, ?)`, tableName) + _, err := db.Exec(query, level, content, timestamp) if err != nil { fmt.Fprintf(os.Stderr, "Failed to write log to database: %v\n", err) }