forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce1b59b
commit 907d8cd
Showing
4 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Package crate implements a driver for the Crate.io database | ||
package crate | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"strings" | ||
|
||
_ "github.com/herenow/go-crate" | ||
"github.com/mattes/migrate/driver" | ||
"github.com/mattes/migrate/file" | ||
"github.com/mattes/migrate/migrate/direction" | ||
) | ||
|
||
func init() { | ||
driver.RegisterDriver("crate", &Driver{}) | ||
} | ||
|
||
type Driver struct { | ||
db *sql.DB | ||
} | ||
|
||
const tableName = "schema_migrations" | ||
|
||
func (driver *Driver) Initialize(url string) error { | ||
db, err := sql.Open("crate", url) | ||
if err != nil { | ||
return err | ||
} | ||
if err := db.Ping(); err != nil { | ||
return err | ||
} | ||
driver.db = db | ||
|
||
if err := driver.ensureVersionTableExists(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (driver *Driver) Close() error { | ||
if err := driver.db.Close(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (driver *Driver) FilenameExtension() string { | ||
return "sql" | ||
} | ||
|
||
func (driver *Driver) Version() (uint64, error) { | ||
var version uint64 | ||
err := driver.db.QueryRow("SELECT version FROM " + tableName + " ORDER BY version DESC LIMIT 1").Scan(&version) | ||
switch { | ||
case err == sql.ErrNoRows: | ||
return 0, nil | ||
case err != nil: | ||
return 0, err | ||
default: | ||
return version, nil | ||
} | ||
} | ||
|
||
func (driver *Driver) Migrate(f file.File, pipe chan interface{}) { | ||
defer close(pipe) | ||
pipe <- f | ||
|
||
if err := f.ReadContent(); err != nil { | ||
pipe <- err | ||
return | ||
} | ||
|
||
lines := strings.Split(string(f.Content), ";") | ||
for _, line := range lines { | ||
query := strings.TrimSpace(line) | ||
query = strings.Replace(query, ";", "", -1) | ||
if query != "" { | ||
_, err := driver.db.Exec(query) | ||
if err != nil { | ||
pipe <- err | ||
return | ||
} | ||
} | ||
} | ||
|
||
if f.Direction == direction.Up { | ||
if _, err := driver.db.Exec("INSERT INTO "+tableName+" (version) VALUES (?)", f.Version); err != nil { | ||
pipe <- err | ||
return | ||
} | ||
} else if f.Direction == direction.Down { | ||
if _, err := driver.db.Exec("DELETE FROM "+tableName+" WHERE version=?", f.Version); err != nil { | ||
pipe <- err | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (driver *Driver) ensureVersionTableExists() error { | ||
if _, err := driver.db.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (version INTEGER PRIMARY KEY)", tableName)); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package crate | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/mattes/migrate/file" | ||
"github.com/mattes/migrate/migrate/direction" | ||
pipep "github.com/mattes/migrate/pipe" | ||
) | ||
|
||
func TestMigrate(t *testing.T) { | ||
host := os.Getenv("CRATE_PORT_4200_TCP_ADDR") | ||
port := os.Getenv("CRATE_PORT_4200_TCP_PORT") | ||
|
||
url := fmt.Sprintf("http://%s:%s", host, port) | ||
|
||
driver := &Driver{} | ||
|
||
if err := driver.Initialize(url); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
successFiles := []file.File{ | ||
{ | ||
Path: "/foobar", | ||
FileName: "001_foobar.up.sql", | ||
Version: 1, | ||
Name: "foobar", | ||
Direction: direction.Up, | ||
Content: []byte(` | ||
CREATE TABLE yolo ( | ||
id integer primary key, | ||
msg string | ||
); | ||
`), | ||
}, | ||
{ | ||
Path: "/foobar", | ||
FileName: "002_foobar.down.sql", | ||
Version: 1, | ||
Name: "foobar", | ||
Direction: direction.Down, | ||
Content: []byte(` | ||
DROP TABLE yolo; | ||
`), | ||
}, | ||
} | ||
|
||
failFiles := []file.File{ | ||
{ | ||
Path: "/foobar", | ||
FileName: "002_foobar.up.sql", | ||
Version: 2, | ||
Name: "foobar", | ||
Direction: direction.Up, | ||
Content: []byte(` | ||
CREATE TABLE error ( | ||
id THIS WILL CAUSE AN ERROR | ||
) | ||
`), | ||
}, | ||
} | ||
|
||
for _, file := range successFiles { | ||
pipe := pipep.New() | ||
go driver.Migrate(file, pipe) | ||
errs := pipep.ReadErrors(pipe) | ||
if len(errs) > 0 { | ||
t.Fatal(errs) | ||
} | ||
} | ||
|
||
for _, file := range failFiles { | ||
pipe := pipep.New() | ||
go driver.Migrate(file, pipe) | ||
errs := pipep.ReadErrors(pipe) | ||
if len(errs) == 0 { | ||
t.Fatal("Migration should have failed but succeeded") | ||
} | ||
} | ||
|
||
if err := driver.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters