-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.go
73 lines (66 loc) · 1.84 KB
/
driver.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
package dbmate
import (
"database/sql"
"fmt"
"net/url"
"time"
)
// Driver provides top level database functions
type Driver interface {
Open(*url.URL) (*sql.DB, error)
DatabaseExists(*url.URL) (bool, error)
CreateDatabase(*url.URL) error
DropDatabase(*url.URL) error
CreateMigrationsTable(*sql.DB) error
SelectMigrations(*sql.DB, int, string) (map[string]bool, error)
InsertMigration(Transaction, string, string) error
DeleteMigration(Transaction, string) error
Lock(*sql.DB) error
Unlock(*sql.DB)
}
// Transaction can represent a database or open transaction
type Transaction interface {
Exec(query string, args ...interface{}) (sql.Result, error)
}
// RunInLock will execute a function in the context of the driver's advisory lock
func RunInLock(driver Driver, sqlDB *sql.DB, timeoutSecs int, lockFunc func(Driver, *sql.DB) error) error {
lockChan := make(chan string, 1)
errChan := make(chan error, 1)
go func() {
if err := driver.Lock(sqlDB); err != nil {
errChan <- err
} else {
lockChan <- "acquired"
}
}()
select {
case <-lockChan:
defer driver.Unlock(sqlDB)
return lockFunc(driver, sqlDB)
case err := <-errChan:
return err
case <-time.After(time.Second * time.Duration(timeoutSecs)):
return fmt.Errorf("Timeout waiting for database migration lock (waited %v seconds)", timeoutSecs)
}
}
// GetDriver loads a database driver by name
func GetDriver(name string) (Driver, error) {
switch name {
case "mysql":
return MySQLDriver{}, nil
case "postgres", "postgresql":
return PostgresDriver{}, nil
case "sqlite", "sqlite3":
return SQLiteDriver{}, nil
default:
return nil, fmt.Errorf("unknown driver: %s", name)
}
}
// GetDriverOpen is a shortcut for GetDriver(u.Scheme).Open(u)
func GetDriverOpen(u *url.URL) (*sql.DB, error) {
drv, err := GetDriver(u.Scheme)
if err != nil {
return nil, err
}
return drv.Open(u)
}