2019-04-14 22:43:03 +02:00
|
|
|
//
|
|
|
|
// 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 (
|
2019-04-15 03:35:42 +02:00
|
|
|
"database/sql"
|
2019-04-14 22:43:03 +02:00
|
|
|
"os"
|
2019-08-25 05:03:05 +02:00
|
|
|
"strings"
|
2019-10-08 07:31:37 +02:00
|
|
|
"time"
|
2019-04-14 22:43:03 +02:00
|
|
|
)
|
|
|
|
|
2023-02-24 19:09:45 +01:00
|
|
|
var myVersion = 42
|
2019-10-16 22:43:43 +02:00
|
|
|
|
2020-09-02 22:31:02 +02:00
|
|
|
type dbexecer interface {
|
|
|
|
Exec(query string, args ...interface{}) (sql.Result, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doordie(db dbexecer, s string, args ...interface{}) {
|
2019-05-20 16:48:02 +02:00
|
|
|
_, err := db.Exec(s, args...)
|
2019-04-15 03:35:42 +02:00
|
|
|
if err != nil {
|
2022-02-06 06:42:13 +01:00
|
|
|
elog.Fatalf("can't run %s: %s", s, err)
|
2019-04-15 03:35:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 22:43:03 +02:00
|
|
|
func upgradedb() {
|
|
|
|
db := opendatabase()
|
|
|
|
dbversion := 0
|
|
|
|
getconfig("dbversion", &dbversion)
|
2019-05-20 16:48:02 +02:00
|
|
|
getconfig("servername", &serverName)
|
2019-04-14 22:43:03 +02:00
|
|
|
|
2023-07-08 18:32:48 +02:00
|
|
|
if dbversion < 40 {
|
2022-02-06 06:42:13 +01:00
|
|
|
elog.Fatal("database is too old to upgrade")
|
2019-10-07 01:00:03 +02:00
|
|
|
}
|
2019-04-14 22:43:03 +02:00
|
|
|
switch dbversion {
|
2020-09-30 21:20:40 +02:00
|
|
|
case 40:
|
2022-05-31 09:06:09 +02:00
|
|
|
doordie(db, "PRAGMA journal_mode=WAL")
|
|
|
|
blobdb := openblobdb()
|
|
|
|
doordie(blobdb, "PRAGMA journal_mode=WAL")
|
|
|
|
doordie(db, "update config set value = 41 where key = 'dbversion'")
|
|
|
|
fallthrough
|
|
|
|
case 41:
|
2023-02-24 19:09:45 +01:00
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
elog.Fatal(err)
|
|
|
|
}
|
|
|
|
rows, err := tx.Query("select honkid, noise from honks where format = 'markdown' and precis <> ''")
|
|
|
|
if err != nil {
|
|
|
|
elog.Fatal(err)
|
|
|
|
}
|
|
|
|
m := make(map[int64]string)
|
|
|
|
var dummy Honk
|
|
|
|
for rows.Next() {
|
|
|
|
err = rows.Scan(&dummy.ID, &dummy.Noise)
|
|
|
|
if err != nil {
|
|
|
|
elog.Fatal(err)
|
|
|
|
}
|
|
|
|
precipitate(&dummy)
|
|
|
|
m[dummy.ID] = dummy.Noise
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
for id, noise := range m {
|
|
|
|
_, err = tx.Exec("update honks set noise = ? where honkid = ?", noise, id)
|
|
|
|
if err != nil {
|
|
|
|
elog.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
elog.Fatal(err)
|
|
|
|
}
|
|
|
|
doordie(db, "update config set value = 42 where key = 'dbversion'")
|
|
|
|
fallthrough
|
|
|
|
case 42:
|
2019-10-20 03:12:08 +02:00
|
|
|
|
2019-04-14 22:43:03 +02:00
|
|
|
default:
|
2022-02-06 06:42:13 +01:00
|
|
|
elog.Fatalf("can't upgrade unknown version %d", dbversion)
|
2019-04-14 22:43:03 +02:00
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|