close databases when done
Ted Unangst tedu@tedunangst.com
Mon, 15 Jan 2024 23:49:00 -0500
4 files changed,
22 insertions(+),
10 deletions(-)
M
backend.go
→
backend.go
@@ -141,6 +141,7 @@ func orphancheck() {
var b [1]byte os.Stdin.Read(b[:]) dlog.Printf("backend shutting down") + closedatabases() os.Exit(0) }
M
database.go
→
database.go
@@ -1170,6 +1170,21 @@ }
return stmt } +var g_blobdb *sql.DB + +func closedatabases() { + err := alreadyopendb.Close() + if err != nil { + elog.Printf("error closing database: %s", err) + } + if g_blobdb != nil { + err = g_blobdb.Close() + if err != nil { + elog.Printf("error closing database: %s", err) + } + } +} + func prepareStatements(db *sql.DB) { stmtHonkers = preparetodie(db, "select honkerid, userid, name, xid, flavor, combos, meta from honkers where userid = ? and (flavor = 'presub' or flavor = 'sub' or flavor = 'peep' or flavor = 'unsub') order by name") stmtSaveHonker = preparetodie(db, "insert into honkers (userid, name, xid, flavor, combos, owner, meta, folxid) values (?, ?, ?, ?, ?, ?, ?, '')")@@ -1214,10 +1229,10 @@ stmtDeleteOnts = preparetodie(db, "delete from onts where honkid = ?")
stmtSaveDonk = preparetodie(db, "insert into donks (honkid, chonkid, fileid) values (?, ?, ?)") stmtDeleteDonks = preparetodie(db, "delete from donks where honkid = ?") stmtSaveFile = preparetodie(db, "insert into filemeta (xid, name, description, url, media, local) values (?, ?, ?, ?, ?, ?)") - blobdb := openblobdb() - stmtSaveFileData = preparetodie(blobdb, "insert into filedata (xid, media, hash, content) values (?, ?, ?, ?)") - stmtCheckFileData = preparetodie(blobdb, "select xid from filedata where hash = ?") - stmtGetFileData = preparetodie(blobdb, "select media, content from filedata where xid = ?") + g_blobdb = openblobdb() + stmtSaveFileData = preparetodie(g_blobdb, "insert into filedata (xid, media, hash, content) values (?, ?, ?, ?)") + stmtCheckFileData = preparetodie(g_blobdb, "select xid from filedata where hash = ?") + stmtGetFileData = preparetodie(g_blobdb, "select media, content from filedata where xid = ?") stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and xid = ?") stmtFindFile = preparetodie(db, "select fileid, xid from filemeta where url = ? and local = 1") stmtFindFileId = preparetodie(db, "select xid, local, description from filemeta where fileid = ? and url = ? and local = 1")
M
upgradedb.go
→
upgradedb.go
@@ -72,12 +72,6 @@ try("update config set value = ? where key = 'dbversion'", ver)
} switch dbversion { - case 40: - doordie(db, "PRAGMA journal_mode=WAL") - blobdb := openblobdb() - doordie(blobdb, "PRAGMA journal_mode=WAL") - doordie(db, "update config set value = 41 where key = 'dbversion'") - fallthrough case 41: tx, err := db.Begin() if err != nil {@@ -217,6 +211,7 @@ setV(48)
fallthrough case 48: try("analyze") + closedatabases() default: elog.Fatalf("can't upgrade unknown version %d", dbversion)