mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-03-13 07:56:14 +01:00
Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
commit
d79fae1107
3 changed files with 54 additions and 55 deletions
|
|
@ -102,12 +102,7 @@ func InitDatabase(forceRebuild bool) (err error) {
|
|||
// 不存在库或者版本不一致都会走到这里
|
||||
|
||||
closeDatabase()
|
||||
if gulu.File.IsExist(util.DBPath) {
|
||||
if err = removeDatabaseFile(); err != nil {
|
||||
logging.LogErrorf("remove database file failed: %s", err)
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
util.RemoveDatabaseFile(util.DBPath)
|
||||
|
||||
initDBConnection()
|
||||
initDBTables()
|
||||
|
|
@ -1486,13 +1481,14 @@ func prepareExecInsertTx(tx *sql.Tx, stmtSQL string, args []interface{}) (err er
|
|||
}
|
||||
|
||||
if _, err = stmt.Exec(args...); err != nil {
|
||||
if strings.Contains(err.Error(), "database disk image is malformed") {
|
||||
tx.Rollback()
|
||||
closeDatabase()
|
||||
removeDatabaseFile()
|
||||
logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "database disk image [%s] is malformed, please restart SiYuan kernel to rebuild it", util.DBPath)
|
||||
}
|
||||
tx.Rollback()
|
||||
logging.LogErrorf("exec database stmt [%s] failed: %s\n %s", stmtSQL, err, logging.ShortStack())
|
||||
|
||||
if strings.Contains(err.Error(), "database disk image is malformed") {
|
||||
closeDatabase()
|
||||
util.RemoveDatabaseFile(util.DBPath)
|
||||
logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "database disk image [%s] is malformed, please restart SiYuan kernel to rebuild it\n\t%s\n\t%v", util.DBPath, stmtSQL, args)
|
||||
}
|
||||
return
|
||||
}
|
||||
return
|
||||
|
|
@ -1500,13 +1496,14 @@ func prepareExecInsertTx(tx *sql.Tx, stmtSQL string, args []interface{}) (err er
|
|||
|
||||
func execStmtTx(tx *sql.Tx, stmt string, args ...interface{}) (err error) {
|
||||
if _, err = tx.Exec(stmt, args...); err != nil {
|
||||
tx.Rollback()
|
||||
logging.LogErrorf("exec database stmt [%s] failed: %s\n %s", stmt, err, logging.ShortStack())
|
||||
|
||||
if strings.Contains(err.Error(), "database disk image is malformed") {
|
||||
tx.Rollback()
|
||||
closeDatabase()
|
||||
removeDatabaseFile()
|
||||
util.RemoveDatabaseFile(util.DBPath)
|
||||
logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "database disk image [%s] is malformed, please restart SiYuan kernel to rebuild it\n\t%s\n\t%v", util.DBPath, stmt, args)
|
||||
}
|
||||
logging.LogErrorf("exec database stmt [%s] failed: %s\n %s", stmt, err, logging.ShortStack())
|
||||
return
|
||||
}
|
||||
return
|
||||
|
|
@ -1559,22 +1556,6 @@ func ialAttr(ial, name string) (ret string) {
|
|||
return
|
||||
}
|
||||
|
||||
func removeDatabaseFile() (err error) {
|
||||
err = os.RemoveAll(util.DBPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = os.RemoveAll(util.DBPath + "-shm")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = os.RemoveAll(util.DBPath + "-wal")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func closeDatabase() {
|
||||
if nil == db {
|
||||
return
|
||||
|
|
|
|||
|
|
@ -62,12 +62,7 @@ func initDatabase(forceRebuild bool) (err error) {
|
|||
}
|
||||
|
||||
closeDatabase()
|
||||
if gulu.File.IsExist(util.BlockTreeDBPath) {
|
||||
if err = removeDatabaseFile(); err != nil {
|
||||
logging.LogErrorf("remove database file [%s] failed: %s", util.BlockTreeDBPath, err)
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
util.RemoveDatabaseFile(util.BlockTreeDBPath)
|
||||
|
||||
initDBConnection()
|
||||
initDBTables()
|
||||
|
|
@ -141,22 +136,6 @@ func closeDatabase() {
|
|||
return
|
||||
}
|
||||
|
||||
func removeDatabaseFile() (err error) {
|
||||
err = os.RemoveAll(util.BlockTreeDBPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = os.RemoveAll(util.BlockTreeDBPath + "-shm")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = os.RemoveAll(util.BlockTreeDBPath + "-wal")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetBlockTreesByType(typ string) (ret []*BlockTree) {
|
||||
sqlStmt := "SELECT * FROM blocktrees WHERE type = ?"
|
||||
rows, err := db.Query(sqlStmt, typ)
|
||||
|
|
@ -643,7 +622,13 @@ func execInsertBlocktrees(tx *sql.Tx, tree *parse.Tree, changedNodes []*ast.Node
|
|||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
logging.LogErrorf("prepare statement [%s] failed: %s", sqlStmt, err)
|
||||
logging.LogErrorf("exec database stmt [%s] failed: %s\n %s", sqlStmt, err, logging.ShortStack())
|
||||
|
||||
if strings.Contains(err.Error(), "database disk image is malformed") {
|
||||
closeDatabase()
|
||||
util.RemoveDatabaseFile(util.BlockTreeDBPath)
|
||||
logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "database disk image [%s] is malformed, please restart SiYuan kernel to rebuild it\n\t%s", util.BlockTreeDBPath, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
|
@ -655,7 +640,13 @@ func execInsertBlocktrees(tx *sql.Tx, tree *parse.Tree, changedNodes []*ast.Node
|
|||
}
|
||||
if _, err = tx.Exec(sqlStmt, n.ID, tree.ID, parentID, tree.Box, tree.Path, tree.HPath, n.IALAttr("updated"), TypeAbbr(n.Type.String())); err != nil {
|
||||
tx.Rollback()
|
||||
logging.LogErrorf("sql exec [%s] failed: %s", sqlStmt, err)
|
||||
logging.LogErrorf("exec database stmt [%s] failed: %s\n %s", stmt, err, logging.ShortStack())
|
||||
|
||||
if strings.Contains(err.Error(), "database disk image is malformed") {
|
||||
closeDatabase()
|
||||
util.RemoveDatabaseFile(util.BlockTreeDBPath)
|
||||
logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "database disk image [%s] is malformed, please restart SiYuan kernel to rebuild it\n\t%s", util.BlockTreeDBPath, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -559,3 +559,30 @@ func LogDatabaseSize(dbPath string) {
|
|||
dbSize := humanize.BytesCustomCeil(uint64(dbFile.Size()), 2)
|
||||
logging.LogInfof("database [%s] size [%s]", dbPath, dbSize)
|
||||
}
|
||||
|
||||
func RemoveDatabaseFile(dbPath string) {
|
||||
if gulu.File.IsExist(dbPath) {
|
||||
err := os.RemoveAll(dbPath)
|
||||
if err != nil {
|
||||
logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "remove database file [%s] failed: %s", dbPath, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if gulu.File.IsExist(dbPath + "-shm") {
|
||||
err := os.RemoveAll(dbPath + "-shm")
|
||||
if err != nil {
|
||||
logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "remove database file [%s] failed: %s", dbPath+"-shm", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if gulu.File.IsExist(dbPath + "-wal") {
|
||||
err := os.RemoveAll(dbPath + "-wal")
|
||||
if err != nil {
|
||||
logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "remove database file [%s] failed: %s", dbPath+"-wal", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue