本文整理汇总了Golang中github.com/mattes/migrate/pipe.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ExamplePipe
func ExamplePipe() {
p := pipe.New()
go func() { p <- "ping" }()
msg := <-p
fmt.Println(msg)
// Output: ping
}
示例2: Reset
// Reset runs the down and up migration function
func Reset(pipe chan interface{}, url, migrationsPath string) {
pipe1 := pipep.New()
go Down(pipe1, url, migrationsPath)
if ok := pipep.WaitAndRedirect(pipe1, pipe, handleInterrupts()); !ok {
go pipep.Close(pipe, nil)
return
} else {
go Up(pipe, url, migrationsPath)
}
}
示例3: main
func main() {
cfgPath := "lobster.cfg"
if len(os.Args) >= 2 {
cfgPath = os.Args[1]
}
lobster.Setup(cfgPath)
url := "mysql://" + lobster.GetDatabaseString()
pipe := pipep.New()
go migrate.Up(pipe, url, "./db/migrations")
hadError := false
done := false
for !done {
select {
case item, more := <-pipe:
if !more {
done = true
} else {
switch item.(type) {
case string:
fmt.Println(item.(string))
case error:
fmt.Printf("ERROR: %v\n", item)
hadError = true
case file.File:
f := item.(file.File)
if f.Direction == direction.Up {
fmt.Print("> ")
} else if f.Direction == direction.Down {
fmt.Print("< ")
}
fmt.Println(f.FileName)
default:
fmt.Printf("%v", item)
}
}
}
}
if !hadError {
fmt.Println("database up to date")
}
}
示例4: Down
// Down rolls back all migrations
func Down(pipe chan interface{}, url, migrationsPath string) {
d, files, version, err := initDriverAndReadMigrationFilesAndGetVersion(url, migrationsPath)
if err != nil {
go pipep.Close(pipe, err)
return
}
applyMigrationFiles, err := files.ToFirstFrom(version)
if err != nil {
if err2 := d.Close(); err2 != nil {
pipe <- err2
}
go pipep.Close(pipe, err)
return
}
if len(applyMigrationFiles) > 0 {
for _, f := range applyMigrationFiles {
pipe1 := pipep.New()
go d.Migrate(f, pipe1)
if ok := pipep.WaitAndRedirect(pipe1, pipe, handleInterrupts()); !ok {
break
}
}
if err2 := d.Close(); err2 != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
} else {
if err2 := d.Close(); err2 != nil {
pipe <- err2
}
go pipep.Close(pipe, nil)
return
}
}
示例5: TestMigrate
func TestMigrate(t *testing.T) {
var session *gocql.Session
driverUrl := "cassandra://localhost/migratetest"
// prepare a clean test database
u, err := url.Parse(driverUrl)
if err != nil {
t.Fatal(err)
}
cluster := gocql.NewCluster(u.Host)
cluster.Keyspace = u.Path[1:len(u.Path)]
cluster.Consistency = gocql.All
cluster.Timeout = 1 * time.Minute
session, err = cluster.CreateSession()
if err != nil {
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 {
t.Fatal(err)
}
d := &Driver{}
if err := d.Initialize(driverUrl); err != nil {
t.Fatal(err)
}
files := []file.File{
{
Path: "/foobar",
FileName: "001_foobar.up.sql",
Version: 1,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
CREATE TABLE yolo (
id varint primary key,
msg text
);
CREATE INDEX ON yolo (msg);
`),
},
{
Path: "/foobar",
FileName: "002_foobar.down.sql",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
DROP TABLE yolo;
`),
},
{
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
)
`),
},
}
pipe := pipep.New()
go d.Migrate(files[0], pipe)
errs := pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[1], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[2], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) == 0 {
t.Error("Expected test case to fail")
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
}
示例6: TestMigrate
// TestMigrate runs some additional tests on Migrate().
// Basic testing is already done in migrate/migrate_test.go
func TestMigrate(t *testing.T) {
driverUrl := "[email protected](127.0.0.1:3306)/migratetest"
// prepare clean database
connection, err := sql.Open("mysql", driverUrl)
if err != nil {
t.Fatal(err)
}
if _, err := connection.Exec(`DROP TABLE IF EXISTS yolo, yolo1, ` + tableName); err != nil {
t.Fatal(err)
}
d := &Driver{}
if err := d.Initialize(driverUrl); err != nil {
t.Fatal(err)
}
files := []file.File{
{
Path: "/foobar",
FileName: "001_foobar.up.sql",
Version: 1,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
CREATE TABLE yolo (
id int(11) not null primary key auto_increment
);
CREATE TABLE yolo1 (
id int(11) not null primary key auto_increment
);
`),
},
{
Path: "/foobar",
FileName: "002_foobar.down.sql",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
DROP TABLE yolo;
`),
},
{
Path: "/foobar",
FileName: "002_foobar.up.sql",
Version: 2,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
// a comment
CREATE TABLE error (
id THIS WILL CAUSE AN ERROR
);
`),
},
}
pipe := pipep.New()
go d.Migrate(files[0], pipe)
errs := pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[1], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[2], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) == 0 {
t.Error("Expected test case to fail")
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
}
示例7: main
func main() {
flag.Usage = func() {
helpCmd()
}
flag.Parse()
command := flag.Arg(0)
if *version {
fmt.Println(Version)
os.Exit(0)
}
if *migrationsPath == "" {
*migrationsPath, _ = os.Getwd()
}
switch command {
case "create":
verifyMigrationsPath(*migrationsPath)
name := flag.Arg(1)
if name == "" {
fmt.Println("Please specify name.")
os.Exit(1)
}
migrationFile, err := migrate.Create(*url, *migrationsPath, name)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Version %v migration files created in %v:\n", migrationFile.Version, *migrationsPath)
fmt.Println(migrationFile.UpFile.FileName)
fmt.Println(migrationFile.DownFile.FileName)
case "migrate":
verifyMigrationsPath(*migrationsPath)
relativeN := flag.Arg(1)
relativeNInt, err := strconv.Atoi(relativeN)
if err != nil {
fmt.Println("Unable to parse param <n>.")
os.Exit(1)
}
timerStart = time.Now()
pipe := pipep.New()
go migrate.Migrate(pipe, *url, *migrationsPath, relativeNInt)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(1)
}
case "goto":
verifyMigrationsPath(*migrationsPath)
toVersion := flag.Arg(1)
toVersionInt, err := strconv.Atoi(toVersion)
if err != nil || toVersionInt < 0 {
fmt.Println("Unable to parse param <v>.")
os.Exit(1)
}
currentVersion, err := migrate.Version(*url, *migrationsPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
relativeNInt := toVersionInt - int(currentVersion)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Migrate(pipe, *url, *migrationsPath, relativeNInt)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(1)
}
case "up":
verifyMigrationsPath(*migrationsPath)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Up(pipe, *url, *migrationsPath)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(1)
}
case "down":
verifyMigrationsPath(*migrationsPath)
timerStart = time.Now()
pipe := pipep.New()
go migrate.Down(pipe, *url, *migrationsPath)
ok := writePipe(pipe)
printTimer()
if !ok {
os.Exit(1)
}
//.........这里部分代码省略.........
示例8: TestMigrate
// 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"
// prepare clean database
connection, err := sql.Open("postgres", driverUrl)
if err != nil {
t.Fatal(err)
}
if _, err := connection.Exec(`
DROP TABLE IF EXISTS yolo;
DROP TABLE IF EXISTS ` + tableName + `;`); err != nil {
t.Fatal(err)
}
d := &Driver{}
if err := d.Initialize(driverUrl); err != nil {
t.Fatal(err)
}
files := []file.File{
{
Path: "/foobar",
FileName: "001_foobar.up.sql",
Version: 1,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
CREATE TABLE yolo (
id serial not null primary key
);
`),
},
{
Path: "/foobar",
FileName: "002_foobar.down.sql",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
DROP TABLE yolo;
`),
},
{
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
)
`),
},
}
pipe := pipep.New()
go d.Migrate(files[0], pipe)
errs := pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[1], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[2], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) == 0 {
t.Error("Expected test case to fail")
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
}
示例9: UpSync
// UpSync is synchronous version of Up
func UpSync(url, migrationsPath string) (err []error, ok bool) {
pipe := pipep.New()
go Up(pipe, url, migrationsPath)
err = pipep.ReadErrors(pipe)
return err, len(err) == 0
}
示例10: NewPipe
// NewPipe is a convenience function for pipe.New().
// This is helpful if the user just wants to import this package and nothing else.
func NewPipe() chan interface{} {
return pipep.New()
}
示例11: MigrateSync
// MigrateSync is synchronous version of Migrate
func MigrateSync(url, migrationsPath string, relativeN int) (err []error, ok bool) {
pipe := pipep.New()
go Migrate(pipe, url, migrationsPath, relativeN)
err = pipep.ReadErrors(pipe)
return err, len(err) == 0
}
示例12: TestMigrate
// TestMigrate runs some additional tests on Migrate()
// Basic testing is already done in migrate/migrate_test.go
func TestMigrate(t *testing.T) {
driverFile := ":memory:"
driverUrl := "sqlite3://" + driverFile
// prepare clean database
connection, err := sql.Open("sqlite3", driverFile)
if err != nil {
t.Fatal(err)
}
if _, err := connection.Exec(`
DROP TABLE IF EXISTS yolo;
DROP TABLE IF EXISTS ` + tableName + `;`); err != nil {
t.Fatal(err)
}
d := &Driver{}
if err := d.Initialize(driverUrl); err != nil {
t.Fatal(err)
}
files := []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 AUTOINCREMENT
);
`),
},
{
Path: "/foobar",
FileName: "002_foobar.down.sql",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
DROP TABLE yolo;
`),
},
{
Path: "/foobar",
FileName: "002_foobar.up.sql",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
CREATE TABLE error (
THIS; WILL CAUSE; AN ERROR;
)
`),
},
}
pipe := pipep.New()
go d.Migrate(files[0], pipe)
errs := pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[1], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[2], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) == 0 {
t.Error("Expected test case to fail")
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
}
示例13: TestMigrate
func TestMigrate(t *testing.T) {
var session *gocql.Session
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)
if err != nil {
t.Fatal(err)
}
cluster := gocql.NewCluster(u.Host)
cluster.Keyspace = u.Path[1:len(u.Path)]
cluster.Consistency = gocql.All
cluster.Timeout = 1 * time.Minute
session, err = cluster.CreateSession()
if err != nil {
t.Fatal(err)
}
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 {
t.Fatal(err)
}
files := []file.File{
{
Path: "/foobar",
FileName: "001_foobar.up.sql",
Version: 1,
Name: "foobar",
Direction: direction.Up,
Content: []byte(`
CREATE TABLE yolo (
id varint primary key,
msg text
);
CREATE INDEX ON yolo (msg);
`),
},
{
Path: "/foobar",
FileName: "002_foobar.down.sql",
Version: 1,
Name: "foobar",
Direction: direction.Down,
Content: []byte(`
DROP TABLE yolo;
`),
},
{
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
)
`),
},
}
pipe := pipep.New()
go d.Migrate(files[0], pipe)
errs := pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[1], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) > 0 {
t.Fatal(errs)
}
pipe = pipep.New()
go d.Migrate(files[2], pipe)
errs = pipep.ReadErrors(pipe)
if len(errs) == 0 {
t.Error("Expected test case to fail")
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
//.........这里部分代码省略.........