|
@@ -23,6 +23,7 @@ import (
|
|
"os"
|
|
"os"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
"regexp"
|
|
"regexp"
|
|
|
|
+ "runtime"
|
|
"strings"
|
|
"strings"
|
|
"time"
|
|
"time"
|
|
"unicode/utf8"
|
|
"unicode/utf8"
|
|
@@ -65,7 +66,6 @@ func InitDatabase(forceRebuild bool) (err error) {
|
|
|
|
|
|
if forceRebuild {
|
|
if forceRebuild {
|
|
ClearQueue()
|
|
ClearQueue()
|
|
- WaitForWritingDatabase()
|
|
|
|
}
|
|
}
|
|
|
|
|
|
initDBConnection()
|
|
initDBConnection()
|
|
@@ -80,12 +80,12 @@ func InitDatabase(forceRebuild bool) (err error) {
|
|
|
|
|
|
// 不存在库或者版本不一致都会走到这里
|
|
// 不存在库或者版本不一致都会走到这里
|
|
|
|
|
|
- db.Close()
|
|
|
|
|
|
+ closeDatabase()
|
|
if gulu.File.IsExist(util.DBPath) {
|
|
if gulu.File.IsExist(util.DBPath) {
|
|
if err = removeDatabaseFile(); nil != err {
|
|
if err = removeDatabaseFile(); nil != err {
|
|
logging.LogErrorf("remove database file [%s] failed: %s", util.DBPath, err)
|
|
logging.LogErrorf("remove database file [%s] failed: %s", util.DBPath, err)
|
|
util.PushClearProgress()
|
|
util.PushClearProgress()
|
|
- return
|
|
|
|
|
|
+ err = nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if gulu.File.IsExist(util.BlockTreePath) {
|
|
if gulu.File.IsExist(util.BlockTreePath) {
|
|
@@ -209,7 +209,7 @@ func initHistoryDBTables() {
|
|
|
|
|
|
func initDBConnection() {
|
|
func initDBConnection() {
|
|
if nil != db {
|
|
if nil != db {
|
|
- db.Close()
|
|
|
|
|
|
+ closeDatabase()
|
|
}
|
|
}
|
|
dsn := util.DBPath + "?_journal_mode=WAL" +
|
|
dsn := util.DBPath + "?_journal_mode=WAL" +
|
|
"&_synchronous=OFF" +
|
|
"&_synchronous=OFF" +
|
|
@@ -1023,7 +1023,7 @@ func batchUpdateHPath(tx *sql.Tx, boxID, rootID, oldHPath, newHPath string) (err
|
|
}
|
|
}
|
|
|
|
|
|
func CloseDatabase() {
|
|
func CloseDatabase() {
|
|
- if err := db.Close(); nil != err {
|
|
|
|
|
|
+ if err := closeDatabase(); nil != err {
|
|
logging.LogErrorf("close database failed: %s", err)
|
|
logging.LogErrorf("close database failed: %s", err)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -1111,7 +1111,7 @@ func execStmtTx(tx *sql.Tx, stmt string, args ...interface{}) (err error) {
|
|
if _, err = tx.Exec(stmt, args...); nil != err {
|
|
if _, err = tx.Exec(stmt, args...); nil != err {
|
|
if strings.Contains(err.Error(), "database disk image is malformed") {
|
|
if strings.Contains(err.Error(), "database disk image is malformed") {
|
|
tx.Rollback()
|
|
tx.Rollback()
|
|
- db.Close()
|
|
|
|
|
|
+ closeDatabase()
|
|
removeDatabaseFile()
|
|
removeDatabaseFile()
|
|
logging.LogFatalf("database disk image [%s] is malformed, please restart SiYuan kernel to rebuild it", util.DBPath)
|
|
logging.LogFatalf("database disk image [%s] is malformed, please restart SiYuan kernel to rebuild it", util.DBPath)
|
|
}
|
|
}
|
|
@@ -1180,3 +1180,13 @@ func removeDatabaseFile() (err error) {
|
|
}
|
|
}
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func closeDatabase() (err error) {
|
|
|
|
+ if nil == db {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ err = db.Close()
|
|
|
|
+ runtime.GC() // 没有这句的话文件句柄不会释放,后面就无法删除文件
|
|
|
|
+ return
|
|
|
|
+}
|