Initial creation of the '.ooni/db' folders when not yet setup

This commit is contained in:
Will Scott 2018-03-08 02:25:40 -08:00
parent 2d8420f069
commit 8d5da14c4b
2 changed files with 24 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package database package database
import ( import (
"os"
"path/filepath" "path/filepath"
"github.com/apex/log" "github.com/apex/log"
@ -29,16 +30,29 @@ func RunMigrations(db *sqlx.DB) error {
} }
// Connect to the database // Connect to the database
func Connect(path string) (*sqlx.DB, error) { func Connect(path string) (db *sqlx.DB, err error) {
db, err := sqlx.Connect("sqlite3", path) home, err := ooni.GetOONIHome()
if err != nil { if err != nil {
return nil, err return
} }
if _, e := os.Stat(filepath.Join(home, "db")); e != nil {
err = os.MkdirAll(filepath.Join(home, "db"), 0700)
if err != nil {
return
}
}
db, err = sqlx.Connect("sqlite3", path)
if err != nil {
return
}
err = RunMigrations(db) err = RunMigrations(db)
if err != nil { if err != nil {
return nil, err db = nil
} }
return db, nil return
} }
// DefaultDatabasePath for the main database // DefaultDatabasePath for the main database

View File

@ -154,6 +154,8 @@ func ReadConfig(path string) (*Config, error) {
if err = c.Validate(); err != nil { if err = c.Validate(); err != nil {
return nil, errors.Wrap(err, "validating") return nil, errors.Wrap(err, "validating")
} }
return c, nil
} }
if err != nil { if err != nil {
@ -186,5 +188,7 @@ func ReadDefaultConfigPaths() (*Config, error) {
return c, nil return c, nil
} }
} }
return nil, errors.New("failed to find a config")
// Run from the default config
return ReadConfig(paths[0])
} }