diff --git a/irc/config.go b/irc/config.go index 3a1282ac..3d1b6504 100644 --- a/irc/config.go +++ b/irc/config.go @@ -230,7 +230,7 @@ type Config struct { Datastore struct { Path string - AutoUpgrade *bool + AutoUpgrade bool } Accounts AccountConfig diff --git a/irc/database.go b/irc/database.go index d92c2027..54f9f469 100644 --- a/irc/database.go +++ b/irc/database.go @@ -8,7 +8,6 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io" "log" "os" "strings" @@ -16,6 +15,7 @@ import ( "github.com/oragono/oragono/irc/modes" "github.com/oragono/oragono/irc/passwd" + "github.com/oragono/oragono/irc/utils" "github.com/tidwall/buntdb" ) @@ -88,11 +88,7 @@ func InitDB(path string) { // OpenDatabase returns an existing database, performing a schema version check. func OpenDatabase(config *Config) (*buntdb.DB, error) { - allowAutoupgrade := true - if config.Datastore.AutoUpgrade != nil { - allowAutoupgrade = *config.Datastore.AutoUpgrade - } - return openDatabaseInternal(config, allowAutoupgrade) + return openDatabaseInternal(config, config.Datastore.AutoUpgrade) } // open the database, giving it at most one chance to auto-upgrade the schema @@ -140,36 +136,13 @@ func openDatabaseInternal(config *Config, allowAutoupgrade bool) (db *buntdb.DB, } } -// implementation of `cp` (go should really provide this...) -func cpFile(src string, dst string) (err error) { - in, err := os.Open(src) - if err != nil { - return - } - defer in.Close() - out, err := os.Create(dst) - if err != nil { - return - } - defer func() { - closeError := out.Close() - if err == nil { - err = closeError - } - }() - if _, err = io.Copy(out, in); err != nil { - return - } - 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) - err = cpFile(path, backupPath) + err = utils.CopyFile(path, backupPath) if err != nil { return err } diff --git a/irc/utils/os.go b/irc/utils/os.go new file mode 100644 index 00000000..ef3eb5d1 --- /dev/null +++ b/irc/utils/os.go @@ -0,0 +1,31 @@ +// Copyright (c) 2018 Shivaram Lingamneni + +package utils + +import ( + "io" + "os" +) + +// implementation of `cp` (go should really provide this...) +func CopyFile(src string, dst string) (err error) { + in, err := os.Open(src) + if err != nil { + return + } + defer in.Close() + out, err := os.Create(dst) + if err != nil { + return + } + defer func() { + closeError := out.Close() + if err == nil { + err = closeError + } + }() + if _, err = io.Copy(out, in); err != nil { + return + } + return +} diff --git a/oragono.yaml b/oragono.yaml index 7fca18a7..9bb17b45 100644 --- a/oragono.yaml +++ b/oragono.yaml @@ -341,8 +341,9 @@ debug: datastore: # path to the datastore path: ircd.db - # if the database schema requires an upgrade, `autoupgrade` (which defaults to true) - # will attempt to perform it automatically on startup. the database will be backed + + # if the database schema requires an upgrade, `autoupgrade` will attempt to + # perform it automatically on startup. the database will be backed # up, and if the upgrade fails, the original database will be restored. autoupgrade: true