schema updates for future changes. xonkers, zonkers, and convoys, oh my.

This commit is contained in:
Ted Unangst 2019-04-19 11:08:22 -04:00
parent c7c5001e53
commit b5249fd53d
6 changed files with 39 additions and 16 deletions

View File

@ -254,7 +254,7 @@ func savexonk(x *Honk) {
} }
dt := x.Date.UTC().Format(dbtimeformat) dt := x.Date.UTC().Format(dbtimeformat)
aud := strings.Join(x.Audience, " ") 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 { if err != nil {
log.Printf("err saving xonk: %s", err) log.Printf("err saving xonk: %s", err)
return return

6
fun.go
View File

@ -260,7 +260,7 @@ func zaggy(keyname string) (key *rsa.PublicKey) {
return return
} }
db := opendatabase() 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 var data string
err := row.Scan(&data) err := row.Scan(&data)
savekey := false savekey := false
@ -293,8 +293,8 @@ func zaggy(keyname string) (key *rsa.PublicKey) {
zaggies[keyname] = key zaggies[keyname] = key
ziggylock.Unlock() ziggylock.Unlock()
if savekey { if savekey {
db.Exec("insert into honkers (name, xid, flavor, pubkey) values (?, ?, ?, ?)", db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)",
"", keyname, "key", data) keyname, "", "", "", data)
} }
return return
} }

25
honk.go
View File

@ -65,6 +65,7 @@ type Honk struct {
Date time.Time Date time.Time
URL string URL string
Noise string Noise string
Convoy string
Audience []string Audience []string
HTML template.HTML HTML template.HTML
Donks []*Donk Donks []*Donk
@ -561,7 +562,7 @@ func getxonk(name, xid string) *Honk {
var dt, aud string var dt, aud string
row := stmtOneXonk.QueryRow(xid) row := stmtOneXonk.QueryRow(xid)
err := row.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.XID, &h.RID, 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 { if err != nil {
log.Printf("error scanning xonk: %s", err) log.Printf("error scanning xonk: %s", err)
return nil return nil
@ -605,7 +606,7 @@ func getsomehonks(rows *sql.Rows, err error) []*Honk {
var h Honk var h Honk
var dt, aud string var dt, aud string
err = rows.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.XID, &h.RID, 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 { if err != nil {
log.Printf("error scanning honks: %s", err) log.Printf("error scanning honks: %s", err)
return nil return nil
@ -662,6 +663,7 @@ func savebonk(w http.ResponseWriter, r *http.Request) {
if xonk.Honker == "" { if xonk.Honker == "" {
xonk.XID = fmt.Sprintf("https://%s/u/%s/h/%s", serverName, xonk.Username, xonk.XID) xonk.XID = fmt.Sprintf("https://%s/u/%s/h/%s", serverName, xonk.Username, xonk.XID)
} }
convoy := ""
userinfo := GetUserInfo(r) userinfo := GetUserInfo(r)
@ -674,13 +676,14 @@ func savebonk(w http.ResponseWriter, r *http.Request) {
XID: xonk.XID, XID: xonk.XID,
Date: dt, Date: dt,
Noise: xonk.Noise, Noise: xonk.Noise,
Convoy: convoy,
Donks: xonk.Donks, Donks: xonk.Donks,
Audience: oneofakind(prepend(thewholeworld, xonk.Audience)), Audience: oneofakind(prepend(thewholeworld, xonk.Audience)),
} }
aud := strings.Join(bonk.Audience, " ") aud := strings.Join(bonk.Audience, " ")
res, err := stmtSaveHonk.Exec(userinfo.UserID, "bonk", "", xid, "", 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 { if err != nil {
log.Printf("error saving bonk: %s", err) log.Printf("error saving bonk: %s", err)
return return
@ -720,6 +723,7 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
if rid != "" { if rid != "" {
what = "tonk" what = "tonk"
} }
convoy := ""
honk := Honk{ honk := Honk{
UserID: userinfo.UserID, UserID: userinfo.UserID,
Username: userinfo.Username, Username: userinfo.Username,
@ -727,6 +731,7 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
XID: xid, XID: xid,
RID: rid, RID: rid,
Date: dt, Date: dt,
Convoy: convoy,
} }
if noise[0] == '@' { if noise[0] == '@' {
honk.Audience = append(grapevine(noise), thewholeworld) honk.Audience = append(grapevine(noise), thewholeworld)
@ -813,7 +818,7 @@ func savehonk(w http.ResponseWriter, r *http.Request) {
aud := strings.Join(honk.Audience, " ") aud := strings.Join(honk.Audience, " ")
res, err := stmtSaveHonk.Exec(userinfo.UserID, what, "", xid, rid, res, err := stmtSaveHonk.Exec(userinfo.UserID, what, "", xid, rid,
dt.Format(dbtimeformat), "", aud, noise) dt.Format(dbtimeformat), "", aud, noise, convoy)
if err != nil { if err != nil {
log.Printf("error saving honk: %s", err) log.Printf("error saving honk: %s", err)
return return
@ -1050,12 +1055,12 @@ func preparetodie(db *sql.DB, s string) *sql.Stmt {
func prepareStatements(db *sql.DB) { func prepareStatements(db *sql.DB) {
stmtHonkers = preparetodie(db, "select honkerid, userid, name, xid, flavor from honkers where userid = ? and flavor = 'sub' or flavor = 'peep'") 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'") 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 = ?") 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 from honks join users on honks.userid = users.userid where honker = '' order by honkid desc limit 50") 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 from honks join users on honks.userid = users.userid where honker = '' and username = ? 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 from honks join users on honks.userid = users.userid where honks.userid = ? and dt > ? order by honkid desc limit 250") 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 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") 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) values (?, ?, ?, ?, ?, ?, ?, ?, ?)") 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 = ?") stmtFileData = preparetodie(db, "select media, content from files where xid = ?")
stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and xid = ?") stmtFindXonk = preparetodie(db, "select honkid from honks where userid = ? and xid = ?")
stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)") stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)")

View File

@ -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 donks (honkid integer, fileid integer);
create table files(fileid integer primary key, xid text, name text, url text, media text, content blob); 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 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 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_honksxid on honks(xid);
create index idx_honksconvoy on honks(convoy);
create index idx_honkshonker on honks(honker); create index idx_honkshonker on honks(honker);
create index idx_honkerxid on honkers(xid); 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_filesxid on files(xid);
create index idx_filesurl on files(url); create index idx_filesurl on files(url);

View File

@ -42,6 +42,18 @@ func upgradedb() {
doordie(db, "update config set value = 2 where key = 'dbversion'") doordie(db, "update config set value = 2 where key = 'dbversion'")
fallthrough fallthrough
case 2: 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: default:
log.Fatalf("can't upgrade unknown version %d", dbversion) log.Fatalf("can't upgrade unknown version %d", dbversion)
} }

View File

@ -70,7 +70,7 @@ var dbtimeformat = "2006-01-02 15:04:05"
var alreadyopendb *sql.DB var alreadyopendb *sql.DB
var dbname = "honk.db" var dbname = "honk.db"
var stmtConfig *sql.Stmt var stmtConfig *sql.Stmt
var myVersion = 2 var myVersion = 3
func initdb() { func initdb() {
schema, err := ioutil.ReadFile("schema.sql") schema, err := ioutil.ReadFile("schema.sql")