/
snapshot_store.go
161 lines (135 loc) · 4.55 KB
/
snapshot_store.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
package s3pg
import (
"time"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/kotsadm/pkg/persistence"
snapshottypes "github.com/replicatedhq/kots/pkg/api/snapshot/types"
"github.com/replicatedhq/kots/pkg/logger"
"go.uber.org/zap"
)
func (c S3PGStore) ListPendingScheduledSnapshots(appID string) ([]snapshottypes.ScheduledSnapshot, error) {
logger.Debug("Listing pending scheduled snapshots",
zap.String("appID", appID))
db := persistence.MustGetPGSession()
query := `SELECT id, app_id, scheduled_timestamp FROM scheduled_snapshots WHERE app_id = $1 AND backup_name IS NULL;`
rows, err := db.Query(query, appID)
if err != nil {
return nil, errors.Wrap(err, "failed to query")
}
defer rows.Close()
scheduledSnapshots := []snapshottypes.ScheduledSnapshot{}
for rows.Next() {
s := snapshottypes.ScheduledSnapshot{}
if err := rows.Scan(&s.ID, &s.AppID, &s.ScheduledTimestamp); err != nil {
return nil, errors.Wrap(err, "failed to scan")
}
scheduledSnapshots = append(scheduledSnapshots, s)
}
return scheduledSnapshots, nil
}
func (c S3PGStore) UpdateScheduledSnapshot(snapshotID string, backupName string) error {
logger.Debug("Updating scheduled snapshot",
zap.String("ID", snapshotID))
db := persistence.MustGetPGSession()
query := `UPDATE scheduled_snapshots SET backup_name = $1 WHERE id = $2`
_, err := db.Exec(query, backupName, snapshotID)
if err != nil {
return errors.Wrap(err, "failed to exec")
}
return nil
}
func (c S3PGStore) DeletePendingScheduledSnapshots(appID string) error {
logger.Debug("Deleting pending scheduled snapshots",
zap.String("appID", appID))
db := persistence.MustGetPGSession()
query := `DELETE FROM scheduled_snapshots WHERE app_id = $1 AND backup_name IS NULL`
_, err := db.Exec(query, appID)
if err != nil {
return errors.Wrap(err, "failed to db exec query")
}
return nil
}
func (c S3PGStore) CreateScheduledSnapshot(id string, appID string, timestamp time.Time) error {
logger.Debug("Creating scheduled snapshot",
zap.String("appID", appID))
db := persistence.MustGetPGSession()
query := `
INSERT INTO scheduled_snapshots (
id,
app_id,
scheduled_timestamp
) VALUES (
$1,
$2,
$3
)
`
_, err := db.Exec(query, id, appID, timestamp)
if err != nil {
return errors.Wrap(err, "Failed to db exec query")
}
return nil
}
func (c S3PGStore) ListPendingScheduledInstanceSnapshots(clusterID string) ([]snapshottypes.ScheduledInstanceSnapshot, error) {
logger.Debug("Listing pending scheduled instance snapshots",
zap.String("clusterID", clusterID))
db := persistence.MustGetPGSession()
query := `SELECT id, cluster_id, scheduled_timestamp FROM scheduled_instance_snapshots WHERE cluster_id = $1 AND backup_name IS NULL;`
rows, err := db.Query(query, clusterID)
if err != nil {
return nil, errors.Wrap(err, "failed to query")
}
defer rows.Close()
scheduledSnapshots := []snapshottypes.ScheduledInstanceSnapshot{}
for rows.Next() {
s := snapshottypes.ScheduledInstanceSnapshot{}
if err := rows.Scan(&s.ID, &s.ClusterID, &s.ScheduledTimestamp); err != nil {
return nil, errors.Wrap(err, "failed to scan")
}
scheduledSnapshots = append(scheduledSnapshots, s)
}
return scheduledSnapshots, nil
}
func (c S3PGStore) UpdateScheduledInstanceSnapshot(snapshotID string, backupName string) error {
logger.Debug("Updating scheduled instance snapshot",
zap.String("ID", snapshotID))
db := persistence.MustGetPGSession()
query := `UPDATE scheduled_instance_snapshots SET backup_name = $1 WHERE id = $2`
_, err := db.Exec(query, backupName, snapshotID)
if err != nil {
return errors.Wrap(err, "failed to exec")
}
return nil
}
func (c S3PGStore) DeletePendingScheduledInstanceSnapshots(clusterID string) error {
logger.Debug("Deleting pending scheduled instance snapshots",
zap.String("clusterID", clusterID))
db := persistence.MustGetPGSession()
query := `DELETE FROM scheduled_instance_snapshots WHERE cluster_id = $1 AND backup_name IS NULL`
_, err := db.Exec(query, clusterID)
if err != nil {
return errors.Wrap(err, "failed to db exec query")
}
return nil
}
func (c S3PGStore) CreateScheduledInstanceSnapshot(id string, clusterID string, timestamp time.Time) error {
logger.Debug("Creating scheduled instance snapshot",
zap.String("clusterID", clusterID))
db := persistence.MustGetPGSession()
query := `
INSERT INTO scheduled_instance_snapshots (
id,
cluster_id,
scheduled_timestamp
) VALUES (
$1,
$2,
$3
)
`
_, err := db.Exec(query, id, clusterID, timestamp)
if err != nil {
return errors.Wrap(err, "Failed to db exec query")
}
return nil
}