|
@ -13,17 +13,26 @@ var testDB *DB |
|
|
func TestMain(m *testing.M) { |
|
|
func TestMain(m *testing.M) { |
|
|
testDbConnect := os.Getenv("DB_TEST_CONNECT") |
|
|
testDbConnect := os.Getenv("DB_TEST_CONNECT") |
|
|
if testDbConnect == "" { |
|
|
if testDbConnect == "" { |
|
|
testDbConnect = "xiaoli_test:test1234@(localhost:3306)/xiaoli_test" |
|
|
|
|
|
|
|
|
testDbConnect = "stufflog_test:test1234@(localhost:3306)/stufflog_test" |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ { |
|
|
db, err := Open(testDbConnect) |
|
|
db, err := Open(testDbConnect) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println("DB ERROR", err) |
|
|
log.Println("DB ERROR", err) |
|
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
|
continue |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
testDB = db |
|
|
|
|
|
} |
|
|
|
|
|
if testDB == nil { |
|
|
|
|
|
log.Println("DB FAILED") |
|
|
os.Exit(1) |
|
|
os.Exit(1) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = goose.SetDialect("mysql") |
|
|
|
|
|
|
|
|
err := goose.SetDialect("mysql") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println("GOOSE ERROR", err) |
|
|
log.Println("GOOSE ERROR", err) |
|
|
os.Exit(1) |
|
|
os.Exit(1) |
|
@ -31,20 +40,19 @@ func TestMain(m *testing.M) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for { |
|
|
for { |
|
|
err = goose.Down(db.db.DB, "../../../migrations/mysql") |
|
|
|
|
|
|
|
|
err = goose.Down(testDB.db.DB, "../../../migrations/mysql") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
break |
|
|
break |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = goose.Up(db.db.DB, "../../../migrations/mysql") |
|
|
|
|
|
|
|
|
err = goose.Up(testDB.db.DB, "../../../migrations/mysql") |
|
|
if err != nil && err != goose.ErrNoNextVersion && err != goose.ErrNoCurrentVersion { |
|
|
if err != nil && err != goose.ErrNoNextVersion && err != goose.ErrNoCurrentVersion { |
|
|
log.Println("UP ERROR", err) |
|
|
log.Println("UP ERROR", err) |
|
|
os.Exit(3) |
|
|
os.Exit(3) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
testDB = db |
|
|
|
|
|
code := m.Run() |
|
|
code := m.Run() |
|
|
|
|
|
|
|
|
os.Exit(code) |
|
|
os.Exit(code) |
|
|