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 "regexp"
24 "strings"
25)
26
27func doordie(db *sql.DB, s string, args ...interface{}) {
28 _, err := db.Exec(s, args...)
29 if err != nil {
30 log.Fatal(err)
31 }
32}
33
34func upgradedb() {
35 db := opendatabase()
36 dbversion := 0
37 getconfig("dbversion", &dbversion)
38 getconfig("servername", &serverName)
39
40 switch dbversion {
41 case 0:
42 doordie(db, "insert into config (key, value) values ('dbversion', 1)")
43 fallthrough
44 case 1:
45 doordie(db, "create table doovers(dooverid integer primary key, dt text, tries integer, username text, rcpt text, msg blob)")
46 doordie(db, "update config set value = 2 where key = 'dbversion'")
47 fallthrough
48 case 2:
49 doordie(db, "alter table honks add column convoy text")
50 doordie(db, "update honks set convoy = ''")
51 doordie(db, "create index idx_honksconvoy on honks(convoy)")
52 doordie(db, "create table xonkers (xonkerid integer primary key, xid text, ibox text, obox text, sbox text, pubkey text)")
53 doordie(db, "insert into xonkers (xid, ibox, obox, sbox, pubkey) select xid, '', '', '', pubkey from honkers where flavor = 'key'")
54 doordie(db, "delete from honkers where flavor = 'key'")
55 doordie(db, "create index idx_xonkerxid on xonkers(xid)")
56 doordie(db, "create table zonkers (zonkerid integer primary key, userid integer, name text, wherefore text)")
57 doordie(db, "create index idx_zonkersname on zonkers(name)")
58 doordie(db, "update config set value = 3 where key = 'dbversion'")
59 fallthrough
60 case 3:
61 doordie(db, "alter table honks add column whofore integer")
62 doordie(db, "update honks set whofore = 0")
63 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)")
64 doordie(db, "update config set value = 4 where key = 'dbversion'")
65 fallthrough
66 case 4:
67 doordie(db, "alter table honkers add column combos text")
68 doordie(db, "update honkers set combos = ''")
69 doordie(db, "update config set value = 5 where key = 'dbversion'")
70 fallthrough
71 case 5:
72 doordie(db, "delete from donks where honkid in (select honkid from honks where what = 'zonk')")
73 doordie(db, "delete from honks where what = 'zonk'")
74 doordie(db, "update config set value = 6 where key = 'dbversion'")
75 fallthrough
76 case 6:
77 doordie(db, "alter table honks add column format")
78 doordie(db, "update honks set format = 'html'")
79 doordie(db, "alter table honks add column precis")
80 doordie(db, "update honks set precis = ''")
81 doordie(db, "alter table honks add column oonker")
82 doordie(db, "update honks set oonker = ''")
83 doordie(db, "update config set value = 7 where key = 'dbversion'")
84 fallthrough
85 case 7:
86 users := allusers()
87 for _, u := range users {
88 h := fmt.Sprintf("https://%s/u/%s", serverName, u.Username)
89 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)
90 doordie(db, "update honks set honker = ?, whofore = 2 where userid = ? and honker = '' and what = 'bonk'", h, u.UserID)
91 }
92 doordie(db, "update config set value = 8 where key = 'dbversion'")
93 fallthrough
94 case 8:
95 doordie(db, "alter table files add column local integer")
96 doordie(db, "update files set local = 1")
97 doordie(db, "update config set value = 9 where key = 'dbversion'")
98 fallthrough
99 case 9:
100 doordie(db, "drop table xonkers")
101 doordie(db, "create table xonkers (xonkerid integer primary key, name text, info text, flavor text)")
102 doordie(db, "create index idx_xonkername on xonkers(name)")
103 doordie(db, "update config set value = 10 where key = 'dbversion'")
104 fallthrough
105 case 10:
106 doordie(db, "update zonkers set wherefore = 'zomain' where wherefore = 'zurl'")
107 doordie(db, "update zonkers set wherefore = 'zord' where wherefore = 'zword'")
108 doordie(db, "update config set value = 11 where key = 'dbversion'")
109 fallthrough
110 case 11:
111 doordie(db, "alter table users add column options text")
112 doordie(db, "update users set options = ''")
113 doordie(db, "update config set value = 12 where key = 'dbversion'")
114 fallthrough
115 case 12:
116 doordie(db, "create index idx_honksoonker on honks(oonker)")
117 doordie(db, "update config set value = 13 where key = 'dbversion'")
118 fallthrough
119 case 13:
120 doordie(db, "alter table honks add column flags integer")
121 doordie(db, "update honks set flags = 0")
122 doordie(db, "update config set value = 14 where key = 'dbversion'")
123 fallthrough
124 case 14:
125 doordie(db, "create table onts (ontology text, honkid integer)")
126 doordie(db, "create index idx_ontology on onts(ontology)")
127 doordie(db, "update config set value = 15 where key = 'dbversion'")
128 fallthrough
129 case 15:
130 doordie(db, "delete from onts")
131 ontmap := make(map[int64][]string)
132 rows, err := db.Query("select honkid, noise from honks")
133 if err != nil {
134 log.Fatalf("can't query honks: %s", err)
135 }
136 re_more := regexp.MustCompile(`#<span>[[:alpha:]][[:alnum:]-]*`)
137 for rows.Next() {
138 var honkid int64
139 var noise string
140 err := rows.Scan(&honkid, &noise)
141 if err != nil {
142 log.Fatalf("can't scan honks: %s", err)
143 }
144 onts := ontologies(noise)
145 mo := re_more.FindAllString(noise, -1)
146 for _, o := range mo {
147 onts = append(onts, "#"+o[7:])
148 }
149 if len(onts) > 0 {
150 ontmap[honkid] = oneofakind(onts)
151 }
152 }
153 rows.Close()
154 tx, err := db.Begin()
155 if err != nil {
156 log.Fatalf("can't begin: %s", err)
157 }
158 stmtOnts, err := tx.Prepare("insert into onts (ontology, honkid) values (?, ?)")
159 if err != nil {
160 log.Fatal(err)
161 }
162 for honkid, onts := range ontmap {
163 for _, o := range onts {
164 _, err = stmtOnts.Exec(strings.ToLower(o), honkid)
165 if err != nil {
166 log.Fatal(err)
167 }
168 }
169 }
170 err = tx.Commit()
171 if err != nil {
172 log.Fatalf("can't commit: %s", err)
173 }
174 doordie(db, "update config set value = 16 where key = 'dbversion'")
175 fallthrough
176 case 16:
177 doordie(db, "alter table files add column description text")
178 doordie(db, "update files set description = name")
179 doordie(db, "update config set value = 17 where key = 'dbversion'")
180 fallthrough
181 case 17:
182 doordie(db, "create table forsaken (honkid integer, precis text, noise text)")
183 doordie(db, "update config set value = 18 where key = 'dbversion'")
184 fallthrough
185 case 18:
186 doordie(db, "create index idx_onthonkid on onts(honkid)")
187 doordie(db, "update config set value = 19 where key = 'dbversion'")
188 case 19:
189 default:
190 log.Fatalf("can't upgrade unknown version %d", dbversion)
191 }
192 os.Exit(0)
193}