From af6e8254421411b309f1fbfdb18a92b873dd1b58 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Mon, 30 Sep 2019 16:43:51 -0400 Subject: [PATCH] add missing deletes to the db cleanup --- database.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++------- util.go | 13 ++++++++++++ 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/database.go b/database.go index 7798008..5fe0676 100644 --- a/database.go +++ b/database.go @@ -368,20 +368,68 @@ func updatehonk(h *Honk) { func cleanupdb(arg string) { db := opendatabase() days, err := strconv.Atoi(arg) + var sqlargs []interface{} + var where string if err != nil { honker := arg expdate := time.Now().UTC().Add(-3 * 24 * time.Hour).Format(dbtimeformat) - doordie(db, "delete from donks where honkid in (select honkid from honks where dt < ? and whofore = 0 and honker = ?)", expdate, honker) - doordie(db, "delete from honks where dt < ? and whofore = 0 and honker = ?", expdate, honker) + where = "dt < ? and whofore = 0 and honker = ?" + sqlargs = append(sqlargs, expdate) + sqlargs = append(sqlargs, honker) } else { expdate := time.Now().UTC().Add(-time.Duration(days) * 24 * time.Hour).Format(dbtimeformat) - doordie(db, "delete from donks where honkid in (select honkid from honks where dt < ? and whofore = 0 and convoy not in (select convoy from honks where whofore = 2 or whofore = 3))", expdate) - doordie(db, "delete from honks where dt < ? and whofore = 0 and convoy not in (select convoy from honks where whofore = 2 or whofore = 3)", expdate) + where = "dt < ? and whofore = 0 and convoy not in (select convoy from honks where whofore = 2 or whofore = 3)" + sqlargs = append(sqlargs, expdate) } + doordie(db, "delete from honks where " + where, sqlargs...) + doordie(db, "delete from donks where honkid not in (select honkid from honks)") + doordie(db, "delete from onts where honkid not in (select honkid from honks)") + doordie(db, "delete from places where honkid not in (select honkid from honks)") + doordie(db, "delete from filemeta where fileid not in (select fileid from donks)") for _, u := range allusers() { doordie(db, "delete from zonkers where userid = ? and wherefore = 'zonvoy' and zonkerid < (select zonkerid from zonkers where userid = ? and wherefore = 'zonvoy' order by zonkerid desc limit 1 offset 200)", u.UserID, u.UserID) } + + filexids := make(map[string]bool) + blobdb := openblobdb() + rows, err := blobdb.Query("select xid from filedata") + if err != nil { + log.Fatal(err) + } + for rows.Next() { + var xid string + err = rows.Scan(&xid) + if err != nil { + log.Fatal(err) + } + filexids[xid] = true + } + rows.Close() + rows, err = db.Query("select xid from filemeta") + for rows.Next() { + var xid string + err = rows.Scan(&xid) + if err != nil { + log.Fatal(err) + } + delete(filexids, xid) + } + rows.Close() + tx, err := blobdb.Begin() + if err != nil { + log.Fatal(err) + } + for xid, _ := range filexids { + _, err = tx.Exec("delete from filedata where xid = ?", xid) + if err != nil { + log.Fatal(err) + } + } + err = tx.Commit() + if err != nil { + log.Fatal(err) + } } var stmtHonkers, stmtDubbers, stmtSaveHonker, stmtUpdateFlavor, stmtUpdateCombos *sql.Stmt @@ -440,10 +488,7 @@ func prepareStatements(db *sql.DB) { stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)") stmtDeleteDonks = preparetodie(db, "delete from donks where honkid = ?") stmtSaveFile = preparetodie(db, "insert into filemeta (xid, name, description, url, media, local) values (?, ?, ?, ?, ?, ?)") - blobdb, err := sql.Open("sqlite3", blobdbname) - if err != nil { - log.Fatal(err) - } + blobdb := openblobdb() stmtSaveFileData = preparetodie(blobdb, "insert into filedata (xid, media, content) values (?, ?, ?)") stmtGetFileData = preparetodie(blobdb, "select media, content from filedata where xid = ?") stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and xid = ?") diff --git a/util.go b/util.go index 5ef9726..2e0ec9d 100644 --- a/util.go +++ b/util.go @@ -287,6 +287,19 @@ func opendatabase() *sql.DB { return db } +func openblobdb() *sql.DB { + var err error + _, err = os.Stat(blobdbname) + if err != nil { + log.Fatalf("unable to open database: %s", err) + } + db, err := sql.Open("sqlite3", blobdbname) + if err != nil { + log.Fatalf("unable to open database: %s", err) + } + return db +} + func getconfig(key string, value interface{}) error { m, ok := value.(*map[string]bool) if ok {