behold the pristine beauty of dbversion 1

This commit is contained in:
Ted Unangst 2019-04-14 16:43:03 -04:00
parent b24f484a84
commit 85e3d2a71a
3 changed files with 60 additions and 6 deletions

19
honk.go
View File

@ -1073,11 +1073,20 @@ func main() {
if len(os.Args) > 1 { if len(os.Args) > 1 {
cmd = os.Args[1] cmd = os.Args[1]
} }
if cmd != "init" { switch cmd {
db := opendatabase() case "init":
prepareStatements(db) initdb()
getconfig("servername", &serverName) 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 { switch cmd {
case "ping": case "ping":
if len(os.Args) < 4 { if len(os.Args) < 4 {
@ -1094,8 +1103,6 @@ func main() {
ping(user, targ) ping(user, targ)
case "peep": case "peep":
peeppeep() peeppeep()
case "init":
initdb()
case "run": case "run":
serve() serve()
case "test": case "test":

41
upgradedb.go Normal file
View File

@ -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)
}

View File

@ -70,6 +70,7 @@ var dbtimeformat = "2006-01-02 15:04:05"
var alreadyopendb *sql.DB var alreadyopendb *sql.DB
var dbname = "honk.db" var dbname = "honk.db"
var stmtConfig *sql.Stmt var stmtConfig *sql.Stmt
var myVersion = 1
func initdb() { func initdb() {
schema, err := ioutil.ReadFile("schema.sql") schema, err := ioutil.ReadFile("schema.sql")
@ -182,6 +183,11 @@ func initdb() {
log.Print(err) log.Print(err)
return return
} }
_, err = db.Exec("insert into config (key, value) values (?, ?)", "dbversion", myVersion)
if err != nil {
log.Print(err)
return
}
err = finishusersetup() err = finishusersetup()
if err != nil { if err != nil {
log.Print(err) log.Print(err)