Start fleshing out database related functions
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package database
|
||||
|
||||
import "time"
|
||||
|
||||
// Measurement model
|
||||
type Measurement struct {
|
||||
ID int `db:"id"`
|
||||
Name string `db:"name"`
|
||||
StartTime time.Time `db:"startTime"`
|
||||
EndTime time.Time `db:"endTime"`
|
||||
Summary string `db:"summary"` // XXX this should be JSON
|
||||
ASN int `db:"asn"`
|
||||
IP string `db:"ip"`
|
||||
CountryCode string `db:"country"`
|
||||
State string `db:"state"`
|
||||
Failure string `db:"failure"`
|
||||
ReportFilePath string `db:"reportFile"`
|
||||
ReportID string `db:"reportId"`
|
||||
Input string `db:"input"`
|
||||
MeasurementID string `db:"measurementId"`
|
||||
ResultID string `db:"resultId"`
|
||||
}
|
||||
|
||||
// Result model
|
||||
type Result struct {
|
||||
ID int `db:"id"`
|
||||
Name int `db:"name"`
|
||||
StartTime time.Time `db:"startTime"`
|
||||
EndTime time.Time `db:"endTime"`
|
||||
Summary string `db:"summary"` // XXX this should be JSON
|
||||
Done bool `db:"done"`
|
||||
DataUsageUp int `db:"dataUsageUp"`
|
||||
DataUsageDown int `db:"dataUsageDown"`
|
||||
}
|
||||
Reference in New Issue
Block a user