Skip to content
This repository has been archived by the owner on Jun 28, 2018. It is now read-only.

Commit

Permalink
Merge pull request #54 from gemnasium/dont-load-all-drivers-43
Browse files Browse the repository at this point in the history
Don't load all drivers
  • Loading branch information
mattes committed Nov 11, 2015
2 parents e351ab2 + 65674ac commit 2a2bc4f
Show file tree
Hide file tree
Showing 19 changed files with 185 additions and 85 deletions.
20 changes: 8 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM scratch
ADD migrate /migrate
ENTRYPOINT ["/migrate"]
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion driver/bash/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{})
}
4 changes: 1 addition & 3 deletions driver/bash/bash_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package bash

import (
"testing"
)
import "testing"

func TestMigrate(t *testing.T) {

Expand Down
12 changes: 9 additions & 3 deletions driver/cassandra/cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{})
}
14 changes: 9 additions & 5 deletions driver/cassandra/cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cassandra

import (
"net/url"
"os"
"testing"
"time"

Expand All @@ -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)
Expand All @@ -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 {
Expand Down
31 changes: 10 additions & 21 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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()
Expand Down
27 changes: 0 additions & 27 deletions driver/driver_test.go

This file was deleted.

12 changes: 9 additions & 3 deletions driver/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -177,3 +179,7 @@ func (driver *Driver) Version() (uint64, error) {
return version, nil
}
}

func init() {
driver.RegisterDriver("mysql", &Driver{})
}
10 changes: 7 additions & 3 deletions driver/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
8 changes: 7 additions & 1 deletion driver/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -119,3 +121,7 @@ func (driver *Driver) Version() (uint64, error) {
return version, nil
}
}

func init() {
driver.RegisterDriver("postgres", &Driver{})
}
8 changes: 6 additions & 2 deletions driver/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2a2bc4f

Please sign in to comment.