1
+ // Package database provides the Database interface.
2
+ // All database drivers must implement this interface, register themselves,
3
+ // optionally provide a `WithInstance` function and pass the tests
4
+ // in package database/testing.
1
5
package database
2
6
3
7
import (
@@ -8,35 +12,55 @@ import (
8
12
)
9
13
10
14
var (
11
- ErrLocked = fmt .Errorf ("unable to acquire lock" )
15
+ ErrLocked = fmt .Errorf ("can't acquire lock" )
12
16
)
13
17
14
18
const NilVersion int = - 1
15
19
16
20
var driversMu sync.RWMutex
17
21
var drivers = make (map [string ]Driver )
18
22
23
+ // Driver is an interface every driver must implement.
24
+ // The driver implementation must pass the `Test` in database/testing.
25
+ // Optionally provide a `WithInstance` function, so users can bypass `Open`
26
+ // and use an existing database instance.
27
+ //
28
+ // Implementations must not assume things nor try to correct user input.
29
+ // If in doubt, return an error.
19
30
type Driver interface {
31
+ // Open returns a new driver instance configured with parameters
32
+ // coming from the URL string. Migrate will call this function
33
+ // only once per instance.
20
34
Open (url string ) (Driver , error )
21
35
36
+ // Close closes the underlying database instance managed by the driver.
37
+ // Migrate will call this function only once per instance.
22
38
Close () error
23
39
40
+ // Lock should acquire a database lock so that only one migration process
41
+ // can run at a time. Migrate will call this function before Run is called.
42
+ // If the implementation can't provide this functionality, return nil.
24
43
Lock () error
25
44
45
+ // Unlock should release the lock. Migrate will call this function after
46
+ // all migrations have been run.
26
47
Unlock () error
27
48
28
- // when version = NilVersion, "deinitialize"
29
- // migration can be nil, in that case, just store version
49
+ // Run applies a migration to the database. Run the migration and store
50
+ // the version. migration can be nil. In that case, just store the version.
51
+ // When version -1 is given, the state should be as if no migration had been run.
30
52
Run (version int , migration io.Reader ) error
31
53
32
- // version > 0: regular version
33
- // version -1: nil version (const NilVersion)
34
- // version < -1: will panic
54
+ // Version returns the currently active version.
55
+ // When no migration has been run yet, it must return -1.
56
+ // If the returned version is < -1 it will panic (in the test).
35
57
Version () (int , error )
36
58
59
+ // Drop deletes everyting in the database.
37
60
Drop () error
38
61
}
39
62
63
+ // Open returns a new driver instance.
40
64
func Open (url string ) (Driver , error ) {
41
65
u , err := nurl .Parse (url )
42
66
if err != nil {
@@ -57,6 +81,7 @@ func Open(url string) (Driver, error) {
57
81
return d .Open (url )
58
82
}
59
83
84
+ // Register globally registers a driver.
60
85
func Register (name string , driver Driver ) {
61
86
driversMu .Lock ()
62
87
defer driversMu .Unlock ()
0 commit comments