migrate/driver/driver.go

55 lines
1.3 KiB
Go
Raw Normal View History

2014-08-11 01:42:57 +00:00
package driver
import (
"errors"
"fmt"
2014-08-12 01:20:28 +00:00
"github.com/mattes/migrate/driver/bash"
2014-08-11 01:42:57 +00:00
"github.com/mattes/migrate/driver/postgres"
"github.com/mattes/migrate/file"
neturl "net/url" // alias to allow `url string` func signature in New
)
type Driver interface {
Initialize(url string) error
FilenameExtension() string
2014-08-11 22:58:30 +00:00
Migrate(files file.Files, pipe chan interface{})
2014-08-11 01:42:57 +00:00
Version() (uint64, error)
}
// InitDriver returns Driver and initializes it
func New(url string) (Driver, error) {
u, err := neturl.Parse(url)
if err != nil {
return nil, err
}
switch u.Scheme {
case "postgres":
d := &postgres.Driver{}
verifyFilenameExtension("postgres", d)
if err := d.Initialize(url); err != nil {
return nil, err
}
return d, nil
2014-08-12 01:20:28 +00:00
case "bash":
d := &bash.Driver{}
verifyFilenameExtension("bash", d)
if err := d.Initialize(url); err != nil {
return nil, err
}
return d, nil
2014-08-11 01:42:57 +00:00
default:
return nil, errors.New(fmt.Sprintf("Driver '%s' not found.", u.Scheme))
}
}
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))
}
}