From 85e3d2a71a26a34f7eac140a12c99da810c5eee5 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Sun, 14 Apr 2019 16:43:03 -0400 Subject: [PATCH] behold the pristine beauty of dbversion 1 --- honk.go | 19 +++++++++++++------ upgradedb.go | 41 +++++++++++++++++++++++++++++++++++++++++ util.go | 6 ++++++ 3 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 upgradedb.go diff --git a/honk.go b/honk.go index 78ce459..68d35c5 100644 --- a/honk.go +++ b/honk.go @@ -1073,11 +1073,20 @@ func main() { 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 @@ func main() { ping(user, targ) case "peep": peeppeep() - case "init": - initdb() case "run": serve() case "test": diff --git a/upgradedb.go b/upgradedb.go new file mode 100644 index 0000000..6a47629 --- /dev/null +++ b/upgradedb.go @@ -0,0 +1,41 @@ +// +// Copyright (c) 2019 Ted Unangst +// +// 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) +} diff --git a/util.go b/util.go index 1d27585..e9af527 100644 --- a/util.go +++ b/util.go @@ -70,6 +70,7 @@ var dbtimeformat = "2006-01-02 15:04:05" var alreadyopendb *sql.DB var dbname = "honk.db" var stmtConfig *sql.Stmt +var myVersion = 1 func initdb() { schema, err := ioutil.ReadFile("schema.sql") @@ -182,6 +183,11 @@ func initdb() { log.Print(err) return } + _, err = db.Exec("insert into config (key, value) values (?, ?)", "dbversion", myVersion) + if err != nil { + log.Print(err) + return + } err = finishusersetup() if err != nil { log.Print(err)