Browse Source

review fixes

tags/v0.12.0
Shivaram Lingamneni 6 years ago
parent
commit
3db71415c9
4 changed files with 38 additions and 33 deletions
  1. 1
    1
      irc/config.go
  2. 3
    30
      irc/database.go
  3. 31
    0
      irc/utils/os.go
  4. 3
    2
      oragono.yaml

+ 1
- 1
irc/config.go View File

230
 
230
 
231
 	Datastore struct {
231
 	Datastore struct {
232
 		Path        string
232
 		Path        string
233
-		AutoUpgrade *bool
233
+		AutoUpgrade bool
234
 	}
234
 	}
235
 
235
 
236
 	Accounts AccountConfig
236
 	Accounts AccountConfig

+ 3
- 30
irc/database.go View File

8
 	"encoding/base64"
8
 	"encoding/base64"
9
 	"encoding/json"
9
 	"encoding/json"
10
 	"fmt"
10
 	"fmt"
11
-	"io"
12
 	"log"
11
 	"log"
13
 	"os"
12
 	"os"
14
 	"strings"
13
 	"strings"
16
 
15
 
17
 	"github.com/oragono/oragono/irc/modes"
16
 	"github.com/oragono/oragono/irc/modes"
18
 	"github.com/oragono/oragono/irc/passwd"
17
 	"github.com/oragono/oragono/irc/passwd"
18
+	"github.com/oragono/oragono/irc/utils"
19
 
19
 
20
 	"github.com/tidwall/buntdb"
20
 	"github.com/tidwall/buntdb"
21
 )
21
 )
88
 
88
 
89
 // OpenDatabase returns an existing database, performing a schema version check.
89
 // OpenDatabase returns an existing database, performing a schema version check.
90
 func OpenDatabase(config *Config) (*buntdb.DB, error) {
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
 // open the database, giving it at most one chance to auto-upgrade the schema
94
 // open the database, giving it at most one chance to auto-upgrade the schema
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
 func performAutoUpgrade(currentVersion string, config *Config) (err error) {
139
 func performAutoUpgrade(currentVersion string, config *Config) (err error) {
167
 	path := config.Datastore.Path
140
 	path := config.Datastore.Path
168
 	log.Printf("attempting to auto-upgrade schema from version %s to %s\n", currentVersion, latestDbSchema)
141
 	log.Printf("attempting to auto-upgrade schema from version %s to %s\n", currentVersion, latestDbSchema)
169
 	timestamp := time.Now().UTC().Format("2006-01-02-15:04:05.000Z")
142
 	timestamp := time.Now().UTC().Format("2006-01-02-15:04:05.000Z")
170
 	backupPath := fmt.Sprintf("%s.v%s.%s.bak", path, currentVersion, timestamp)
143
 	backupPath := fmt.Sprintf("%s.v%s.%s.bak", path, currentVersion, timestamp)
171
 	log.Printf("making a backup of current database at %s\n", backupPath)
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
 	if err != nil {
146
 	if err != nil {
174
 		return err
147
 		return err
175
 	}
148
 	}

+ 31
- 0
irc/utils/os.go View File

1
+// Copyright (c) 2018 Shivaram Lingamneni
2
+
3
+package utils
4
+
5
+import (
6
+	"io"
7
+	"os"
8
+)
9
+
10
+// implementation of `cp` (go should really provide this...)
11
+func CopyFile(src string, dst string) (err error) {
12
+	in, err := os.Open(src)
13
+	if err != nil {
14
+		return
15
+	}
16
+	defer in.Close()
17
+	out, err := os.Create(dst)
18
+	if err != nil {
19
+		return
20
+	}
21
+	defer func() {
22
+		closeError := out.Close()
23
+		if err == nil {
24
+			err = closeError
25
+		}
26
+	}()
27
+	if _, err = io.Copy(out, in); err != nil {
28
+		return
29
+	}
30
+	return
31
+}

+ 3
- 2
oragono.yaml View File

341
 datastore:
341
 datastore:
342
     # path to the datastore
342
     # path to the datastore
343
     path: ircd.db
343
     path: ircd.db
344
-    # if the database schema requires an upgrade, `autoupgrade` (which defaults to true)
345
-    # will attempt to perform it automatically on startup. the database will be backed
344
+
345
+    # if the database schema requires an upgrade, `autoupgrade` will attempt to
346
+    # perform it automatically on startup. the database will be backed
346
     # up, and if the upgrade fails, the original database will be restored.
347
     # up, and if the upgrade fails, the original database will be restored.
347
     autoupgrade: true
348
     autoupgrade: true
348
 
349
 

Loading…
Cancel
Save