all repos — honk @ 03b9e6c2f97218bc0babcf36d3d8f08627a47f87

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	"log"
21	"os"
22)
23
24func doordie(db *sql.DB, s string) {
25	_, err := db.Exec(s)
26	if err != nil {
27		log.Fatal(err)
28	}
29}
30
31func upgradedb() {
32	db := opendatabase()
33	dbversion := 0
34	getconfig("dbversion", &dbversion)
35
36	switch dbversion {
37	case 0:
38		doordie(db, "insert into config (key, value) values ('dbversion', 1)")
39		fallthrough
40	case 1:
41		doordie(db, "create table doovers(dooverid integer primary key, dt text, tries integer, username text, rcpt text, msg blob)")
42		doordie(db, "update config set value = 2 where key = 'dbversion'")
43		fallthrough
44	case 2:
45		doordie(db, "alter table honks add column convoy text")
46		doordie(db, "update honks set convoy = ''")
47		doordie(db, "create index idx_honksconvoy on honks(convoy)")
48		doordie(db, "create table xonkers (xonkerid integer primary key, xid text, ibox text, obox text, sbox text, pubkey text)")
49		doordie(db, "insert into xonkers (xid, ibox, obox, sbox, pubkey) select xid, '', '', '', pubkey from honkers where flavor = 'key'")
50		doordie(db, "delete from honkers where flavor = 'key'")
51		doordie(db, "create index idx_xonkerxid on xonkers(xid)")
52		doordie(db, "create table zonkers (zonkerid integer primary key, userid integer, name text, wherefore text)")
53		doordie(db, "create index idx_zonkersname on zonkers(name)")
54		doordie(db, "update config set value = 3 where key = 'dbversion'")
55		fallthrough
56	case 3:
57		doordie(db, "alter table honks add column whofore integer")
58		doordie(db, "update honks set whofore = 0")
59		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)")
60		doordie(db, "update config set value = 4 where key = 'dbversion'")
61		fallthrough
62	case 4:
63	default:
64		log.Fatalf("can't upgrade unknown version %d", dbversion)
65	}
66	os.Exit(0)
67}