all repos — honk @ dc081988e58738d4f2b6749d088bd5dd2753b46b

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	"os"
21)
22
23var myVersion = 43
24
25type dbexecer interface {
26	Exec(query string, args ...interface{}) (sql.Result, error)
27}
28
29func doordie(db dbexecer, s string, args ...interface{}) {
30	_, err := db.Exec(s, args...)
31	if err != nil {
32		elog.Fatalf("can't run %s: %s", s, err)
33	}
34}
35
36func upgradedb() {
37	db := opendatabase()
38	dbversion := 0
39	getconfig("dbversion", &dbversion)
40	getconfig("servername", &serverName)
41
42	if dbversion < 40 {
43		elog.Fatal("database is too old to upgrade")
44	}
45	switch dbversion {
46	case 40:
47		doordie(db, "PRAGMA journal_mode=WAL")
48		blobdb := openblobdb()
49		doordie(blobdb, "PRAGMA journal_mode=WAL")
50		doordie(db, "update config set value = 41 where key = 'dbversion'")
51		fallthrough
52	case 41:
53		tx, err := db.Begin()
54		if err != nil {
55			elog.Fatal(err)
56		}
57		rows, err := tx.Query("select honkid, noise from honks where format = 'markdown' and precis <> ''")
58		if err != nil {
59			elog.Fatal(err)
60		}
61		m := make(map[int64]string)
62		var dummy Honk
63		for rows.Next() {
64			err = rows.Scan(&dummy.ID, &dummy.Noise)
65			if err != nil {
66				elog.Fatal(err)
67			}
68			precipitate(&dummy)
69			m[dummy.ID] = dummy.Noise
70		}
71		rows.Close()
72		for id, noise := range m {
73			_, err = tx.Exec("update honks set noise = ? where honkid = ?", noise, id)
74			if err != nil {
75				elog.Fatal(err)
76			}
77		}
78		err = tx.Commit()
79		if err != nil {
80			elog.Fatal(err)
81		}
82		doordie(db, "update config set value = 42 where key = 'dbversion'")
83		fallthrough
84	case 42:
85		doordie(db, "update honks set what = 'honk', flags = flags & ~ 32 where what = 'tonk' or what = 'wonk'")
86		doordie(db, "delete from honkmeta where genus = 'wonkles' or genus = 'guesses'")
87		doordie(db, "update config set value = 43 where key = 'dbversion'")
88		fallthrough
89	case 43:
90
91	default:
92		elog.Fatalf("can't upgrade unknown version %d", dbversion)
93	}
94	os.Exit(0)
95}