-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_db.go
55 lines (51 loc) · 2.63 KB
/
driver_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
package adapt
import (
"context"
"database/sql"
)
// DBTarget is a container for a sql execution target (either sql.DB or sql.Tx)
type DBTarget interface {
Exec(query string, args ...interface{}) (sql.Result, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
}
// DatabaseDriver is a special extension of Driver. It is always needed when
// adapt should execute a migration from a SqlStatementsSource.
type DatabaseDriver interface {
Driver
// 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)
// DeleteMigration should delete the migration from the database. It is
// important that the provided DBTarget is used, which is a container for
// the underlying execution target (either sql.DB directly or an eventually
// running sql.Tx).
DeleteMigration(migrationID string, target DBTarget) error
}
// DatabaseDriverCustomMigration extends DatabaseDriver by providing a custom
// migration callback. This can be used when the default execution strategy of
// a DatabaseDriver isn't sufficient and the Driver needs fine-grained control
// over every single executed statement. When using DatabaseDriverCustomMigration
// the Driver itself is fully responsible for starting/committing transactions
// and checking if ParsedMigrations can be executed within a transaction.
type DatabaseDriverCustomMigration interface {
DatabaseDriver
// Migrate provides a callback for fine-grained manual migrations. It is
// responsible for the full transaction-lifecycle and checking if all
// components support transactions. As long as Migrate doesn't return an
// error adapt assumes that the ParsedMigration was applied successfully
// and continues with setting the finished time in it's meta store. If
// Migrate internally starts a transaction is should call beforeFinish
// before committing the transaction. In certain situations (for example
// during Down-migrations) adapt will want to execute statements within
// the same transaction as the migration itself. If Migrate doesn't start
// it's own migration it should call beforeFinish before returning the
// function. beforeFinish is allowed to be nil.
Migrate(migration *ParsedMigration, beforeFinish func(target DBTarget) error) error
}