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 "strings"
23 "time"
24)
25
26var myVersion = 37
27
28func doordie(db *sql.DB, s string, args ...interface{}) {
29 _, err := db.Exec(s, args...)
30 if err != nil {
31 log.Fatalf("can't run %s: %s", s, err)
32 }
33}
34
35func upgradedb() {
36 db := opendatabase()
37 dbversion := 0
38 getconfig("dbversion", &dbversion)
39 getconfig("servername", &serverName)
40
41 if dbversion < 13 {
42 log.Fatal("database is too old to upgrade")
43 }
44 switch dbversion {
45 case 25:
46 doordie(db, "delete from auth")
47 doordie(db, "alter table auth add column expiry text")
48 doordie(db, "update config set value = 26 where key = 'dbversion'")
49 fallthrough
50 case 26:
51 s := ""
52 getconfig("servermsg", &s)
53 if s == "" {
54 setconfig("servermsg", "<h2>Things happen.</h2>")
55 }
56 s = ""
57 getconfig("aboutmsg", &s)
58 if s == "" {
59 setconfig("aboutmsg", "<h3>What is honk?</h3><p>Honk is amazing!")
60 }
61 s = ""
62 getconfig("loginmsg", &s)
63 if s == "" {
64 setconfig("loginmsg", "<h2>login</h2>")
65 }
66 d := -1
67 getconfig("debug", &d)
68 if d == -1 {
69 setconfig("debug", 0)
70 }
71 doordie(db, "update config set value = 27 where key = 'dbversion'")
72 fallthrough
73 case 27:
74 createserveruser(db)
75 doordie(db, "update config set value = 28 where key = 'dbversion'")
76 fallthrough
77 case 28:
78 doordie(db, "drop table doovers")
79 doordie(db, "create table doovers(dooverid integer primary key, dt text, tries integer, userid integer, rcpt text, msg blob)")
80 doordie(db, "update config set value = 29 where key = 'dbversion'")
81 fallthrough
82 case 29:
83 doordie(db, "alter table honkers add column owner text")
84 doordie(db, "update honkers set owner = xid")
85 doordie(db, "update config set value = 30 where key = 'dbversion'")
86 fallthrough
87 case 30:
88 tx, err := db.Begin()
89 if err != nil {
90 log.Fatal(err)
91 }
92 rows, err := tx.Query("select userid, options from users")
93 if err != nil {
94 log.Fatal(err)
95 }
96 m := make(map[int64]string)
97 for rows.Next() {
98 var userid int64
99 var options string
100 err = rows.Scan(&userid, &options)
101 if err != nil {
102 log.Fatal(err)
103 }
104 var uo UserOptions
105 uo.SkinnyCSS = strings.Contains(options, " skinny ")
106 m[userid], err = jsonify(uo)
107 if err != nil {
108 log.Fatal(err)
109 }
110 }
111 rows.Close()
112 for u, o := range m {
113 _, err = tx.Exec("update users set options = ? where userid = ?", o, u)
114 if err != nil {
115 log.Fatal(err)
116 }
117 }
118 err = tx.Commit()
119 if err != nil {
120 log.Fatal(err)
121 }
122 doordie(db, "update config set value = 31 where key = 'dbversion'")
123 fallthrough
124 case 31:
125 doordie(db, "create table tracks (xid text, fetches text)")
126 doordie(db, "create index idx_trackhonkid on tracks(xid)")
127 doordie(db, "update config set value = 32 where key = 'dbversion'")
128 fallthrough
129 case 32:
130 doordie(db, "alter table xonkers add column dt text")
131 doordie(db, "update xonkers set dt = ?", time.Now().UTC().Format(dbtimeformat))
132 doordie(db, "update config set value = 33 where key = 'dbversion'")
133 fallthrough
134 case 33:
135 doordie(db, "alter table honkers add column meta text")
136 doordie(db, "update honkers set meta = '{}'")
137 doordie(db, "update config set value = 34 where key = 'dbversion'")
138 fallthrough
139 case 34:
140 doordie(db, "create table chonks (chonkid integer primary key, userid integer, xid text, who txt, target text, dt text, noise text, format text)")
141 doordie(db, "update config set value = 35 where key = 'dbversion'")
142 fallthrough
143 case 35:
144 doordie(db, "alter table donks add column chonkid integer")
145 doordie(db, "update donks set chonkid = -1")
146 doordie(db, "create index idx_donkshonk on donks(honkid)")
147 doordie(db, "create index idx_donkschonk on donks(chonkid)")
148 doordie(db, "update config set value = 36 where key = 'dbversion'")
149 fallthrough
150 case 36:
151 doordie(db, "alter table honkers add column folxid text")
152 doordie(db, "update honkers set folxid = 'lostdata'")
153 doordie(db, "update config set value = 37 where key = 'dbversion'")
154 fallthrough
155 case 37:
156
157 default:
158 log.Fatalf("can't upgrade unknown version %d", dbversion)
159 }
160 os.Exit(0)
161}