Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2da9761

Browse files
committed
add comments for database
1 parent e69e0de commit 2da9761

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

database/driver.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
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.
15
package database
26

37
import (
@@ -8,35 +12,55 @@ import (
812
)
913

1014
var (
11-
ErrLocked = fmt.Errorf("unable to acquire lock")
15+
ErrLocked = fmt.Errorf("can't acquire lock")
1216
)
1317

1418
const NilVersion int = -1
1519

1620
var driversMu sync.RWMutex
1721
var drivers = make(map[string]Driver)
1822

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.
1930
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.
2034
Open(url string) (Driver, error)
2135

36+
// Close closes the underlying database instance managed by the driver.
37+
// Migrate will call this function only once per instance.
2238
Close() error
2339

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.
2443
Lock() error
2544

45+
// Unlock should release the lock. Migrate will call this function after
46+
// all migrations have been run.
2647
Unlock() error
2748

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.
3052
Run(version int, migration io.Reader) error
3153

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).
3557
Version() (int, error)
3658

59+
// Drop deletes everyting in the database.
3760
Drop() error
3861
}
3962

63+
// Open returns a new driver instance.
4064
func Open(url string) (Driver, error) {
4165
u, err := nurl.Parse(url)
4266
if err != nil {
@@ -57,6 +81,7 @@ func Open(url string) (Driver, error) {
5781
return d.Open(url)
5882
}
5983

84+
// Register globally registers a driver.
6085
func Register(name string, driver Driver) {
6186
driversMu.Lock()
6287
defer driversMu.Unlock()

database/driver_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package database
2+
3+
func ExampleDriver() {
4+
// see database/stub for an example
5+
6+
// database/stub/stub.go has the driver implementation
7+
// database/stub/stub_test.go runs database/testing/test.go:Test
8+
}

0 commit comments

Comments
 (0)