storj/satellite/satellitedb/database.go
Yingrong Zhao 09b0c2a630
create db implementation for offer table (#2031)
* init marketing service

Fix linting error

Create offerdb implementation

Create offers service

Add update method

Create offer table and migration

Fix linting error

fix conflicts

Insert new data

Change duration to have clear indication to be based on days

add error wrapper

Change from using uuid to int for id field

* Create Marketing service

* make error virable name more readable

* add condition in update service method to check offer status

* generate lock file

Change get to listAllOffers

* Add method for getting current offer

wip

* add check for expires_at in update method

* Fix conflicts

* add copyright header

* Fix linting error

* only allow update to active offers

* add isDefault argument to GetCurrent

* Update lock file

* add migration file

* finish migrate for adding credit_in_cents for both award and invitee

* save 100 years as expiration date for default offers

* create crud test for offers

* add GetCurrent test

* modify doc

* Fix GetCurrent to work with default offer

* fix linting issue

* add more tests and address feedbacks

* fix migration file

* add type column back to match with mockup design

* add type column back to match with mockup design

* move doc changes to new pr

* add comments

* change GetCurrent to GetCurrentByType

* fix typo
2019-06-04 15:17:01 -04:00

161 lines
4.2 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/internal/dbutil"
"storj.io/storj/internal/dbutil/pgutil"
"storj.io/storj/pkg/accounting"
"storj.io/storj/pkg/audit"
"storj.io/storj/pkg/bwagreement"
"storj.io/storj/pkg/certdb"
"storj.io/storj/pkg/datarepair/irreparable"
"storj.io/storj/pkg/datarepair/queue"
"storj.io/storj/pkg/overlay"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/marketing"
"storj.io/storj/satellite/orders"
dbx "storj.io/storj/satellite/satellitedb/dbx"
)
var (
// Error is the default satellitedb errs class
Error = errs.Class("satellitedb")
)
//go:generate go run ../../scripts/lockedgen.go -o locked.go -p satellitedb -i storj.io/storj/satellite.DB
// DB contains access to different database tables
type DB struct {
log *zap.Logger
db *dbx.DB
driver string
source string
}
// New creates instance of database (supports: postgres, sqlite3)
func New(log *zap.Logger, databaseURL string) (satellite.DB, error) {
driver, source, err := dbutil.SplitConnstr(databaseURL)
if err != nil {
return nil, err
}
if driver == "postgres" {
source = pgutil.CheckApplicationName(source)
}
db, err := dbx.Open(driver, source)
if err != nil {
return nil, Error.New("failed opening database %q, %q: %v",
driver, source, err)
}
log.Debug("Connected to:", zap.String("db source", source))
db.SetMaxIdleConns(dbutil.DefaultMaxIdleConns)
core := &DB{log: log, db: db, driver: driver, source: source}
if driver == "sqlite3" {
return newLocked(core), nil
}
return core, nil
}
// NewInMemory creates instance of Sqlite in memory satellite database
func NewInMemory(log *zap.Logger) (satellite.DB, error) {
return New(log, "sqlite3://file::memory:?mode=memory")
}
// Close is used to close db connection
func (db *DB) Close() error {
return db.db.Close()
}
// CreateSchema creates a schema if it doesn't exist.
func (db *DB) CreateSchema(schema string) error {
if db.driver == "postgres" {
return pgutil.CreateSchema(db.db, schema)
}
return nil
}
// TestDBAccess for raw database access,
// should not be used outside of migration tests.
func (db *DB) TestDBAccess() *dbx.DB { return db.db }
// DropSchema drops the named schema
func (db *DB) DropSchema(schema string) error {
if db.driver == "postgres" {
return pgutil.DropSchema(db.db, schema)
}
return nil
}
// BandwidthAgreement is a getter for bandwidth agreement repository
func (db *DB) BandwidthAgreement() bwagreement.DB {
return &bandwidthagreement{db: db.db}
}
// CertDB is a getter for uplink's specific info like public key, id, etc...
func (db *DB) CertDB() certdb.DB {
return &certDB{db: db.db}
}
// // PointerDB is a getter for PointerDB repository
// func (db *DB) PointerDB() pointerdb.DB {
// return &pointerDB{db: db.db}
// }
// OverlayCache is a getter for overlay cache repository
func (db *DB) OverlayCache() overlay.DB {
return &overlaycache{db: db.db}
}
// RepairQueue is a getter for RepairQueue repository
func (db *DB) RepairQueue() queue.RepairQueue {
return &repairQueue{db: db.db}
}
// StoragenodeAccounting returns database for tracking storagenode usage
func (db *DB) StoragenodeAccounting() accounting.StoragenodeAccounting {
return &StoragenodeAccounting{db: db.db}
}
// ProjectAccounting returns database for tracking project data use
func (db *DB) ProjectAccounting() accounting.ProjectAccounting {
return &ProjectAccounting{db: db.db}
}
// Irreparable returns database for storing segments that failed repair
func (db *DB) Irreparable() irreparable.DB {
return &irreparableDB{db: db.db}
}
// Console returns database for storing users, projects and api keys
func (db *DB) Console() console.DB {
return &ConsoleDB{
db: db.db,
methods: db.db,
}
}
// Marketing returns database for storing offers and credits
func (db *DB) Marketing() marketing.DB {
return &MarketingDB{
db: db.db,
methods: db.db,
}
}
// Orders returns database for storing orders
func (db *DB) Orders() orders.DB {
return &ordersDB{db: db.db}
}
// Containment returns database for storing pending audit info
func (db *DB) Containment() audit.Containment {
return &containment{db: db.db}
}