all repos — honk @ e83036b3857fa7fb068d5f35c3b966855ed5be9f

my fork of honk

upgradedb.go (view raw)

  1//
  2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
  3//
  4// Permission to use, copy, modify, and distribute this software for any
  5// purpose with or without fee is hereby granted, provided that the above
  6// copyright notice and this permission notice appear in all copies.
  7//
  8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15
 16package main
 17
 18import (
 19	"database/sql"
 20	"fmt"
 21	"log"
 22	"os"
 23)
 24
 25func doordie(db *sql.DB, s string, args ...interface{}) {
 26	_, err := db.Exec(s, args...)
 27	if err != nil {
 28		log.Fatal(err)
 29	}
 30}
 31
 32func upgradedb() {
 33	db := opendatabase()
 34	dbversion := 0
 35	getconfig("dbversion", &dbversion)
 36	getconfig("servername", &serverName)
 37
 38	switch dbversion {
 39	case 0:
 40		doordie(db, "insert into config (key, value) values ('dbversion', 1)")
 41		fallthrough
 42	case 1:
 43		doordie(db, "create table doovers(dooverid integer primary key, dt text, tries integer, username text, rcpt text, msg blob)")
 44		doordie(db, "update config set value = 2 where key = 'dbversion'")
 45		fallthrough
 46	case 2:
 47		doordie(db, "alter table honks add column convoy text")
 48		doordie(db, "update honks set convoy = ''")
 49		doordie(db, "create index idx_honksconvoy on honks(convoy)")
 50		doordie(db, "create table xonkers (xonkerid integer primary key, xid text, ibox text, obox text, sbox text, pubkey text)")
 51		doordie(db, "insert into xonkers (xid, ibox, obox, sbox, pubkey) select xid, '', '', '', pubkey from honkers where flavor = 'key'")
 52		doordie(db, "delete from honkers where flavor = 'key'")
 53		doordie(db, "create index idx_xonkerxid on xonkers(xid)")
 54		doordie(db, "create table zonkers (zonkerid integer primary key, userid integer, name text, wherefore text)")
 55		doordie(db, "create index idx_zonkersname on zonkers(name)")
 56		doordie(db, "update config set value = 3 where key = 'dbversion'")
 57		fallthrough
 58	case 3:
 59		doordie(db, "alter table honks add column whofore integer")
 60		doordie(db, "update honks set whofore = 0")
 61		doordie(db, "update honks set whofore = 1 where honkid in (select honkid from honks join users on honks.userid = users.userid where instr(audience, username) > 0)")
 62		doordie(db, "update config set value = 4 where key = 'dbversion'")
 63		fallthrough
 64	case 4:
 65		doordie(db, "alter table honkers add column combos text")
 66		doordie(db, "update honkers set combos = ''")
 67		doordie(db, "update config set value = 5 where key = 'dbversion'")
 68		fallthrough
 69	case 5:
 70		doordie(db, "delete from donks where honkid in (select honkid from honks where what = 'zonk')")
 71		doordie(db, "delete from honks where what = 'zonk'")
 72		doordie(db, "update config set value = 6 where key = 'dbversion'")
 73		fallthrough
 74	case 6:
 75		doordie(db, "alter table honks add column format")
 76		doordie(db, "update honks set format = 'html'")
 77		doordie(db, "alter table honks add column precis")
 78		doordie(db, "update honks set precis = ''")
 79		doordie(db, "alter table honks add column oonker")
 80		doordie(db, "update honks set oonker = ''")
 81		doordie(db, "update config set value = 7 where key = 'dbversion'")
 82		fallthrough
 83	case 7:
 84		users := allusers()
 85		for _, u := range users {
 86			h := fmt.Sprintf("https://%s/u/%s", serverName, u.Username)
 87			doordie(db, fmt.Sprintf("update honks set xid = '%s/h/' || xid, honker = ?, whofore = 2 where userid = ? and honker = '' and (what = 'honk' or what = 'tonk')", h), h, u.UserID)
 88			doordie(db, "update honks set honker = ?, whofore = 2 where userid = ? and honker = '' and what = 'bonk'", h, u.UserID)
 89		}
 90		doordie(db, "update config set value = 8 where key = 'dbversion'")
 91	case 8:
 92		doordie(db, "alter table files add column local integer")
 93		doordie(db, "update files set local = 1")
 94		doordie(db, "update config set value = 9 where key = 'dbversion'")
 95	case 9:
 96		doordie(db, "drop table xonkers")
 97		doordie(db, "create table xonkers (xonkerid integer primary key, name text, info text, flavor text)")
 98		doordie(db, "create index idx_xonkername on xonkers(name)")
 99		doordie(db, "update config set value = 10 where key = 'dbversion'")
100	case 10:
101		doordie(db, "update zonkers set wherefore = 'zomain' where wherefore = 'zurl'")
102		doordie(db, "update zonkers set wherefore = 'zord' where wherefore = 'zword'")
103		doordie(db, "update config set value = 11 where key = 'dbversion'")
104	case 11:
105	default:
106		log.Fatalf("can't upgrade unknown version %d", dbversion)
107	}
108	cleanupdb(30)
109	os.Exit(0)
110}