无法使Go测试中的Goose DB迁移工作

huangapple go评论125阅读模式
英文:

Cannot make Goose DB Migration working for Go testing

问题

我目前正在学习用于Web编程的Golang,现在我正在学习有关数据库、Rest API和Golang中的测试。

现在我在使用Goose数据库迁移和Go测试集成时遇到了问题。

我想将Goose迁移集成到我的Go测试代码中,我的场景是在测试之前升级所有迁移,然后在测试完成后重置整个数据库。

我的问题是我找不到任何关于如何在Goose中实现这一点的文档/示例代码。

我还尝试使用exec.Command()执行goose命令,但它总是返回退出状态1。

这是我现有的在执行测试之前触发迁移的代码:

  1. func pretest() {
  2. var args = []string{
  3. os.Getenv("DB_SERVER"),
  4. "\"user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable\"",
  5. "up",
  6. }
  7. exe := exec.Command("goose", args...)
  8. exe.Dir = os.Getenv("DB_MIGRATION")
  9. output := exe.Run()
  10. fmt.Println(output)
  11. }

我的问题是是否可能从Go代码内部触发迁移(up/down/reset)(在这种情况下是测试代码)?

为什么exec.Command()在执行另一个Linux命令时(如ls、pwd、mkdir)工作正常,但却始终返回退出状态1?

英文:

I am currently learning Golang for web programming and now I continue by learning about database,Rest API and testing in Golang.

and now I got an issue with Goose database Migration and Go Testing integration.

I want to integrate goose migration to my Go testing code, my scenario is to up all migrations before test and then reset all database once testing is done.

my problem is I can't find any documentation/sample code to do it with Goose.

I also have tried to execute the goose command using exec.Command() but it always returns exit status 1

this is my existing code to trigger the migration up before testing being executed:

  1. func pretest() {
  2. var args = []string{
  3. os.Getenv("DB_SERVER"),
  4. "\"user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable\"",
  5. "up",
  6. }
  7. exe := exec.Command("goose", args...)
  8. exe.Dir = os.Getenv("DB_MIGRATION")
  9. /* result, err := exe.Output()*/
  10. //fmt.Println(string(result))
  11. /*fmt.Println(err)*/
  12. output := exe.Run()
  13. fmt.Println(output)
  14. }

无法使Go测试中的Goose DB迁移工作

  1. $ go test -v
  2. exit status 1
  3. testing: warning: no tests to run
  4. PASS
  5. ok

my question is it possible to trigger migration (up/down/reset) from inside Go Code (in this case is testing code) ?

Why exec.Command() keep returning code status 1 but it work well when I execute another linux command (ls, pwd, mkdir are work well in the same way) ?

答案1

得分: 1

我对go也是新手,所以我的分析可能完全错误,但由于goose似乎是一个开源的包,我查看了cmd/goose/main.go文件,该文件涉及到内部如何调用命令:

  1. if err := goose.Run(command, db, *dir, arguments...); err != nil {
  2. log.Fatalf("goose run: %v", err)
  3. }

这里调用了在goose.go中定义的Run函数:

  1. func Run(command string, db *sql.DB, dir string, args ...string) error {

该函数接受command字符串参数和db字符串参数。

但是,如果你仍然想使用execCommand,也许你可以查看goose_test.go文件,该文件传递了dir参数,用于指定迁移的目录。

英文:

I am new to go as well, so my analysis may be totally wrong but since goose seems to be an open source package itself, I took a look at the cmd/goose/main.go file that refers to how the commands are called internally:

  1. if err := goose.Run(command, db, *dir, arguments...); err != nil {
  2. log.Fatalf("goose run: %v", err)
  3. }

Which calls the Runfunc that is defined in the goose.go

  1. func Run(command string, db *sql.DB, dir string, args ...string) error {

That takes argument command string argument with db strings as well.

But if you would like to still work with the execCommand then maybe you could take a look at goose_test.go file which which passes arguments dir where the migrations are to be run perhaps.

答案2

得分: 0

最后,我通过更改代码中的以下行来解决了这个问题:

  1. "user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable",

改为:

  1. "user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable",
英文:

Finally I got this issue solved by changing this following line on my code:

  1. "\"user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable\"",

to:

  1. "user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable",

huangapple
  • 本文由 发表于 2017年7月30日 21:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/45400203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定