fix(sqlite): enable WAL and export consistent snapshots

This commit is contained in:
Noru Wyrms
2026-10-25 02:27:05 +00:00
parent 02a2b79c08
commit ada0550571
7 changed files with 185 additions and 5 deletions
+17
View File
@@ -6,6 +6,7 @@ package db
import (
"context"
"fmt"
"log"
"net"
"net/url"
@@ -58,6 +59,10 @@ func initSQLite() {
log.Fatalf("[SQLite] init connection failed: %v\n", err)
}
if err = enableSQLiteWAL(db); err != nil {
log.Fatalf("[SQLite] enable WAL failed: %v\n", err)
}
// Trace 注入
if err = db.Use(
newGORMTracingPlugin([]attribute.KeyValue{
@@ -71,6 +76,18 @@ func initSQLite() {
log.Printf("[SQLite] initialized (path: %s)\n", sqlitePath)
}
// enableSQLiteWAL 在文件数据库上启用 WAL;内存数据库仅支持 memory 日志模式。
func enableSQLiteWAL(database *gorm.DB) error {
var mode string
if err := database.Raw("PRAGMA journal_mode = WAL").Scan(&mode).Error; err != nil {
return err
}
if mode != "wal" && mode != "memory" {
return fmt.Errorf("unexpected SQLite journal mode: %s", mode)
}
return nil
}
// initPostgres 初始化 PostgreSQL 数据库
func initPostgres() {
var err error