diff --git a/activity.go b/activity.go index c1419e7..f6c9967 100644 --- a/activity.go +++ b/activity.go @@ -902,6 +902,7 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { Date: xonk.Date, Noise: xonk.Noise, Format: xonk.Format, + Donks: xonk.Donks, } savechonk(&ch) return nil diff --git a/database.go b/database.go index 149b860..77d85b2 100644 --- a/database.go +++ b/database.go @@ -484,9 +484,26 @@ func finddonk(url string) *Donk { func savechonk(ch *Chonk) error { dt := ch.Date.UTC().Format(dbtimeformat) - res, err := stmtSaveChonk.Exec(ch.UserID, ch.XID, ch.Who, ch.Target, dt, ch.Noise, ch.Format) + db := opendatabase() + tx, err := db.Begin() + if err != nil { + log.Printf("can't begin tx: %s", err) + return err + } + + res, err := tx.Stmt(stmtSaveChonk).Exec(ch.UserID, ch.XID, ch.Who, ch.Target, dt, ch.Noise, ch.Format) if err == nil { ch.ID, _ = res.LastInsertId() + for _, d := range ch.Donks { + _, err := tx.Stmt(stmtSaveDonk).Exec(-1, ch.ID, d.FileID) + if err != nil { + log.Printf("error saving donk: %s", err) + break + } + } + err = tx.Commit() + } else { + tx.Rollback() } return err } @@ -608,7 +625,7 @@ func deletehonk(honkid int64) error { func saveextras(tx *sql.Tx, h *Honk) error { for _, d := range h.Donks { - _, err := tx.Stmt(stmtSaveDonk).Exec(h.ID, d.FileID) + _, err := tx.Stmt(stmtSaveDonk).Exec(h.ID, -1, d.FileID) if err != nil { log.Printf("error saving donk: %s", err) return err @@ -706,7 +723,7 @@ func cleanupdb(arg string) { sqlargs = append(sqlargs, expdate) } doordie(db, "delete from honks where flags & 4 = 0 and whofore = 0 and "+where, sqlargs...) - doordie(db, "delete from donks where honkid not in (select honkid from honks)") + doordie(db, "delete from donks where honkid > 0 and honkid not in (select honkid from honks)") doordie(db, "delete from onts where honkid not in (select honkid from honks)") doordie(db, "delete from honkmeta where honkid not in (select honkid from honks)") @@ -818,7 +835,7 @@ func prepareStatements(db *sql.DB) { stmtUpdateHonk = preparetodie(db, "update honks set precis = ?, noise = ?, format = ?, whofore = ?, dt = ? where honkid = ?") stmtSaveOnt = preparetodie(db, "insert into onts (ontology, honkid) values (?, ?)") stmtDeleteOnts = preparetodie(db, "delete from onts where honkid = ?") - stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)") + 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() diff --git a/honk.go b/honk.go index 7a454a0..b7bc067 100644 --- a/honk.go +++ b/honk.go @@ -103,6 +103,7 @@ type Chonk struct { Date time.Time Noise string Format string + Donks []*Donk Handle string HTML template.HTML } diff --git a/schema.go b/schema.go index d27b15f..05646e1 100644 --- a/schema.go +++ b/schema.go @@ -4,7 +4,7 @@ var sqlSchema = ` create table honks (honkid integer primary key, userid integer, what text, honker text, xid text, rid text, dt text, url text, audience text, noise text, convoy text, whofore integer, format text, precis text, oonker text, flags integer); create table chonks (chonkid integer primary key, userid integer, xid text, who txt, target text, dt text, noise text, format text); -create table donks (honkid integer, fileid integer); +create table donks (honkid integer, chonkid integer, fileid integer); create table filemeta (fileid integer primary key, xid text, name text, description text, url text, media text, local integer); create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, combos text, owner text, meta text); create table xonkers (xonkerid integer primary key, name text, info text, flavor text, dt text); @@ -19,6 +19,8 @@ create index idx_honksxid on honks(xid); create index idx_honksconvoy on honks(convoy); create index idx_honkshonker on honks(honker); create index idx_honksoonker on honks(oonker); +create index idx_donkshonk on donks(honkid); +create index idx_donkschonk on donks(chonkid); create index idx_honkerxid on honkers(xid); create index idx_xonkername on xonkers(name); create index idx_zonkersname on zonkers(name); diff --git a/schema.sql b/schema.sql index f46f9a8..5b9783d 100644 --- a/schema.sql +++ b/schema.sql @@ -1,7 +1,7 @@ create table honks (honkid integer primary key, userid integer, what text, honker text, xid text, rid text, dt text, url text, audience text, noise text, convoy text, whofore integer, format text, precis text, oonker text, flags integer); create table chonks (chonkid integer primary key, userid integer, xid text, who txt, target text, dt text, noise text, format text); -create table donks (honkid integer, fileid integer); +create table donks (honkid integer, chonkid integer, fileid integer); create table filemeta (fileid integer primary key, xid text, name text, description text, url text, media text, local integer); create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, combos text, owner text, meta text); create table xonkers (xonkerid integer primary key, name text, info text, flavor text, dt text); @@ -16,6 +16,8 @@ create index idx_honksxid on honks(xid); create index idx_honksconvoy on honks(convoy); create index idx_honkshonker on honks(honker); create index idx_honksoonker on honks(oonker); +create index idx_donkshonk on donks(honkid); +create index idx_donkschonk on donks(chonkid); create index idx_honkerxid on honkers(xid); create index idx_xonkername on xonkers(name); create index idx_zonkersname on zonkers(name); diff --git a/upgradedb.go b/upgradedb.go index 7db913a..8eac574 100644 --- a/upgradedb.go +++ b/upgradedb.go @@ -24,7 +24,7 @@ import ( "time" ) -var myVersion = 35 +var myVersion = 36 func doordie(db *sql.DB, s string, args ...interface{}) { _, err := db.Exec(s, args...) @@ -370,6 +370,13 @@ func upgradedb() { doordie(db, "update config set value = 35 where key = 'dbversion'") fallthrough case 35: + doordie(db, "alter table donks add column chonkid integer") + doordie(db, "update donks set chonkid = -1") + doordie(db, "create index idx_donkshonk on donks(honkid)") + doordie(db, "create index idx_donkschonk on donks(chonkid)") + doordie(db, "update config set value = 36 where key = 'dbversion'") + fallthrough + case 36: default: log.Fatalf("can't upgrade unknown version %d", dbversion)