review fixes

This commit is contained in:
Shivaram Lingamneni 2018-04-20 03:57:48 -04:00
parent 69fd3ac324
commit 3db71415c9
4 changed files with 38 additions and 33 deletions

View File

@ -230,7 +230,7 @@ type Config struct {
Datastore struct { Datastore struct {
Path string Path string
AutoUpgrade *bool AutoUpgrade bool
} }
Accounts AccountConfig Accounts AccountConfig

View File

@ -8,7 +8,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"strings" "strings"
@ -16,6 +15,7 @@ import (
"github.com/oragono/oragono/irc/modes" "github.com/oragono/oragono/irc/modes"
"github.com/oragono/oragono/irc/passwd" "github.com/oragono/oragono/irc/passwd"
"github.com/oragono/oragono/irc/utils"
"github.com/tidwall/buntdb" "github.com/tidwall/buntdb"
) )
@ -88,11 +88,7 @@ func InitDB(path string) {
// OpenDatabase returns an existing database, performing a schema version check. // OpenDatabase returns an existing database, performing a schema version check.
func OpenDatabase(config *Config) (*buntdb.DB, error) { func OpenDatabase(config *Config) (*buntdb.DB, error) {
allowAutoupgrade := true return openDatabaseInternal(config, config.Datastore.AutoUpgrade)
if config.Datastore.AutoUpgrade != nil {
allowAutoupgrade = *config.Datastore.AutoUpgrade
}
return openDatabaseInternal(config, allowAutoupgrade)
} }
// open the database, giving it at most one chance to auto-upgrade the schema // 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) { func performAutoUpgrade(currentVersion string, config *Config) (err error) {
path := config.Datastore.Path path := config.Datastore.Path
log.Printf("attempting to auto-upgrade schema from version %s to %s\n", currentVersion, latestDbSchema) 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") timestamp := time.Now().UTC().Format("2006-01-02-15:04:05.000Z")
backupPath := fmt.Sprintf("%s.v%s.%s.bak", path, currentVersion, timestamp) backupPath := fmt.Sprintf("%s.v%s.%s.bak", path, currentVersion, timestamp)
log.Printf("making a backup of current database at %s\n", backupPath) log.Printf("making a backup of current database at %s\n", backupPath)
err = cpFile(path, backupPath) err = utils.CopyFile(path, backupPath)
if err != nil { if err != nil {
return err return err
} }

31
irc/utils/os.go Normal file
View File

@ -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
}

View File

@ -341,8 +341,9 @@ debug:
datastore: datastore:
# path to the datastore # path to the datastore
path: ircd.db 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. # up, and if the upgrade fails, the original database will be restored.
autoupgrade: true autoupgrade: true