all repos — honk @ 9c2d2289ff615b4b08a39c88b72b6cfc4863860a

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	default:
46		log.Fatalf("can't upgrade unknown version %d", dbversion)
47	}
48	os.Exit(0)
49}