2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2014-03-02 00:02:24 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2018-04-04 03:49:40 +02:00
|
|
|
"encoding/json"
|
2014-03-08 03:14:02 +01:00
|
|
|
"fmt"
|
2014-03-02 00:02:24 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
2016-11-06 04:47:13 +01:00
|
|
|
"strings"
|
2018-04-16 22:28:31 +02:00
|
|
|
"time"
|
2016-06-15 13:50:56 +02:00
|
|
|
|
2018-04-04 03:49:40 +02:00
|
|
|
"github.com/oragono/oragono/irc/modes"
|
2018-04-20 09:57:48 +02:00
|
|
|
"github.com/oragono/oragono/irc/utils"
|
2017-10-05 16:03:53 +02:00
|
|
|
|
2016-09-05 10:45:42 +02:00
|
|
|
"github.com/tidwall/buntdb"
|
2014-03-02 00:02:24 +01:00
|
|
|
)
|
|
|
|
|
2016-09-05 10:45:42 +02:00
|
|
|
const (
|
2016-09-17 13:23:04 +02:00
|
|
|
// 'version' of the database schema
|
|
|
|
keySchemaVersion = "db.version"
|
2016-11-06 04:47:13 +01:00
|
|
|
// latest schema of the db
|
2018-04-04 03:49:40 +02:00
|
|
|
latestDbSchema = "3"
|
2016-09-05 10:45:42 +02:00
|
|
|
)
|
|
|
|
|
2018-04-04 03:49:40 +02:00
|
|
|
type SchemaChanger func(*Config, *buntdb.Tx) error
|
|
|
|
|
|
|
|
type SchemaChange struct {
|
|
|
|
InitialVersion string // the change will take this version
|
|
|
|
TargetVersion string // and transform it into this version
|
|
|
|
Changer SchemaChanger
|
|
|
|
}
|
|
|
|
|
|
|
|
// maps an initial version to a schema change capable of upgrading it
|
|
|
|
var schemaChanges map[string]SchemaChange
|
|
|
|
|
2018-04-16 22:28:31 +02:00
|
|
|
type incompatibleSchemaError struct {
|
|
|
|
currentVersion string
|
|
|
|
requiredVersion string
|
|
|
|
}
|
|
|
|
|
|
|
|
func IncompatibleSchemaError(currentVersion string) (result *incompatibleSchemaError) {
|
|
|
|
return &incompatibleSchemaError{
|
|
|
|
currentVersion: currentVersion,
|
|
|
|
requiredVersion: latestDbSchema,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *incompatibleSchemaError) Error() string {
|
|
|
|
return fmt.Sprintf("Database requires update. Expected schema v%s, got v%s", err.requiredVersion, err.currentVersion)
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:23:04 +02:00
|
|
|
// InitDB creates the database.
|
|
|
|
func InitDB(path string) {
|
2016-09-05 10:45:42 +02:00
|
|
|
// prepare kvstore db
|
2016-09-17 13:23:04 +02:00
|
|
|
//TODO(dan): fail if already exists instead? don't want to overwrite good data
|
|
|
|
os.Remove(path)
|
|
|
|
store, err := buntdb.Open(path)
|
2016-09-05 10:45:42 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(fmt.Sprintf("Failed to open datastore: %s", err.Error()))
|
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
|
|
|
err = store.Update(func(tx *buntdb.Tx) error {
|
2016-09-17 13:23:04 +02:00
|
|
|
// set schema version
|
2018-04-04 03:49:40 +02:00
|
|
|
tx.Set(keySchemaVersion, latestDbSchema, nil)
|
2016-09-05 10:45:42 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2016-11-06 04:47:13 +01:00
|
|
|
log.Fatal("Could not save datastore:", err.Error())
|
2016-09-05 10:45:42 +02:00
|
|
|
}
|
2014-03-02 00:02:24 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// OpenDatabase returns an existing database, performing a schema version check.
|
2018-04-16 22:28:31 +02:00
|
|
|
func OpenDatabase(config *Config) (*buntdb.DB, error) {
|
2018-04-20 09:57:48 +02:00
|
|
|
return openDatabaseInternal(config, config.Datastore.AutoUpgrade)
|
2018-04-16 22:28:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// open the database, giving it at most one chance to auto-upgrade the schema
|
|
|
|
func openDatabaseInternal(config *Config, allowAutoupgrade bool) (db *buntdb.DB, err error) {
|
|
|
|
db, err = buntdb.Open(config.Datastore.Path)
|
2017-09-28 07:30:53 +02:00
|
|
|
if err != nil {
|
2018-04-16 22:28:31 +02:00
|
|
|
return
|
2017-09-28 07:30:53 +02:00
|
|
|
}
|
|
|
|
|
2018-04-16 22:28:31 +02:00
|
|
|
defer func() {
|
|
|
|
if err != nil && db != nil {
|
|
|
|
db.Close()
|
|
|
|
db = nil
|
2017-09-28 07:30:53 +02:00
|
|
|
}
|
2018-04-16 22:28:31 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
// read the current version string
|
|
|
|
var version string
|
|
|
|
err = db.View(func(tx *buntdb.Tx) error {
|
|
|
|
version, err = tx.Get(keySchemaVersion)
|
|
|
|
return err
|
2017-09-28 07:30:53 +02:00
|
|
|
})
|
2018-04-16 22:28:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if version == latestDbSchema {
|
|
|
|
// success
|
|
|
|
return
|
|
|
|
}
|
2017-09-28 07:30:53 +02:00
|
|
|
|
2018-04-16 22:28:31 +02:00
|
|
|
// XXX quiesce the DB so we can be sure it's safe to make a backup copy
|
|
|
|
db.Close()
|
|
|
|
db = nil
|
|
|
|
if allowAutoupgrade {
|
|
|
|
err = performAutoUpgrade(version, config)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// successful autoupgrade, let's try this again:
|
|
|
|
return openDatabaseInternal(config, false)
|
|
|
|
} else {
|
|
|
|
err = IncompatibleSchemaError(version)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func performAutoUpgrade(currentVersion string, config *Config) (err error) {
|
|
|
|
path := config.Datastore.Path
|
|
|
|
log.Printf("attempting to auto-upgrade schema from version %s to %s\n", currentVersion, latestDbSchema)
|
|
|
|
timestamp := time.Now().UTC().Format("2006-01-02-15:04:05.000Z")
|
|
|
|
backupPath := fmt.Sprintf("%s.v%s.%s.bak", path, currentVersion, timestamp)
|
|
|
|
log.Printf("making a backup of current database at %s\n", backupPath)
|
2018-04-20 09:57:48 +02:00
|
|
|
err = utils.CopyFile(path, backupPath)
|
2018-04-16 22:28:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = UpgradeDB(config)
|
|
|
|
if err != nil {
|
|
|
|
// database upgrade is a single transaction, so we don't need to restore the backup;
|
|
|
|
// we can just delete it
|
|
|
|
os.Remove(backupPath)
|
|
|
|
}
|
|
|
|
return err
|
2017-09-28 07:30:53 +02:00
|
|
|
}
|
|
|
|
|
2016-09-17 13:23:04 +02:00
|
|
|
// UpgradeDB upgrades the datastore to the latest schema.
|
2018-04-16 22:28:31 +02:00
|
|
|
func UpgradeDB(config *Config) (err error) {
|
2018-04-04 03:49:40 +02:00
|
|
|
store, err := buntdb.Open(config.Datastore.Path)
|
2016-11-06 04:47:13 +01:00
|
|
|
if err != nil {
|
2018-04-16 22:28:31 +02:00
|
|
|
return err
|
2016-11-06 04:47:13 +01:00
|
|
|
}
|
|
|
|
defer store.Close()
|
|
|
|
|
2018-04-04 03:49:40 +02:00
|
|
|
var version string
|
2016-11-06 04:47:13 +01:00
|
|
|
err = store.Update(func(tx *buntdb.Tx) error {
|
2018-04-04 03:49:40 +02:00
|
|
|
for {
|
|
|
|
version, _ = tx.Get(keySchemaVersion)
|
|
|
|
change, schemaNeedsChange := schemaChanges[version]
|
|
|
|
if !schemaNeedsChange {
|
2018-04-16 22:28:31 +02:00
|
|
|
if version == latestDbSchema {
|
|
|
|
// success!
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// unable to upgrade to the desired version, roll back
|
|
|
|
return IncompatibleSchemaError(version)
|
2016-11-06 04:47:13 +01:00
|
|
|
}
|
2018-04-16 22:28:31 +02:00
|
|
|
log.Println("attempting to update schema from version " + version)
|
2018-04-04 03:49:40 +02:00
|
|
|
err := change.Changer(config, tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-11-06 04:47:13 +01:00
|
|
|
}
|
2018-04-04 03:49:40 +02:00
|
|
|
_, _, err = tx.Set(keySchemaVersion, change.TargetVersion, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-16 22:28:31 +02:00
|
|
|
log.Println("successfully updated schema to version " + change.TargetVersion)
|
2016-11-06 04:47:13 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2018-04-04 03:49:40 +02:00
|
|
|
|
2016-11-06 04:47:13 +01:00
|
|
|
if err != nil {
|
2018-04-16 22:28:31 +02:00
|
|
|
log.Println("database upgrade failed and was rolled back")
|
2016-11-06 04:47:13 +01:00
|
|
|
}
|
2018-04-16 22:28:31 +02:00
|
|
|
return err
|
2014-03-02 00:02:24 +01:00
|
|
|
}
|
2018-04-04 03:49:40 +02:00
|
|
|
|
|
|
|
func schemaChangeV1toV2(config *Config, tx *buntdb.Tx) error {
|
|
|
|
// == version 1 -> 2 ==
|
|
|
|
// account key changes and account.verified key bugfix.
|
|
|
|
|
|
|
|
var keysToRemove []string
|
|
|
|
newKeys := make(map[string]string)
|
|
|
|
|
|
|
|
tx.AscendKeys("account *", func(key, value string) bool {
|
|
|
|
keysToRemove = append(keysToRemove, key)
|
|
|
|
splitkey := strings.Split(key, " ")
|
|
|
|
|
|
|
|
// work around bug
|
|
|
|
if splitkey[2] == "exists" {
|
|
|
|
// manually create new verified key
|
|
|
|
newVerifiedKey := fmt.Sprintf("%s.verified %s", splitkey[0], splitkey[1])
|
|
|
|
newKeys[newVerifiedKey] = "1"
|
|
|
|
} else if splitkey[1] == "%s" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
newKey := fmt.Sprintf("%s.%s %s", splitkey[0], splitkey[2], splitkey[1])
|
|
|
|
newKeys[newKey] = value
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, key := range keysToRemove {
|
|
|
|
tx.Delete(key)
|
|
|
|
}
|
|
|
|
for key, value := range newKeys {
|
|
|
|
tx.Set(key, value, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. channel founder names should be casefolded
|
|
|
|
// 2. founder should be explicitly granted the ChannelFounder user mode
|
|
|
|
// 3. explicitly initialize stored channel modes to the server default values
|
|
|
|
func schemaChangeV2ToV3(config *Config, tx *buntdb.Tx) error {
|
|
|
|
var channels []string
|
|
|
|
prefix := "channel.exists "
|
|
|
|
tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
|
|
|
|
if !strings.HasPrefix(key, prefix) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
chname := strings.TrimPrefix(key, prefix)
|
|
|
|
channels = append(channels, chname)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
// founder names should be casefolded
|
|
|
|
// founder should be explicitly granted the ChannelFounder user mode
|
|
|
|
for _, channel := range channels {
|
|
|
|
founderKey := "channel.founder " + channel
|
|
|
|
founder, _ := tx.Get(founderKey)
|
|
|
|
if founder != "" {
|
|
|
|
founder, err := CasefoldName(founder)
|
|
|
|
if err == nil {
|
|
|
|
tx.Set(founderKey, founder, nil)
|
|
|
|
accountToUmode := map[string]modes.Mode{
|
|
|
|
founder: modes.ChannelFounder,
|
|
|
|
}
|
|
|
|
atustr, _ := json.Marshal(accountToUmode)
|
|
|
|
tx.Set("channel.accounttoumode "+channel, string(atustr), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// explicitly store the channel modes
|
2018-08-28 19:34:43 +02:00
|
|
|
defaultModes := config.Channels.defaultModes
|
2018-04-04 03:49:40 +02:00
|
|
|
modeStrings := make([]string, len(defaultModes))
|
|
|
|
for i, mode := range defaultModes {
|
|
|
|
modeStrings[i] = string(mode)
|
|
|
|
}
|
|
|
|
defaultModeString := strings.Join(modeStrings, "")
|
|
|
|
for _, channel := range channels {
|
|
|
|
tx.Set("channel.modes "+channel, defaultModeString, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
allChanges := []SchemaChange{
|
2018-04-23 02:36:50 +02:00
|
|
|
{
|
2018-04-04 03:49:40 +02:00
|
|
|
InitialVersion: "1",
|
|
|
|
TargetVersion: "2",
|
|
|
|
Changer: schemaChangeV1toV2,
|
|
|
|
},
|
2018-04-23 02:36:50 +02:00
|
|
|
{
|
2018-04-04 03:49:40 +02:00
|
|
|
InitialVersion: "2",
|
|
|
|
TargetVersion: "3",
|
|
|
|
Changer: schemaChangeV2ToV3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// build the index
|
|
|
|
schemaChanges = make(map[string]SchemaChange)
|
|
|
|
for _, change := range allChanges {
|
|
|
|
schemaChanges[change.InitialVersion] = change
|
|
|
|
}
|
|
|
|
}
|