-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_sql.go
305 lines (266 loc) · 8.69 KB
/
driver_sql.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package adapt
import (
"context"
"database/sql"
"log/slog"
"time"
)
// SqlStatementsDriver is a special interface for defining a DatabaseDriver that
// only wants to use there database specific dialect. A SqlStatementsDriver can
// be converted into a full DatabaseDriver using a provided adapter, that can be
// accessed using FromSqlStatementsDriver. Basically a SqlStatementsDriver only
// reports it's features and (query, args) pairs for the different needed
// operations that depend on the underlying SQL dialect.
//
// The big advantage of SqlStatementsDriver is that it reduces boilerplate and
// error checking drastically for all primitive DatabaseDriver that support
// sql.DB.
type SqlStatementsDriver interface {
// Name reports the name of this Driver. It is mainly used for logging
// output and can be an empty string.
Name() string
// Init initializes the internal state of a driver. It should be used to
// apply options or return an error which the internal state is invalid.
// For tasks like establishing connections and performing health checks
// Healthy should be used.
Init(log *slog.Logger) error
// Healthy should report if everything is ready and healthy to proceed
// with running migrations. One example would be to ping the database
// and check if the connection is intact. Also Healthy is responsible for
// creating the structure of your meta-storage, e.g. the database and
// meta-table.
Healthy() error
// SupportsLocks reports whether the driver supports locking or not. This
// influences if AcquireLock and ReleaseLock are called.
SupportsLocks() bool
// AcquireLock must return a database query that acquires an exclusive
// lock.
AcquireLock() (query string)
// ReleaseLock must return a database query that released the previously
// acquired lock.
ReleaseLock() (query string)
// ListMigrations must return a database query that selects all Migration
// data in the following order: ID, Executor, Started, Finished, Hash, Adapt
// Deployment, DeploymentOrder, Down. The field's types are the same as in the
// Migration struct.
ListMigrations() (query string)
// AddMigration must return a database query and it's corresponding args
// that insert the passed Migration into the meta-table.
AddMigration(m *Migration) (query string, args []interface{})
// SetMigrationToFinished must return a database query and it's corresponding
// args in order to set the migration with migrationID to finished.
SetMigrationToFinished(migrationID string) (query string, args []interface{})
// Close should close all underlying connections opened during Healthy and
// perform any necessary clean-up operations. Close is always called, even
// when an error is encountered somewhere or the library panics
Close() error
// DB should return the database connection that gets used to execute
// sql statements
DB() *sql.DB
// SupportsTx reports whether the driver supports database transactions.
// If SupportsTx is true and ParsedMigration wants transactions to be used
// all migration statements will be executed within a single transaction.
SupportsTx() bool
// TxBeginOpts provides the transaction begin options that should be used
// when adapt starts a new transaction.
TxBeginOpts() (ctx context.Context, opts *sql.TxOptions)
// UseGlobalTx instructs the adapter to start a single global transaction
// for all database queries/executes. When used the transaction is started
// during Init and committed/rollbacked during Close.
UseGlobalTx() bool
// DeleteMigration must return a database query and it's corresponding args
// in order to delete the specified migration.
DeleteMigration(migrationID string) (query string, args []interface{})
}
// FromSqlStatementsDriver converts a SqlStatementsDriver to a full DatabaseDriver
// by wrapping it in an internal adapter that handles all sql.DB operations
// according to the features specified by SqlStatementsDriver
func FromSqlStatementsDriver(driver SqlStatementsDriver) DatabaseDriver {
return &stmtDriver{
driver: driver,
}
}
type stmtDriver struct {
driver SqlStatementsDriver
log *slog.Logger
target DBTarget
tx *sql.Tx
rollback bool
}
func (d *stmtDriver) Name() string {
return d.driver.Name()
}
func (d *stmtDriver) Init(log *slog.Logger) error {
d.log = log
err := d.driver.Init(log)
if err != nil {
return err
}
if d.driver.SupportsTx() && d.driver.UseGlobalTx() {
log.Debug("driver supports tx and instructs us to use a global tx. Beginning global tx")
ctx, opts := d.driver.TxBeginOpts()
tx, err := d.driver.DB().BeginTx(ctx, opts)
if err != nil {
log.Error("unable to start tx", "error", err)
return err
}
log.Info("using global tx as database target")
d.target = tx
d.tx = tx
} else {
d.target = d.driver.DB()
}
return nil
}
func (d *stmtDriver) Healthy() error {
return d.driver.Healthy()
}
func (d *stmtDriver) SupportsLocks() bool {
return d.driver.SupportsLocks()
}
func (d *stmtDriver) AcquireLock() error {
var err error
if query := d.driver.AcquireLock(); len(query) > 0 {
_, err = d.target.Exec(query)
if err != nil {
d.rollback = true
}
}
return err
}
func (d *stmtDriver) ReleaseLock() error {
var err error
if query := d.driver.ReleaseLock(); len(query) > 0 {
_, err = d.target.Exec(query)
if err != nil {
d.rollback = true
}
}
return err
}
func (d *stmtDriver) ListMigrations() ([]*Migration, error) {
var migrations []*Migration
rows, err := d.target.Query(d.driver.ListMigrations())
if err != nil {
return nil, err
}
defer func() {
_ = rows.Close()
}()
for rows.Next() {
var id, executor, adapt, deployment string
var deploymentOrder int
var started time.Time
var finished sql.NullTime
var hash sql.NullString
var down *[]byte
err = rows.Scan(&id, &executor, &started, &finished, &hash, &adapt, &deployment, &deploymentOrder, &down)
if err != nil {
return nil, err
}
m := &Migration{
ID: id,
Executor: executor,
Started: started,
Adapt: adapt,
Deployment: deployment,
DeploymentOrder: deploymentOrder,
Down: down,
}
if finished.Valid && finished.Time.Year() > 1 {
m.Finished = &(finished.Time)
}
if hash.Valid {
m.Hash = &(hash.String)
}
migrations = append(migrations, m)
}
err = rows.Err()
if err != nil {
d.rollback = true
return nil, err
}
return migrations, nil
}
func (d *stmtDriver) AddMigration(m *Migration) error {
query, args := d.driver.AddMigration(m)
_, err := d.target.Exec(query, args...)
if err != nil {
d.rollback = true
}
return err
}
func (d *stmtDriver) Migrate(migration *ParsedMigration, beforeFinish func(target DBTarget) error) error {
for _, s := range migration.Stmts {
d.log.Debug("executing statement", "statement", s)
started := time.Now()
if _, err := d.target.Exec(s); err != nil {
d.log.Error("failed executing statement", "statement", s, "error", err)
d.rollback = true
return err
}
end := time.Now()
d.log.Debug("executing statement took", "duration", end.Sub(started))
}
if beforeFinish != nil {
d.log.Debug("beforeFinishCallback is provided. calling so cleanup can be performed within the (eventually running) same transaction")
err := beforeFinish(d.target)
if err != nil {
d.log.Error("beforeFinishCallback failed", "error", err)
d.rollback = true
return err
} else {
d.log.Debug("beforeFinishCallback successful")
}
}
return nil
}
func (d *stmtDriver) SetMigrationToFinished(migrationID string) error {
query, args := d.driver.SetMigrationToFinished(migrationID)
_, err := d.target.Exec(query, args...)
if err != nil {
d.rollback = true
}
return err
}
func (d *stmtDriver) Close() error {
// if tx is not nil, we started a tx and need to commit/rollback it
if d.tx != nil {
d.log.Debug("ending global tx")
if d.rollback {
d.log.Debug("rollback of global tx")
err := d.tx.Rollback()
if err != nil {
d.log.Error("rollback of global tx failed", "error", err)
} else {
d.log.Info("rollback of global tx succeeded")
}
} else {
d.log.Debug("commit of global tx")
err := d.tx.Commit()
if err != nil {
d.log.Error("commit of global tx failed", "error", err)
} else {
d.log.Info("commit of global tx succeeded")
}
}
}
return d.driver.Close()
}
func (d *stmtDriver) DB() *sql.DB {
return d.driver.DB()
}
func (d *stmtDriver) SupportsTx() bool {
return d.driver.SupportsTx()
}
func (d *stmtDriver) TxBeginOpts() (ctx context.Context, opts *sql.TxOptions) {
return d.driver.TxBeginOpts()
}
func (d *stmtDriver) DeleteMigration(migrationID string, target DBTarget) error {
query, args := d.driver.DeleteMigration(migrationID)
_, err := target.Exec(query, args...)
if err != nil {
d.rollback = true
}
return err
}