From b5249fd53db40ffae0bb87cc740366178bf4c72b Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Fri, 19 Apr 2019 11:08:22 -0400 Subject: [PATCH] schema updates for future changes. xonkers, zonkers, and convoys, oh my. --- activity.go | 2 +- fun.go | 6 +++--- honk.go | 25 +++++++++++++++---------- schema.sql | 8 +++++++- upgradedb.go | 12 ++++++++++++ util.go | 2 +- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/activity.go b/activity.go index a3a4417..729e303 100644 --- a/activity.go +++ b/activity.go @@ -254,7 +254,7 @@ func savexonk(x *Honk) { } dt := x.Date.UTC().Format(dbtimeformat) aud := strings.Join(x.Audience, " ") - res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud, x.Noise) + res, err := stmtSaveHonk.Exec(x.UserID, x.What, x.Honker, x.XID, x.RID, dt, x.URL, aud, x.Noise, x.Convoy) if err != nil { log.Printf("err saving xonk: %s", err) return diff --git a/fun.go b/fun.go index 3e6d26b..8986ff9 100644 --- a/fun.go +++ b/fun.go @@ -260,7 +260,7 @@ func zaggy(keyname string) (key *rsa.PublicKey) { return } db := opendatabase() - row := db.QueryRow("select pubkey from honkers where flavor = 'key' and xid = ?", keyname) + row := db.QueryRow("select pubkey from xonkers where xid = ?", keyname) var data string err := row.Scan(&data) savekey := false @@ -293,8 +293,8 @@ func zaggy(keyname string) (key *rsa.PublicKey) { zaggies[keyname] = key ziggylock.Unlock() if savekey { - db.Exec("insert into honkers (name, xid, flavor, pubkey) values (?, ?, ?, ?)", - "", keyname, "key", data) + db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)", + keyname, "", "", "", data) } return } diff --git a/honk.go b/honk.go index 5a76bd9..e14f07a 100644 --- a/honk.go +++ b/honk.go @@ -65,6 +65,7 @@ type Honk struct { Date time.Time URL string Noise string + Convoy string Audience []string HTML template.HTML Donks []*Donk @@ -561,7 +562,7 @@ func getxonk(name, xid string) *Honk { var dt, aud string row := stmtOneXonk.QueryRow(xid) err := row.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.XID, &h.RID, - &dt, &h.URL, &aud, &h.Noise) + &dt, &h.URL, &aud, &h.Noise, &h.Convoy) if err != nil { log.Printf("error scanning xonk: %s", err) return nil @@ -605,7 +606,7 @@ func getsomehonks(rows *sql.Rows, err error) []*Honk { var h Honk var dt, aud string err = rows.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.XID, &h.RID, - &dt, &h.URL, &aud, &h.Noise) + &dt, &h.URL, &aud, &h.Noise, &h.Convoy) if err != nil { log.Printf("error scanning honks: %s", err) return nil @@ -662,6 +663,7 @@ func savebonk(w http.ResponseWriter, r *http.Request) { if xonk.Honker == "" { xonk.XID = fmt.Sprintf("https://%s/u/%s/h/%s", serverName, xonk.Username, xonk.XID) } + convoy := "" userinfo := GetUserInfo(r) @@ -674,13 +676,14 @@ func savebonk(w http.ResponseWriter, r *http.Request) { XID: xonk.XID, Date: dt, Noise: xonk.Noise, + Convoy: convoy, Donks: xonk.Donks, Audience: oneofakind(prepend(thewholeworld, xonk.Audience)), } aud := strings.Join(bonk.Audience, " ") res, err := stmtSaveHonk.Exec(userinfo.UserID, "bonk", "", xid, "", - dt.Format(dbtimeformat), "", aud, bonk.Noise) + dt.Format(dbtimeformat), "", aud, bonk.Noise, bonk.Convoy) if err != nil { log.Printf("error saving bonk: %s", err) return @@ -720,6 +723,7 @@ func savehonk(w http.ResponseWriter, r *http.Request) { if rid != "" { what = "tonk" } + convoy := "" honk := Honk{ UserID: userinfo.UserID, Username: userinfo.Username, @@ -727,6 +731,7 @@ func savehonk(w http.ResponseWriter, r *http.Request) { XID: xid, RID: rid, Date: dt, + Convoy: convoy, } if noise[0] == '@' { honk.Audience = append(grapevine(noise), thewholeworld) @@ -813,7 +818,7 @@ func savehonk(w http.ResponseWriter, r *http.Request) { aud := strings.Join(honk.Audience, " ") res, err := stmtSaveHonk.Exec(userinfo.UserID, what, "", xid, rid, - dt.Format(dbtimeformat), "", aud, noise) + dt.Format(dbtimeformat), "", aud, noise, convoy) if err != nil { log.Printf("error saving honk: %s", err) return @@ -1050,12 +1055,12 @@ func preparetodie(db *sql.DB, s string) *sql.Stmt { func prepareStatements(db *sql.DB) { stmtHonkers = preparetodie(db, "select honkerid, userid, name, xid, flavor from honkers where userid = ? and flavor = 'sub' or flavor = 'peep'") stmtDubbers = preparetodie(db, "select honkerid, userid, name, xid, flavor from honkers where userid = ? and flavor = 'dub'") - stmtOneXonk = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise from honks join users on honks.userid = users.userid where xid = ?") - stmtHonks = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise from honks join users on honks.userid = users.userid where honker = '' order by honkid desc limit 50") - stmtUserHonks = preparetodie(db, "select honkid, honks.userid, username, what, honker, xid, rid, dt, url, audience, noise from honks join users on honks.userid = users.userid where honker = '' and username = ? order by honkid desc limit 50") - stmtHonksForUser = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise from honks join users on honks.userid = users.userid where honks.userid = ? and dt > ? order by honkid desc limit 250") - stmtHonksByHonker = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, honks.xid, rid, dt, url, audience, noise from honks join users on honks.userid = users.userid join honkers on honkers.xid = honks.honker where honks.userid = ? and honkers.name = ? order by honkid desc limit 50") - stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise) values (?, ?, ?, ?, ?, ?, ?, ?, ?)") + stmtOneXonk = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where xid = ?") + stmtHonks = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where honker = '' order by honkid desc limit 50") + stmtUserHonks = preparetodie(db, "select honkid, honks.userid, username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where honker = '' and username = ? order by honkid desc limit 50") + stmtHonksForUser = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid where honks.userid = ? and dt > ? order by honkid desc limit 250") + stmtHonksByHonker = preparetodie(db, "select honkid, honks.userid, users.username, what, honker, honks.xid, rid, dt, url, audience, noise, convoy from honks join users on honks.userid = users.userid join honkers on honkers.xid = honks.honker where honks.userid = ? and honkers.name = ? order by honkid desc limit 50") + stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") stmtFileData = preparetodie(db, "select media, content from files where xid = ?") stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and xid = ?") stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)") diff --git a/schema.sql b/schema.sql index 1bdaf37..66c5bff 100644 --- a/schema.sql +++ b/schema.sql @@ -1,13 +1,19 @@ -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); +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); create table donks (honkid integer, fileid integer); create table files(fileid integer primary key, xid text, name text, url text, media text, content blob); create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, pubkey text); +create table xonkers (xonkerid integer primary key, xid text, ibox text, obox text, sbox text, pubkey text); +create table zonkers (zonkerid integer primary key, name text, wherefore text); create table doovers(dooverid integer primary key, dt text, tries integer, username text, rcpt text, msg blob); create index idx_honksxid on honks(xid); +create index idx_honksconvoy on honks(convoy); create index idx_honkshonker on honks(honker); create index idx_honkerxid on honkers(xid); +create index idx_xonkerxid on xonkers(xid); +create index idx_xonkerxid on xonkers(xid); +create index idx_zonkersname on zonkers(name); create index idx_filesxid on files(xid); create index idx_filesurl on files(url); diff --git a/upgradedb.go b/upgradedb.go index 792f941..65fc437 100644 --- a/upgradedb.go +++ b/upgradedb.go @@ -42,6 +42,18 @@ func upgradedb() { doordie(db, "update config set value = 2 where key = 'dbversion'") fallthrough case 2: + doordie(db, "alter table honks add column convoy text") + doordie(db, "update honks set convoy = ''") + doordie(db, "create index idx_honksconvoy on honks(convoy)") + doordie(db, "create table xonkers (xonkerid integer primary key, xid text, ibox text, obox text, sbox text, pubkey text)") + doordie(db, "insert into xonkers (xid, ibox, obox, sbox, pubkey) select xid, '', '', '', pubkey from honkers where flavor = 'key'") + doordie(db, "delete from honkers where flavor = 'key'") + doordie(db, "create index idx_xonkerxid on xonkers(xid)") + doordie(db, "create table zonkers (zonkerid integer primary key, name text, wherefore text)") + doordie(db, "create index idx_zonkersname on zonkers(name)") + doordie(db, "update config set value = 3 where key = 'dbversion'") + fallthrough + case 3: default: log.Fatalf("can't upgrade unknown version %d", dbversion) } diff --git a/util.go b/util.go index b1c4b91..c4550de 100644 --- a/util.go +++ b/util.go @@ -70,7 +70,7 @@ var dbtimeformat = "2006-01-02 15:04:05" var alreadyopendb *sql.DB var dbname = "honk.db" var stmtConfig *sql.Stmt -var myVersion = 2 +var myVersion = 3 func initdb() { schema, err := ioutil.ReadFile("schema.sql")