英文:
Cannot make Goose DB Migration working for Go testing
问题
我目前正在学习用于Web编程的Golang,现在我正在学习有关数据库、Rest API和Golang中的测试。
现在我在使用Goose数据库迁移和Go测试集成时遇到了问题。
我想将Goose迁移集成到我的Go测试代码中,我的场景是在测试之前升级所有迁移,然后在测试完成后重置整个数据库。
我的问题是我找不到任何关于如何在Goose中实现这一点的文档/示例代码。
我还尝试使用exec.Command()执行goose命令,但它总是返回退出状态1。
这是我现有的在执行测试之前触发迁移的代码:
func pretest() {
var args = []string{
os.Getenv("DB_SERVER"),
"\"user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable\"",
"up",
}
exe := exec.Command("goose", args...)
exe.Dir = os.Getenv("DB_MIGRATION")
output := exe.Run()
fmt.Println(output)
}
我的问题是是否可能从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:
func pretest() {
var args = []string{
os.Getenv("DB_SERVER"),
"\"user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable\"",
"up",
}
exe := exec.Command("goose", args...)
exe.Dir = os.Getenv("DB_MIGRATION")
/* result, err := exe.Output()*/
//fmt.Println(string(result))
/*fmt.Println(err)*/
output := exe.Run()
fmt.Println(output)
}
$ go test -v
exit status 1
testing: warning: no tests to run
PASS
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文件,该文件涉及到内部如何调用命令:
if err := goose.Run(command, db, *dir, arguments...); err != nil {
log.Fatalf("goose run: %v", err)
}
这里调用了在goose.go中定义的Run
函数:
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:
if err := goose.Run(command, db, *dir, arguments...); err != nil {
log.Fatalf("goose run: %v", err)
}
Which calls the Run
func that is defined in the goose.go
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
最后,我通过更改代码中的以下行来解决了这个问题:
"user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable",
改为:
"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:
"\"user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable\"",
to:
"user=" + os.Getenv("DB_USERNAME") + " dbname=" + os.Getenv("DB_TEST_NAME") + " sslmode=disable",
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论