|
@@ -52,14 +52,26 @@ func (err *incompatibleSchemaError) Error() string {
|
52
|
52
|
return fmt.Sprintf("Database requires update. Expected schema v%s, got v%s", err.requiredVersion, err.currentVersion)
|
53
|
53
|
}
|
54
|
54
|
|
55
|
|
-// InitDB creates the database.
|
|
55
|
+// InitDB creates the database, implementing the `oragono initdb` command.
|
56
|
56
|
func InitDB(path string) {
|
57
|
|
- // prepare kvstore db
|
58
|
|
- //TODO(dan): fail if already exists instead? don't want to overwrite good data
|
59
|
|
- os.Remove(path)
|
|
57
|
+ _, err := os.Stat(path)
|
|
58
|
+ if err == nil {
|
|
59
|
+ log.Fatal("Datastore already exists (delete it manually to continue): ", path)
|
|
60
|
+ } else if !os.IsNotExist(err) {
|
|
61
|
+ log.Fatal("Datastore path is inaccessible: ", err.Error())
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ err = initializeDB(path)
|
|
65
|
+ if err != nil {
|
|
66
|
+ log.Fatal("Could not save datastore: ", err.Error())
|
|
67
|
+ }
|
|
68
|
+}
|
|
69
|
+
|
|
70
|
+// internal database initialization code
|
|
71
|
+func initializeDB(path string) error {
|
60
|
72
|
store, err := buntdb.Open(path)
|
61
|
73
|
if err != nil {
|
62
|
|
- log.Fatal(fmt.Sprintf("Failed to open datastore: %s", err.Error()))
|
|
74
|
+ return err
|
63
|
75
|
}
|
64
|
76
|
defer store.Close()
|
65
|
77
|
|
|
@@ -69,9 +81,7 @@ func InitDB(path string) {
|
69
|
81
|
return nil
|
70
|
82
|
})
|
71
|
83
|
|
72
|
|
- if err != nil {
|
73
|
|
- log.Fatal("Could not save datastore:", err.Error())
|
74
|
|
- }
|
|
84
|
+ return err
|
75
|
85
|
}
|
76
|
86
|
|
77
|
87
|
// OpenDatabase returns an existing database, performing a schema version check.
|
|
@@ -81,10 +91,6 @@ func OpenDatabase(config *Config) (*buntdb.DB, error) {
|
81
|
91
|
|
82
|
92
|
// open the database, giving it at most one chance to auto-upgrade the schema
|
83
|
93
|
func openDatabaseInternal(config *Config, allowAutoupgrade bool) (db *buntdb.DB, err error) {
|
84
|
|
- _, err = os.Stat(config.Datastore.Path)
|
85
|
|
- if os.IsNotExist(err) {
|
86
|
|
- return
|
87
|
|
- }
|
88
|
94
|
db, err = buntdb.Open(config.Datastore.Path)
|
89
|
95
|
if err != nil {
|
90
|
96
|
return
|