2014-08-13 00:38:29 +00:00
|
|
|
// Package driver holds the driver interface.
|
2014-08-11 01:42:57 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-27 01:46:41 +00:00
|
|
|
neturl "net/url" // alias to allow `url string` func signature in New
|
|
|
|
|
2014-08-11 01:42:57 +00:00
|
|
|
"github.com/mattes/migrate/file"
|
|
|
|
)
|
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
// Driver is the interface type that needs to implemented by all drivers.
|
2014-08-11 01:42:57 +00:00
|
|
|
type Driver interface {
|
2014-08-13 00:38:29 +00:00
|
|
|
|
|
|
|
// Initialize is the first function to be called.
|
|
|
|
// Check the url string and open and verify any connection
|
|
|
|
// that has to be made.
|
2014-08-11 01:42:57 +00:00
|
|
|
Initialize(url string) error
|
2014-08-13 00:38:29 +00:00
|
|
|
|
2014-08-25 15:44:45 +00:00
|
|
|
// Close is the last function to be called.
|
|
|
|
// Close any open connection here.
|
|
|
|
Close() error
|
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
// FilenameExtension returns the extension of the migration files.
|
|
|
|
// The returned string must not begin with a dot.
|
2014-08-11 01:42:57 +00:00
|
|
|
FilenameExtension() string
|
2014-08-13 00:38:29 +00:00
|
|
|
|
|
|
|
// Migrate is the heart of the driver.
|
2014-08-13 18:06:38 +00:00
|
|
|
// It will receive a file which the driver should apply
|
2014-08-13 00:38:29 +00:00
|
|
|
// to its backend or whatever. The migration function should use
|
|
|
|
// the pipe channel to return any errors or other useful information.
|
2014-08-13 18:06:38 +00:00
|
|
|
Migrate(file file.File, pipe chan interface{})
|
2014-08-13 00:38:29 +00:00
|
|
|
|
|
|
|
// Version returns the current migration version.
|
2014-08-11 01:42:57 +00:00
|
|
|
Version() (uint64, error)
|
|
|
|
}
|
|
|
|
|
2014-08-13 00:38:29 +00:00
|
|
|
// New returns Driver and calls Initialize on it
|
2014-08-11 01:42:57 +00:00
|
|
|
func New(url string) (Driver, error) {
|
|
|
|
u, err := neturl.Parse(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-22 20:29:26 +00:00
|
|
|
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
|
2014-08-11 01:42:57 +00:00
|
|
|
}
|
2015-10-22 20:29:26 +00:00
|
|
|
|
|
|
|
return d, nil
|
2014-08-11 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-11 10:11:28 +00:00
|
|
|
// verifyFilenameExtension panics if the driver's filename extension
|
2014-08-13 00:38:29 +00:00
|
|
|
// is not correct or empty.
|
2014-08-11 01:42:57 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|