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
+19 -5
View File
@@ -1,6 +1,7 @@
package database
import (
"os"
"path/filepath"
"github.com/apex/log"
@@ -29,16 +30,29 @@ func RunMigrations(db *sqlx.DB) error {
}
// Connect to the database
func Connect(path string) (*sqlx.DB, error) {
db, err := sqlx.Connect("sqlite3", path)
func Connect(path string) (db *sqlx.DB, err error) {
home, err := ooni.GetOONIHome()
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)
if err != nil {
return nil, err
db = nil
}
return db, nil
return
}
// DefaultDatabasePath for the main database