all repos — honk @ 67e3355d0a33fc01e2d3bde42656e1f0807773a7

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 = 44
 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	try := func(s string, args ...interface{}) {
 46		_, err := db.Exec(s, args...)
 47		if err != nil {
 48			elog.Fatalf("can't run %s: %s", s, err)
 49		}
 50	}
 51	setV := func(ver int64) {
 52		try("update config set value = ? where key = 'dbversion'", ver)
 53	}
 54
 55	switch dbversion {
 56	case 40:
 57		doordie(db, "PRAGMA journal_mode=WAL")
 58		blobdb := openblobdb()
 59		doordie(blobdb, "PRAGMA journal_mode=WAL")
 60		doordie(db, "update config set value = 41 where key = 'dbversion'")
 61		fallthrough
 62	case 41:
 63		tx, err := db.Begin()
 64		if err != nil {
 65			elog.Fatal(err)
 66		}
 67		rows, err := tx.Query("select honkid, noise from honks where format = 'markdown' and precis <> ''")
 68		if err != nil {
 69			elog.Fatal(err)
 70		}
 71		m := make(map[int64]string)
 72		var dummy Honk
 73		for rows.Next() {
 74			err = rows.Scan(&dummy.ID, &dummy.Noise)
 75			if err != nil {
 76				elog.Fatal(err)
 77			}
 78			precipitate(&dummy)
 79			m[dummy.ID] = dummy.Noise
 80		}
 81		rows.Close()
 82		for id, noise := range m {
 83			_, err = tx.Exec("update honks set noise = ? where honkid = ?", noise, id)
 84			if err != nil {
 85				elog.Fatal(err)
 86			}
 87		}
 88		err = tx.Commit()
 89		if err != nil {
 90			elog.Fatal(err)
 91		}
 92		doordie(db, "update config set value = 42 where key = 'dbversion'")
 93		fallthrough
 94	case 42:
 95		doordie(db, "update honks set what = 'honk', flags = flags & ~ 32 where what = 'tonk' or what = 'wonk'")
 96		doordie(db, "delete from honkmeta where genus = 'wonkles' or genus = 'guesses'")
 97		doordie(db, "update config set value = 43 where key = 'dbversion'")
 98		fallthrough
 99	case 43:
100		try("alter table honks add column plain text")
101		try("update honks set plain = ''")
102		setV(44)
103		fallthrough
104	case 44:
105
106	default:
107		elog.Fatalf("can't upgrade unknown version %d", dbversion)
108	}
109	os.Exit(0)
110}