diff --git a/build/database/lite/install/ainstall.sql b/build/database/lite/install/ainstall.sql new file mode 100644 index 00000000..9ec324b1 --- /dev/null +++ b/build/database/lite/install/ainstall.sql @@ -0,0 +1,9 @@ +-- ---------------------------- +-- Configuration +-- ---------------------------- + +-- 启用完整的自动空间回收 +PRAGMA auto_vacuum = FULL; +-- 日志模式设置为 WAL 并发写入 +PRAGMA journal_mode = WAL; +PRAGMA synchronous = NORMAL; diff --git a/src/framework/database/db/db.go b/src/framework/database/db/db.go index 971ef042..a7a11fd4 100644 --- a/src/framework/database/db/db.go +++ b/src/framework/database/db/db.go @@ -36,7 +36,7 @@ func loadDialect() map[string]dialectInfo { // 数据库类型对应的数据库连接 switch item["type"] { case "sqlite": - dsn := fmt.Sprint(item["database"]) + dsn := fmt.Sprintf("%s", item["database"]) dialects[key] = dialectInfo{ dialectic: sqlite.Open(dsn), logging: parse.Boolean(item["logging"]), @@ -108,6 +108,15 @@ func Connect() { sqlDB.SetMaxOpenConns(100) // SetConnMaxLifetime 设置了连接可复用的最大时间。 sqlDB.SetConnMaxLifetime(time.Hour) + // 执行初始连接配置 + if db.Name() == "sqlite" { + // -- 设置较大的自动检查点阈值 + sqlDB.Exec("PRAGMA wal_autocheckpoint = 5000;") + // -- 内存映射 I/O 最大字节数为 64MB + sqlDB.Exec("PRAGMA mmap_size = 67108864;") + // 抛出 SQLITE_BUSY 错误 默认0立即,设置等待 5 秒(5000 毫秒) + sqlDB.Exec("PRAGMA busy_timeout = 5000;") + } logger.Infof("database %s connection is successful.", key) dbMap[key] = db } diff --git a/src/framework/database/db/expand.go b/src/framework/database/db/expand.go index e36ccc40..75f3763c 100644 --- a/src/framework/database/db/expand.go +++ b/src/framework/database/db/expand.go @@ -14,12 +14,12 @@ import ( // ImportSQL 导入SQL func ImportSQL() { - sqlPath := config.Get("sqlPath").(string) - if sqlPath == "" { + sqlPath, sqlPathOk := config.Get("sqlPath").(string) + if !sqlPathOk || sqlPath == "" { return } - sqlSource := config.Get("sqlSource").(string) - if sqlSource == "" { + sqlSource, sqlSourceOk := config.Get("sqlSource").(string) + if !sqlSourceOk || sqlSource == "" { sqlSource = config.Get("database.defaultDataSourceName").(string) } @@ -61,6 +61,7 @@ func ImportSQL() { } // log.Println("process success") + Close() os.Exit(0) }