diff options
Diffstat (limited to 'upgradedb.go')
| -rwxr-xr-x | upgradedb.go | 55 |
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) +} |
