-
Notifications
You must be signed in to change notification settings - Fork 402
/
database.go
127 lines (105 loc) · 2.9 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package multinodedb
import (
"context"
"strings"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/private/dbutil"
"storj.io/private/dbutil/pgutil"
"storj.io/private/tagsql"
"storj.io/storj/multinode"
"storj.io/storj/multinode/multinodedb/dbx"
"storj.io/storj/multinode/nodes"
"storj.io/storj/private/migrate"
)
var (
// ensures that multinodeDB implements multinode.DB.
_ multinode.DB = (*DB)(nil)
mon = monkit.Package()
// Error is the default multinodedb errs class.
Error = errs.Class("multinodedb")
)
// DB combines access to different database tables with a record
// of the db driver, db implementation, and db source URL.
// Implementation of multinode.DB interface.
//
// architecture: Master Database
type DB struct {
*dbx.DB
log *zap.Logger
driver string
source string
implementation dbutil.Implementation
migrationDB tagsql.DB
}
// Open creates instance of database supports postgres.
func Open(ctx context.Context, log *zap.Logger, databaseURL string) (*DB, error) {
driver, source, implementation, err := dbutil.SplitConnStr(databaseURL)
if err != nil {
return nil, err
}
switch implementation {
case dbutil.SQLite3:
source = sqlite3SetDefaultOptions(source)
case dbutil.Postgres:
source, err = pgutil.CheckApplicationName(source, "multinode")
if err != nil {
return nil, err
}
default:
return nil, Error.New("unsupported driver %q", driver)
}
dbxDB, err := dbx.Open(driver, source)
if err != nil {
return nil, Error.New("failed opening database via DBX at %q: %v",
source, err)
}
log.Debug("Connected to:", zap.String("db source", source))
dbutil.Configure(ctx, dbxDB.DB, "multinodedb", mon)
core := &DB{
DB: dbxDB,
log: log,
driver: driver,
implementation: implementation,
source: source,
}
core.migrationDB = core
return core, nil
}
// Nodes returns nodes database.
func (db *DB) Nodes() nodes.DB {
return &nodesdb{
methods: db,
}
}
// MigrateToLatest migrates db to the latest version.
func (db DB) MigrateToLatest(ctx context.Context) error {
var migration *migrate.Migration
switch db.implementation {
case dbutil.SQLite3:
migration = db.SQLite3Migration()
case dbutil.Postgres:
migration = db.PostgresMigration()
default:
return migrate.Create(ctx, "database", db.DB)
}
return migration.Run(ctx, db.log)
}
// sqlite3SetDefaultOptions sets default options for disk-based db with URI filename source string
// if no options were set.
func sqlite3SetDefaultOptions(source string) string {
if !strings.HasPrefix(source, "file:") {
return source
}
// do not set anything for in-memory db
if strings.HasPrefix(source, "file::memory:") {
return source
}
if strings.Contains(source, "?") {
return source
}
return source + "?_journal=WAL&_busy_timeout=10000"
}