mirror of https://github.com/status-im/migrate.git
Merge pull request #54 from gemnasium/dont-load-all-drivers-43
Don't load all drivers
This commit is contained in:
commit
2a2bc4fec8
20
.travis.yml
20
.travis.yml
|
@ -1,21 +1,17 @@
|
|||
language: go
|
||||
sudo: required
|
||||
|
||||
go:
|
||||
- 1.3
|
||||
- 1.4
|
||||
- 1.5
|
||||
- tip
|
||||
|
||||
addons:
|
||||
postgresql: "9.3"
|
||||
|
||||
services:
|
||||
- cassandra
|
||||
- docker
|
||||
|
||||
before_script:
|
||||
- >
|
||||
/usr/local/cassandra/bin/cqlsh -e "CREATE KEYSPACE migratetest WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor' : 1};"
|
||||
- psql -c 'create database migratetest;' -U postgres
|
||||
- mysql -e 'create database migratetest;'
|
||||
before_install:
|
||||
- curl -L https://github.com/docker/compose/releases/download/1.4.2/docker-compose-`uname -s`-`uname -m` > docker-compose
|
||||
- chmod +x docker-compose
|
||||
- sudo mv docker-compose /usr/local/bin
|
||||
- sed -i -e 's/golang/golang:'"$TRAVIS_GO_VERSION"'/' docker-compose.yml
|
||||
|
||||
script: go test -p 1 ./...
|
||||
script: make test
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
FROM scratch
|
||||
ADD migrate /migrate
|
||||
ENTRYPOINT ["/migrate"]
|
|
@ -0,0 +1,22 @@
|
|||
IMAGE=mattes/migrate
|
||||
DCR=docker-compose run --rm
|
||||
.PHONY: clean test build release docker-build docker-push run
|
||||
|
||||
all: release
|
||||
|
||||
clean:
|
||||
rm -f migrate
|
||||
|
||||
test:
|
||||
$(DCR) go-test
|
||||
|
||||
build:
|
||||
$(DCR) go-build
|
||||
|
||||
release: test build docker-build docker-push
|
||||
|
||||
docker-build:
|
||||
docker build --rm -t $(IMAGE) .
|
||||
|
||||
docker-push:
|
||||
docker push $(IMAGE)
|
|
@ -78,6 +78,9 @@ See GoDoc here: http://godoc.org/github.com/mattes/migrate/migrate
|
|||
```go
|
||||
import "github.com/mattes/migrate/migrate"
|
||||
|
||||
// Import any required drivers so that they are registered and available
|
||||
import _ "github.com/mattes/migrate/drivers/mysql"
|
||||
|
||||
// use synchronous versions of migration functions ...
|
||||
allErrors, ok := migrate.UpSync("driver://url", "./path")
|
||||
if !ok {
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
go: &go
|
||||
image: golang
|
||||
working_dir: /go/src/github.com/mattes/migrate
|
||||
volumes:
|
||||
- $GOPATH:/go
|
||||
go-test:
|
||||
<<: *go
|
||||
command: sh -c 'go get -t -v ./... && go test -v ./...'
|
||||
links:
|
||||
- postgres
|
||||
- mysql
|
||||
- cassandra
|
||||
go-build:
|
||||
<<: *go
|
||||
command: sh -c 'go get -v && go build -ldflags ''-s'' -o migrater'
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
postgres:
|
||||
image: postgres
|
||||
mysql:
|
||||
image: mysql
|
||||
environment:
|
||||
MYSQL_DATABASE: migratetest
|
||||
MYSQL_ALLOW_EMPTY_PASSWORD: yes
|
||||
cassandra:
|
||||
image: cassandra
|
|
@ -2,8 +2,8 @@
|
|||
package bash
|
||||
|
||||
import (
|
||||
"github.com/mattes/migrate/driver"
|
||||
"github.com/mattes/migrate/file"
|
||||
_ "github.com/mattes/migrate/migrate/direction"
|
||||
)
|
||||
|
||||
type Driver struct {
|
||||
|
@ -30,3 +30,7 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
|
|||
func (driver *Driver) Version() (uint64, error) {
|
||||
return uint64(0), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
driver.RegisterDriver("bash", &Driver{})
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package bash
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
import "testing"
|
||||
|
||||
func TestMigrate(t *testing.T) {
|
||||
|
||||
|
|
|
@ -3,12 +3,14 @@ package cassandra
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gocql/gocql"
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gocql/gocql"
|
||||
"github.com/mattes/migrate/driver"
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
)
|
||||
|
||||
type Driver struct {
|
||||
|
@ -153,3 +155,7 @@ func (driver *Driver) Version() (uint64, error) {
|
|||
err := driver.session.Query("SELECT version FROM "+tableName+" WHERE versionRow = ?", versionRow).Scan(&version)
|
||||
return uint64(version) - 1, err
|
||||
}
|
||||
|
||||
func init() {
|
||||
driver.RegisterDriver("cassandra", &Driver{})
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package cassandra
|
|||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -13,7 +14,10 @@ import (
|
|||
|
||||
func TestMigrate(t *testing.T) {
|
||||
var session *gocql.Session
|
||||
driverUrl := "cassandra://localhost/migratetest"
|
||||
|
||||
host := os.Getenv("CASSANDRA_PORT_9042_TCP_ADDR")
|
||||
port := os.Getenv("CASSANDRA_PORT_9042_TCP_PORT")
|
||||
driverUrl := "cassandra://" + host + ":" + port + "/system"
|
||||
|
||||
// prepare a clean test database
|
||||
u, err := url.Parse(driverUrl)
|
||||
|
@ -32,12 +36,12 @@ func TestMigrate(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := session.Query(`DROP TABLE IF EXISTS yolo`).Exec(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := session.Query(`DROP TABLE IF EXISTS ` + tableName).Exec(); err != nil {
|
||||
if err := session.Query(`CREATE KEYSPACE IF NOT EXISTS migrate WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1};`).Exec(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cluster.Keyspace = "migrate"
|
||||
session, err = cluster.CreateSession()
|
||||
driverUrl = "cassandra://" + host + ":" + port + "/migrate"
|
||||
|
||||
d := &Driver{}
|
||||
if err := d.Initialize(driverUrl); err != nil {
|
||||
|
|
|
@ -5,22 +5,9 @@ import (
|
|||
"fmt"
|
||||
neturl "net/url" // alias to allow `url string` func signature in New
|
||||
|
||||
"github.com/mattes/migrate/driver/bash"
|
||||
"github.com/mattes/migrate/driver/cassandra"
|
||||
"github.com/mattes/migrate/driver/mysql"
|
||||
"github.com/mattes/migrate/driver/postgres"
|
||||
"github.com/mattes/migrate/driver/sqlite3"
|
||||
"github.com/mattes/migrate/file"
|
||||
)
|
||||
|
||||
var driverMap = map[string]Driver{
|
||||
"postgres": &postgres.Driver{},
|
||||
"mysql": &mysql.Driver{},
|
||||
"bash": &bash.Driver{},
|
||||
"cassandra": &cassandra.Driver{},
|
||||
"sqlite3": &sqlite3.Driver{},
|
||||
}
|
||||
|
||||
// Driver is the interface type that needs to implemented by all drivers.
|
||||
type Driver interface {
|
||||
|
||||
|
@ -54,17 +41,19 @@ func New(url string) (Driver, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if d, found := driverMap[u.Scheme]; found {
|
||||
verifyFilenameExtension(u.Scheme, d)
|
||||
if err := d.Initialize(url); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return d, nil
|
||||
d := GetDriver(u.Scheme)
|
||||
if d == nil {
|
||||
return nil, fmt.Errorf("Driver '%s' not found.", u.Scheme)
|
||||
}
|
||||
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 drivers filename extension
|
||||
// verifyFilenameExtension panics if the driver's filename extension
|
||||
// is not correct or empty.
|
||||
func verifyFilenameExtension(driverName string, d Driver) {
|
||||
f := d.FilenameExtension()
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
package driver
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
if _, err := New("unknown://url"); err == nil {
|
||||
t.Error("no error although driver unknown")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewBash(t *testing.T) {
|
||||
driver, err := New("bash://url")
|
||||
if err != nil {
|
||||
t.Error("error although bash driver known")
|
||||
}
|
||||
version, err := driver.Version()
|
||||
if version != 0 {
|
||||
t.Errorf("expected bash driver version to be 0, got %d\n", version)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSqlite3(t *testing.T) {
|
||||
_, err := New("sqlite3://test.db")
|
||||
if err != nil {
|
||||
t.Error("error although sqlite3 driver known")
|
||||
}
|
||||
}
|
|
@ -7,12 +7,14 @@ import (
|
|||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/mattes/migrate/driver"
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
)
|
||||
|
||||
type Driver struct {
|
||||
|
@ -177,3 +179,7 @@ func (driver *Driver) Version() (uint64, error) {
|
|||
return version, nil
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
driver.RegisterDriver("mysql", &Driver{})
|
||||
}
|
||||
|
|
|
@ -2,17 +2,21 @@ package mysql
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
pipep "github.com/mattes/migrate/pipe"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMigrate runs some additional tests on Migrate().
|
||||
// Basic testing is already done in migrate/migrate_test.go
|
||||
func TestMigrate(t *testing.T) {
|
||||
driverUrl := "mysql://root@tcp(127.0.0.1:3306)/migratetest"
|
||||
host := os.Getenv("MYSQL_PORT_3306_TCP_ADDR")
|
||||
port := os.Getenv("MYSQL_PORT_3306_TCP_PORT")
|
||||
driverUrl := "mysql://root@tcp(" + host + ":" + port + ")/migratetest"
|
||||
|
||||
// prepare clean database
|
||||
connection, err := sql.Open("mysql", strings.SplitN(driverUrl, "mysql://", 2)[1])
|
||||
|
|
|
@ -5,10 +5,12 @@ import (
|
|||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/lib/pq"
|
||||
"github.com/mattes/migrate/driver"
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Driver struct {
|
||||
|
@ -119,3 +121,7 @@ func (driver *Driver) Version() (uint64, error) {
|
|||
return version, nil
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
driver.RegisterDriver("postgres", &Driver{})
|
||||
}
|
||||
|
|
|
@ -2,16 +2,20 @@ package postgres
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
pipep "github.com/mattes/migrate/pipe"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMigrate runs some additional tests on Migrate().
|
||||
// Basic testing is already done in migrate/migrate_test.go
|
||||
func TestMigrate(t *testing.T) {
|
||||
driverUrl := "postgres://localhost/migratetest?sslmode=disable"
|
||||
host := os.Getenv("POSTGRES_PORT_5432_TCP_ADDR")
|
||||
port := os.Getenv("POSTGRES_PORT_5432_TCP_PORT")
|
||||
driverUrl := "postgres://postgres@" + host + ":" + port + "/template1?sslmode=disable"
|
||||
|
||||
// prepare clean database
|
||||
connection, err := sql.Open("postgres", driverUrl)
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package driver
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var driversMu sync.Mutex
|
||||
var drivers = make(map[string]Driver)
|
||||
|
||||
// Registers a driver so it can be created from its name. Drivers should
|
||||
// call this from an init() function so that they registers themselvse on
|
||||
// import
|
||||
func RegisterDriver(name string, driver Driver) {
|
||||
driversMu.Lock()
|
||||
defer driversMu.Unlock()
|
||||
if driver == nil {
|
||||
panic("driver: Register driver is nil")
|
||||
}
|
||||
if _, dup := drivers[name]; dup {
|
||||
panic("sql: Register called twice for driver " + name)
|
||||
}
|
||||
drivers[name] = driver
|
||||
}
|
||||
|
||||
// Retrieves a registered driver by name
|
||||
func GetDriver(name string) Driver {
|
||||
driversMu.Lock()
|
||||
defer driversMu.Unlock()
|
||||
driver := drivers[name]
|
||||
return driver
|
||||
}
|
||||
|
||||
// Drivers returns a sorted list of the names of the registered drivers.
|
||||
func Drivers() []string {
|
||||
driversMu.Lock()
|
||||
defer driversMu.Unlock()
|
||||
var list []string
|
||||
for name := range drivers {
|
||||
list = append(list, name)
|
||||
}
|
||||
sort.Strings(list)
|
||||
return list
|
||||
}
|
|
@ -5,10 +5,12 @@ import (
|
|||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/mattes/migrate/driver"
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Driver struct {
|
||||
|
@ -123,3 +125,7 @@ func (driver *Driver) Version() (uint64, error) {
|
|||
return version, nil
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
driver.RegisterDriver("sqlite3", &Driver{})
|
||||
}
|
||||
|
|
|
@ -2,10 +2,11 @@ package sqlite3
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"github.com/mattes/migrate/file"
|
||||
"github.com/mattes/migrate/migrate/direction"
|
||||
pipep "github.com/mattes/migrate/pipe"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMigrate runs some additional tests on Migrate()
|
||||
|
|
|
@ -2,12 +2,17 @@ package migrate
|
|||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
// Ensure imports for each driver we wish to test
|
||||
|
||||
_ "github.com/mattes/migrate/driver/postgres"
|
||||
_ "github.com/mattes/migrate/driver/sqlite3"
|
||||
)
|
||||
|
||||
// Add Driver URLs here to test basic Up, Down, .. functions.
|
||||
var driverUrls = []string{
|
||||
"postgres://localhost/migratetest?sslmode=disable",
|
||||
"postgres://postgres@" + os.Getenv("POSTGRES_PORT_5432_TCP_ADDR") + ":" + os.Getenv("POSTGRES_PORT_5432_TCP_PORT") + "/template1?sslmode=disable",
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
|
@ -54,7 +59,7 @@ func TestCreate(t *testing.T) {
|
|||
func TestReset(t *testing.T) {
|
||||
for _, driverUrl := range driverUrls {
|
||||
t.Logf("Test driver: %s", driverUrl)
|
||||
tmpdir, err := ioutil.TempDir("/tmp", "migrate-test")
|
||||
tmpdir, err := ioutil.TempDir("/", "migrate-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue