Skip to content

Commit

Permalink
fix wal issue
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Dec 16, 2022
1 parent dfbf203 commit a9aab89
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions db/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sqlite

import (
"database/sql"
"errors"
"mapserver/coords"
"mapserver/db"
"mapserver/public"
Expand Down Expand Up @@ -169,13 +170,32 @@ func New(filename string) (*Sqlite3Accessor, error) {
return nil, err
}

// limit connection and set a busy-timeout to prevent errors if the db should be locked sometimes
// sqlite connection limit
db.SetMaxOpenConns(1)
_, err = db.Exec("pragma busy_timeout = 5000;")

err = EnableWAL(db)
if err != nil {
return nil, err
}

sq := &Sqlite3Accessor{db: db, filename: filename}
return sq, nil
}

func EnableWAL(db *sql.DB) error {
result := db.QueryRow("pragma journal_mode;")
var mode string
err := result.Scan(&mode)
if err != nil {
return err
}

if mode != "wal" {
_, err = db.Exec("pragma journal_mode = wal;")
if err != nil {
return errors.New("couldn't switch the db-journal to wal-mode, please stop the minetest-engine to allow doing this or do it manually: " + err.Error())
}
}

return nil
}

0 comments on commit a9aab89

Please sign in to comment.