summaryrefslogtreecommitdiff
path: root/upgradedb.go
diff options
context:
space:
mode:
authorNicolas Sterchele <nicolas@sterchelen.net>2022-08-31 23:08:52 +0200
committerNicolas Sterchele <nicolas@sterchelen.net>2022-08-31 23:19:58 +0200
commitb9281c89737419216b710a87c31686d21adf86bc (patch)
treebc2b2c73b35ead61bee37b75aec1eee2d78c6ef4 /upgradedb.go
initial commit
Thanks to Ted Unangst for its work. Originally available here https://humungus.tedunangst.com/r/inks
Diffstat (limited to 'upgradedb.go')
-rwxr-xr-xupgradedb.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/upgradedb.go b/upgradedb.go
new file mode 100755
index 0000000..ec7a8cb
--- /dev/null
+++ b/upgradedb.go
@@ -0,0 +1,55 @@
+//
+// 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 (
+ "database/sql"
+ "log"
+ "os"
+)
+
+var dbVersion = 2
+
+func doordie(db *sql.DB, s string, args ...interface{}) {
+ _, err := db.Exec(s, args...)
+ if err != nil {
+ log.Fatalf("can't run %s: %s", s, err)
+ }
+}
+
+func upgradedb() {
+ db := opendatabase()
+ ver := 0
+ getconfig("dbversion", &ver)
+
+ switch ver {
+ case 0:
+ doordie(db, "drop table auth")
+ doordie(db, "CREATE TABLE auth (authid integer primary key, userid integer, hash text, expiry text)")
+ doordie(db, "CREATE INDEX idxauth_hash on auth(hash)")
+ doordie(db, "insert into config (key, value) values ('dbversion', 1)")
+ fallthrough
+ case 1:
+ doordie(db, "create table sources (sourceid integer primary key, name text, notes text)")
+ doordie(db, "update config set value = 2 where key = 'dbversion'")
+ fallthrough
+ case 2:
+
+ default:
+ log.Fatalf("can't upgrade unknown version %d", ver)
+ }
+ os.Exit(0)
+}