2018-11-26 21:49:55 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2018-12-07 14:46:42 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2018-11-26 21:49:55 +00:00
|
|
|
"storj.io/storj/internal/migrate"
|
|
|
|
dbx "storj.io/storj/pkg/accounting/dbx"
|
|
|
|
"storj.io/storj/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
var (
|
|
|
|
// Error is the default accountingdb errs class
|
|
|
|
Error = errs.Class("accountingdb")
|
2018-12-05 14:03:23 +00:00
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
// LastBandwidthTally is a name in the accounting timestamps database
|
2018-12-05 14:03:23 +00:00
|
|
|
LastBandwidthTally = dbx.Timestamps_Name("LastBandwidthTally")
|
2018-12-07 14:46:42 +00:00
|
|
|
)
|
2018-12-05 14:03:23 +00:00
|
|
|
|
2018-11-26 21:49:55 +00:00
|
|
|
// NewDb - constructor for DB
|
|
|
|
func NewDb(databaseURL string) (*dbx.DB, error) {
|
|
|
|
dbURL, err := utils.ParseURL(databaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
db, err := dbx.Open(dbURL.Scheme, dbURL.Path)
|
|
|
|
if err != nil {
|
2018-12-07 14:46:42 +00:00
|
|
|
return nil, Error.New("failed opening database %q, %q: %v",
|
|
|
|
dbURL.Scheme, dbURL.Path, err)
|
2018-11-26 21:49:55 +00:00
|
|
|
}
|
|
|
|
err = migrate.Create("accounting", db)
|
|
|
|
if err != nil {
|
|
|
|
_ = db.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|