2017-02-10 03:04:54 +00:00
|
|
|
// Package database provides the Database interface.
|
|
|
|
// All database drivers must implement this interface, register themselves,
|
|
|
|
// optionally provide a `WithInstance` function and pass the tests
|
|
|
|
// in package database/testing.
|
2017-02-08 06:01:29 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
nurl "net/url"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-10 03:04:54 +00:00
|
|
|
ErrLocked = fmt.Errorf("can't acquire lock")
|
2017-02-08 06:01:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const NilVersion int = -1
|
|
|
|
|
|
|
|
var driversMu sync.RWMutex
|
|
|
|
var drivers = make(map[string]Driver)
|
|
|
|
|
2017-02-11 19:13:27 +00:00
|
|
|
// Driver is the interface every database driver must implement.
|
2017-02-10 03:04:54 +00:00
|
|
|
//
|
2017-02-11 19:13:27 +00:00
|
|
|
// How to implement a database driver?
|
|
|
|
// 1. Implement this interface.
|
|
|
|
// 2. Optionally, add a function named `WithInstance`.
|
|
|
|
// This function should accept an existing DB instance and a Config{} struct
|
|
|
|
// and return a driver instance.
|
|
|
|
// 3. Add a test that calls database/testing.go:Test()
|
|
|
|
// 4. Add own tests for Open(), WithInstance() (when provided) and Close().
|
|
|
|
// All other functions are tested by tests in database/testing.
|
|
|
|
// Saves you some time and makes sure all database drivers behave the same way.
|
|
|
|
// 5. Call Register in init().
|
|
|
|
//
|
|
|
|
// Guidelines:
|
|
|
|
// * Don't try to correct user input. Don't assume things.
|
|
|
|
// When in doubt, return an error and explain the situation to the user.
|
|
|
|
// * All configuration input must come from the URL string in func Open()
|
|
|
|
// or the Config{} struct in WithInstance. Don't os.Getenv().
|
2017-02-08 06:01:29 +00:00
|
|
|
type Driver interface {
|
2017-02-10 03:04:54 +00:00
|
|
|
// Open returns a new driver instance configured with parameters
|
|
|
|
// coming from the URL string. Migrate will call this function
|
|
|
|
// only once per instance.
|
2017-02-08 06:01:29 +00:00
|
|
|
Open(url string) (Driver, error)
|
|
|
|
|
2017-02-10 03:04:54 +00:00
|
|
|
// Close closes the underlying database instance managed by the driver.
|
|
|
|
// Migrate will call this function only once per instance.
|
2017-02-08 06:01:29 +00:00
|
|
|
Close() error
|
|
|
|
|
2017-02-10 03:04:54 +00:00
|
|
|
// Lock should acquire a database lock so that only one migration process
|
|
|
|
// can run at a time. Migrate will call this function before Run is called.
|
|
|
|
// If the implementation can't provide this functionality, return nil.
|
2017-02-15 07:12:16 +00:00
|
|
|
// Return database.ErrLocked if database is already locked.
|
2017-02-08 06:01:29 +00:00
|
|
|
Lock() error
|
|
|
|
|
2017-02-10 03:04:54 +00:00
|
|
|
// Unlock should release the lock. Migrate will call this function after
|
|
|
|
// all migrations have been run.
|
2017-02-08 06:01:29 +00:00
|
|
|
Unlock() error
|
|
|
|
|
2017-02-10 03:04:54 +00:00
|
|
|
// Run applies a migration to the database. Run the migration and store
|
|
|
|
// the version. migration can be nil. In that case, just store the version.
|
|
|
|
// When version -1 is given, the state should be as if no migration had been run.
|
2017-02-08 06:01:29 +00:00
|
|
|
Run(version int, migration io.Reader) error
|
|
|
|
|
2017-02-10 03:04:54 +00:00
|
|
|
// Version returns the currently active version.
|
|
|
|
// When no migration has been run yet, it must return -1.
|
|
|
|
// If the returned version is < -1 it will panic (in the test).
|
2017-02-08 06:01:29 +00:00
|
|
|
Version() (int, error)
|
|
|
|
|
2017-02-10 03:04:54 +00:00
|
|
|
// Drop deletes everyting in the database.
|
2017-02-08 06:01:29 +00:00
|
|
|
Drop() error
|
|
|
|
}
|
|
|
|
|
2017-02-10 03:04:54 +00:00
|
|
|
// Open returns a new driver instance.
|
2017-02-08 06:01:29 +00:00
|
|
|
func Open(url string) (Driver, error) {
|
|
|
|
u, err := nurl.Parse(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Scheme == "" {
|
|
|
|
return nil, fmt.Errorf("database driver: invalid URL scheme")
|
|
|
|
}
|
|
|
|
|
|
|
|
driversMu.RLock()
|
|
|
|
d, ok := drivers[u.Scheme]
|
|
|
|
driversMu.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("database driver: unknown driver %v (forgotton import?)", u.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.Open(url)
|
|
|
|
}
|
|
|
|
|
2017-02-10 03:04:54 +00:00
|
|
|
// Register globally registers a driver.
|
2017-02-08 06:01:29 +00:00
|
|
|
func Register(name string, driver Driver) {
|
|
|
|
driversMu.Lock()
|
|
|
|
defer driversMu.Unlock()
|
|
|
|
if driver == nil {
|
|
|
|
panic("Register driver is nil")
|
|
|
|
}
|
|
|
|
if _, dup := drivers[name]; dup {
|
|
|
|
panic("Register called twice for driver " + name)
|
|
|
|
}
|
|
|
|
drivers[name] = driver
|
|
|
|
}
|