Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 #1

Merged
merged 3 commits into from
Jan 23, 2018
Merged

v2 #1

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 13 additions & 25 deletions crate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,32 @@ import (
)

func init() {
driver.RegisterDriver("crate", &Driver{})
driver.Register("crate", "sql", nil, Open)
}

type Driver struct {
db *sql.DB
}

// make sure our driver still implements the driver.Driver interface
var _ driver.Driver = (*Driver)(nil)

const tableName = "schema_migrations"

func (driver *Driver) Initialize(url string) error {
func Open(url string) (driver.Driver, error) {
driver := &Driver{}
url = strings.Replace(url, "crate", "http", 1)
db, err := sql.Open("crate", url)
if err != nil {
return err
return nil, err
}

if err := db.Ping(); err != nil {
return err
return nil, err
}
driver.db = db

if err := driver.ensureVersionTableExists(); err != nil {
return err
return nil, err
}
return nil
return driver, nil
}

func (driver *Driver) Close() error {
Expand All @@ -50,10 +48,6 @@ func (driver *Driver) Close() error {
return nil
}

func (driver *Driver) FilenameExtension() string {
return "sql"
}

// Version returns the current migration version.
func (driver *Driver) Version() (file.Version, error) {
var version file.Version
Expand Down Expand Up @@ -89,35 +83,29 @@ func (driver *Driver) Versions() (file.Versions, error) {
return versions, err
}

func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
defer close(pipe)
pipe <- f

func (driver *Driver) Migrate(f file.File) error {
if err := f.ReadContent(); err != nil {
pipe <- err
return
return err
}

lines := splitContent(string(f.Content))
for _, line := range lines {
_, err := driver.db.Exec(line)
if err != nil {
pipe <- err
return
return err
}
}

if f.Direction == direction.Up {
if _, err := driver.db.Exec("INSERT INTO "+tableName+" (version) VALUES (?)", f.Version); err != nil {
pipe <- err
return
return err
}
} else if f.Direction == direction.Down {
if _, err := driver.db.Exec("DELETE FROM "+tableName+" WHERE version=?", f.Version); err != nil {
pipe <- err
return
return err
}
}
return nil
}

// Execute a statement
Expand Down
24 changes: 10 additions & 14 deletions crate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"os"
"testing"

"github.com/db-journey/migrate/file"
"github.com/db-journey/migrate/direction"
pipep "github.com/db-journey/migrate/pipe"
"github.com/db-journey/migrate/driver"
"github.com/db-journey/migrate/file"
)

func TestContentSplit(t *testing.T) {
Expand Down Expand Up @@ -40,9 +40,9 @@ func TestMigrate(t *testing.T) {

url := fmt.Sprintf("crate://%s:%s", host, port)

driver := &Driver{}

if err := driver.Initialize(url); err != nil {
var err error
var driver driver.Driver
if driver, err = Open(url); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -88,19 +88,15 @@ func TestMigrate(t *testing.T) {
}

for _, file := range successFiles {
pipe := pipep.New()
go driver.Migrate(file, pipe)
errs := pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
err := driver.Migrate(file)
if err != nil {
t.Fatal(err)
}
}

for _, file := range failFiles {
pipe := pipep.New()
go driver.Migrate(file, pipe)
errs := pipep.ReadErrors(pipe)
if len(errs) == 0 {
err := driver.Migrate(file)
if err == nil {
t.Fatal("Migration should have failed but succeeded")
}
}
Expand Down