Start fleshing out database related functions
This commit is contained in:
parent
6008b5c7c5
commit
ca231bca9f
8 changed files with 511 additions and 12 deletions
44
internal/database/database.go
Normal file
44
internal/database/database.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/openobservatory/gooni/internal/bindata"
|
||||
"github.com/pkg/errors"
|
||||
migrate "github.com/rubenv/sql-migrate"
|
||||
)
|
||||
|
||||
// RunMigrations runs the database migrations
|
||||
func RunMigrations(db *sqlx.DB) error {
|
||||
migrations := &migrate.AssetMigrationSource{
|
||||
Asset: bindata.Asset,
|
||||
AssetDir: bindata.AssetDir,
|
||||
Dir: "data/migrations",
|
||||
}
|
||||
n, err := migrate.Exec(db.DB, "sqlite3", migrations, migrate.Up)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debugf("performed %d migrations", n)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Connect(path string) (*sqlx.DB, error) {
|
||||
db, err := sqlx.Connect("sqlite3", path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// XXX RunMigrations(db)
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func DefaultDatabasePath() (string, error) {
|
||||
home, err := GetOONIHome()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "default database path")
|
||||
}
|
||||
return filepath.Join(home, "db", "main.db"), nil
|
||||
}
|
||||
Loading…
Reference in a new issue