all repos — honk @ 6dc14843423cde8c71228c26493b6135a73da059

my fork of honk

save donks for chonks
Ted Unangst tedu@tedunangst.com
Thu, 14 May 2020 23:51:42 -0400
commit

6dc14843423cde8c71228c26493b6135a73da059

parent

105c22a8a5e3b204e76d8d61e8ad105ea69ce508

6 files changed, 37 insertions(+), 7 deletions(-)

jump to
M activity.goactivity.go

@@ -902,6 +902,7 @@ Target: xonk.Honker,

Date: xonk.Date, Noise: xonk.Noise, Format: xonk.Format, + Donks: xonk.Donks, } savechonk(&ch) return nil
M database.godatabase.go

@@ -484,9 +484,26 @@ }

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 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 @@ where = "dt < ? and convoy not in (select convoy from honks where flags & 4 or whofore = 2 or whofore = 3)"

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 @@ stmtDeleteHonk = preparetodie(db, "delete from honks where honkid = ?")

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()
M honk.gohonk.go

@@ -103,6 +103,7 @@ Target string

Date time.Time Noise string Format string + Donks []*Donk Handle string HTML template.HTML }
M schema.goschema.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);
M schema.sqlschema.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);
M upgradedb.goupgradedb.go

@@ -24,7 +24,7 @@ "strings"

"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 @@ doordie(db, "create table chonks (chonkid integer primary key, userid integer, xid text, who txt, target text, dt text, noise text, format text)")

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)