fix(database): fix sqlite init error in smb workspace

fix(postLog/database): change journal mode from WAL to DELETE
This commit is contained in:
2026-08-05 13:31:25 +08:00
parent c25b267334
commit 814fbe0dae
8 changed files with 59 additions and 5 deletions
+9 -1
View File
@@ -1,3 +1,11 @@
/.claude
agent.md
config.json
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
+1 -1
View File
@@ -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.
+17
View File
@@ -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)
}
View File
BIN
View File
Binary file not shown.
View File
BIN
View File
Binary file not shown.
+32 -3
View File
@@ -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)
}