forked from vmware-archive/atc
/
db.go
168 lines (131 loc) · 4.13 KB
/
db.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package db
import (
"database/sql"
"database/sql/driver"
"errors"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/event"
"github.com/lib/pq"
)
//go:generate counterfeiter . Conn
type Conn interface {
Begin() (Tx, error)
Close() error
Driver() driver.Driver
Exec(query string, args ...interface{}) (sql.Result, error)
Ping() error
Prepare(query string) (*sql.Stmt, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
SetMaxIdleConns(n int)
SetMaxOpenConns(n int)
}
//go:generate counterfeiter . Tx
type Tx interface {
Commit() error
Exec(query string, args ...interface{}) (sql.Result, error)
Prepare(query string) (*sql.Stmt, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
Rollback() error
Stmt(stmt *sql.Stmt) *sql.Stmt
}
func Wrap(sqlDB *sql.DB) Conn {
return &wrappedDB{DB: sqlDB}
}
func WrapWithError(sqlDB *sql.DB, err error) (Conn, error) {
return &wrappedDB{DB: sqlDB}, err
}
type wrappedDB struct {
*sql.DB
}
func (wrapped *wrappedDB) Begin() (Tx, error) {
return wrapped.DB.Begin()
}
func swallowUniqueViolation(err error) error {
if err != nil {
if pgErr, ok := err.(*pq.Error); ok {
if pgErr.Code.Class().Name() == "integrity_constraint_violation" {
return nil
}
}
return err
}
return nil
}
type DB interface {
GetTeams() ([]SavedTeam, error)
CreateTeam(team Team) (SavedTeam, error)
CreateDefaultTeamIfNotExists() error
DeleteTeamByName(teamName string) error
GetAllStartedBuilds() ([]Build, error)
GetPublicBuilds(page Page) ([]Build, Pagination, error)
FindJobIDForBuild(buildID int) (int, bool, error)
CreatePipe(pipeGUID string, url string, teamID int) error
GetPipe(pipeGUID string) (Pipe, error)
GetTaskLock(logger lager.Logger, taskName string) (Lock, bool, error)
DeleteBuildEventsByBuildIDs(buildIDs []int) error
Workers() ([]SavedWorker, error) // auto-expires workers based on ttl
GetWorker(workerName string) (SavedWorker, bool, error)
SaveWorker(WorkerInfo, time.Duration) (SavedWorker, error)
GetContainer(string) (SavedContainer, bool, error)
CreateContainer(container Container, ttl time.Duration, maxLifetime time.Duration, volumeHandles []string) (SavedContainer, error)
FindContainerByIdentifier(ContainerIdentifier) (SavedContainer, bool, error)
FindLatestSuccessfulBuildsPerJob() (map[int]int, error)
FindJobContainersFromUnsuccessfulBuilds() ([]SavedContainer, error)
UpdateExpiresAtOnContainer(handle string, ttl time.Duration) error
ReapContainer(handle string) error
DeleteContainer(string) error
InsertVolume(data Volume) error
GetVolumes() ([]SavedVolume, error)
GetVolumesByIdentifier(VolumeIdentifier) ([]SavedVolume, error)
ReapVolume(string) error
SetVolumeTTLAndSizeInBytes(string, time.Duration, int64) error
SetVolumeTTL(string, time.Duration) error
GetVolumeTTL(volumeHandle string) (time.Duration, bool, error)
GetVolumesForOneOffBuildImageResources() ([]SavedVolume, error)
}
//go:generate counterfeiter . Notifier
type Notifier interface {
Notify() <-chan struct{}
Close() error
}
//ConfigVersion is a sequence identifier used for compare-and-swap
type ConfigVersion int
var ErrConfigComparisonFailed = errors.New("comparison with existing config failed during save")
var ErrEndOfBuildEventStream = errors.New("end of build event stream")
var ErrBuildEventStreamClosed = errors.New("build event stream closed")
//go:generate counterfeiter . EventSource
type EventSource interface {
Next() (event.Envelope, error)
Close() error
}
type BuildInput struct {
Name string
VersionedResource
FirstOccurrence bool
}
type BuildOutput struct {
VersionedResource
}
type SavedWorker struct {
WorkerInfo
TeamName string
ExpiresIn time.Duration
}
type WorkerInfo struct {
GardenAddr string
BaggageclaimURL string
HTTPProxyURL string
HTTPSProxyURL string
NoProxy string
ActiveContainers int
ResourceTypes []atc.WorkerResourceType
Platform string
Tags []string
TeamID int
Name string
StartTime int64
}