|
@@ -8,7 +8,6 @@ import (
|
8
|
8
|
"encoding/base64"
|
9
|
9
|
"encoding/json"
|
10
|
10
|
"fmt"
|
11
|
|
- "io"
|
12
|
11
|
"log"
|
13
|
12
|
"os"
|
14
|
13
|
"strings"
|
|
@@ -16,6 +15,7 @@ import (
|
16
|
15
|
|
17
|
16
|
"github.com/oragono/oragono/irc/modes"
|
18
|
17
|
"github.com/oragono/oragono/irc/passwd"
|
|
18
|
+ "github.com/oragono/oragono/irc/utils"
|
19
|
19
|
|
20
|
20
|
"github.com/tidwall/buntdb"
|
21
|
21
|
)
|
|
@@ -88,11 +88,7 @@ func InitDB(path string) {
|
88
|
88
|
|
89
|
89
|
// OpenDatabase returns an existing database, performing a schema version check.
|
90
|
90
|
func OpenDatabase(config *Config) (*buntdb.DB, error) {
|
91
|
|
- allowAutoupgrade := true
|
92
|
|
- if config.Datastore.AutoUpgrade != nil {
|
93
|
|
- allowAutoupgrade = *config.Datastore.AutoUpgrade
|
94
|
|
- }
|
95
|
|
- return openDatabaseInternal(config, allowAutoupgrade)
|
|
91
|
+ return openDatabaseInternal(config, config.Datastore.AutoUpgrade)
|
96
|
92
|
}
|
97
|
93
|
|
98
|
94
|
// 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,
|
140
|
136
|
}
|
141
|
137
|
}
|
142
|
138
|
|
143
|
|
-// implementation of `cp` (go should really provide this...)
|
144
|
|
-func cpFile(src string, dst string) (err error) {
|
145
|
|
- in, err := os.Open(src)
|
146
|
|
- if err != nil {
|
147
|
|
- return
|
148
|
|
- }
|
149
|
|
- defer in.Close()
|
150
|
|
- out, err := os.Create(dst)
|
151
|
|
- if err != nil {
|
152
|
|
- return
|
153
|
|
- }
|
154
|
|
- defer func() {
|
155
|
|
- closeError := out.Close()
|
156
|
|
- if err == nil {
|
157
|
|
- err = closeError
|
158
|
|
- }
|
159
|
|
- }()
|
160
|
|
- if _, err = io.Copy(out, in); err != nil {
|
161
|
|
- return
|
162
|
|
- }
|
163
|
|
- return
|
164
|
|
-}
|
165
|
|
-
|
166
|
139
|
func performAutoUpgrade(currentVersion string, config *Config) (err error) {
|
167
|
140
|
path := config.Datastore.Path
|
168
|
141
|
log.Printf("attempting to auto-upgrade schema from version %s to %s\n", currentVersion, latestDbSchema)
|
169
|
142
|
timestamp := time.Now().UTC().Format("2006-01-02-15:04:05.000Z")
|
170
|
143
|
backupPath := fmt.Sprintf("%s.v%s.%s.bak", path, currentVersion, timestamp)
|
171
|
144
|
log.Printf("making a backup of current database at %s\n", backupPath)
|
172
|
|
- err = cpFile(path, backupPath)
|
|
145
|
+ err = utils.CopyFile(path, backupPath)
|
173
|
146
|
if err != nil {
|
174
|
147
|
return err
|
175
|
148
|
}
|