storj/pkg/accounting/db.go
JT Olio 1c96db01ba
better database error handling (#784)
* better database error handling

Change-Id: I28dbd69cf6c2fa268e02405521ff6e6c1a68a702

* missing comments added

* missing comment added
2018-12-07 07:46:42 -07:00

40 lines
909 B
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package accounting
import (
"github.com/zeebo/errs"
"storj.io/storj/internal/migrate"
dbx "storj.io/storj/pkg/accounting/dbx"
"storj.io/storj/pkg/utils"
)
var (
// Error is the default accountingdb errs class
Error = errs.Class("accountingdb")
// LastBandwidthTally is a name in the accounting timestamps database
LastBandwidthTally = dbx.Timestamps_Name("LastBandwidthTally")
)
// 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 {
return nil, Error.New("failed opening database %q, %q: %v",
dbURL.Scheme, dbURL.Path, err)
}
err = migrate.Create("accounting", db)
if err != nil {
_ = db.Close()
return nil, err
}
return db, nil
}