forked from db-journey/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
69 lines (56 loc) · 1.91 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
// Package driver holds the driver interface.
package driver
import (
"fmt"
neturl "net/url" // alias to allow `url string` func signature in New
"github.com/gemnasium/migrate/file"
)
// Driver is the interface type that needs to implemented by all drivers.
type Driver interface {
// Initialize is the first function to be called.
// Check the url string and open and verify any connection
// that has to be made.
Initialize(url string) error
// Close is the last function to be called.
// Close any open connection here.
Close() error
// FilenameExtension returns the extension of the migration files.
// The returned string must not begin with a dot.
FilenameExtension() string
// Migrate is the heart of the driver.
// It will receive a file which the driver should apply
// to its backend or whatever. The migration function should use
// the pipe channel to return any errors or other useful information.
Migrate(file file.File, pipe chan interface{})
// Version returns the current migration version.
Version() (file.Version, error)
// Versions returns the list of applied migrations.
Versions() (file.Versions, error)
}
// New returns Driver and calls Initialize on it.
func New(url string) (Driver, error) {
u, err := neturl.Parse(url)
if err != nil {
return nil, err
}
d := GetDriver(u.Scheme)
if d == nil {
return nil, fmt.Errorf("Driver '%s' not found.", u.Scheme)
}
verifyFilenameExtension(u.Scheme, d)
if err := d.Initialize(url); err != nil {
return nil, err
}
return d, nil
}
// verifyFilenameExtension panics if the driver's filename extension
// is not correct or empty.
func verifyFilenameExtension(driverName string, d Driver) {
f := d.FilenameExtension()
if f == "" {
panic(fmt.Sprintf("%s.FilenameExtension() returns empty string.", driverName))
}
if f[0:1] == "." {
panic(fmt.Sprintf("%s.FilenameExtension() returned string must not start with a dot.", driverName))
}
}