all repos — honk @ 6efd4cb5d646e926f486e660ec1e59120fb64bfe

my fork of honk

behold the pristine beauty of dbversion 1
Ted Unangst tedu@tedunangst.com
Sun, 14 Apr 2019 16:43:03 -0400
commit

6efd4cb5d646e926f486e660ec1e59120fb64bfe

parent

afaa6f49b960ab605c03cb67bddd67677e7cf280

3 files changed, 60 insertions(+), 6 deletions(-)

jump to
M honk.gohonk.go

@@ -1073,11 +1073,20 @@ cmd := "run"

if len(os.Args) > 1 { cmd = os.Args[1] } - if cmd != "init" { - db := opendatabase() - prepareStatements(db) - getconfig("servername", &serverName) + switch cmd { + case "init": + initdb() + case "upgrade": + upgradedb() + } + db := opendatabase() + dbversion := 0 + getconfig("dbversion", &dbversion) + if dbversion != myVersion { + log.Fatal("incorrect database version. run upgrade.") } + getconfig("servername", &serverName) + prepareStatements(db) switch cmd { case "ping": if len(os.Args) < 4 {

@@ -1094,8 +1103,6 @@ }

ping(user, targ) case "peep": peeppeep() - case "init": - initdb() case "run": serve() case "test":
A upgradedb.go

@@ -0,0 +1,41 @@

+// +// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com> +// +// Permission to use, copy, modify, and distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +package main + +import ( + "log" + "os" +) + +func upgradedb() { + db := opendatabase() + dbversion := 0 + getconfig("dbversion", &dbversion) + + var err error + switch dbversion { + case 0: + _, err = db.Exec("insert into config (key, value) values ('dbversion', 1)") + if err != nil { + log.Fatal(err) + } + fallthrough + case 1: + default: + log.Fatalf("can't upgrade unknown version %d", dbversion) + } + os.Exit(0) +}
M util.goutil.go

@@ -70,6 +70,7 @@

var alreadyopendb *sql.DB var dbname = "honk.db" var stmtConfig *sql.Stmt +var myVersion = 1 func initdb() { schema, err := ioutil.ReadFile("schema.sql")

@@ -178,6 +179,11 @@ var randbytes [16]byte

rand.Read(randbytes[:]) key := fmt.Sprintf("%x", randbytes) _, err = db.Exec("insert into config (key, value) values (?, ?)", "csrfkey", key) + if err != nil { + log.Print(err) + return + } + _, err = db.Exec("insert into config (key, value) values (?, ?)", "dbversion", myVersion) if err != nil { log.Print(err) return