23/12/2019 - GO
You can use package below to create a database instance and use it to truncate list of tables in Golang unit tests.
package test
import (
"database/sql"
"fmt"
)
func OpenDB(driver, address string, maxIdleConns int) *sql.DB {
db, _ := sql.Open(driver, address)
db.SetMaxIdleConns(maxIdleConns)
return db
}
func CloseDB(db *sql.DB) {
_ = db.Close()
}
func TruncateTables(db *sql.DB, tables []string) {
_, _ = db.Exec("SET FOREIGN_KEY_CHECKS=0;")
for _, v := range tables {
_, _ = db.Exec(fmt.Sprintf("TRUNCATE TABLE %s;", v))
}
_, _ = db.Exec("SET FOREIGN_KEY_CHECKS=1;")
}
// Obtain a database instance
db := test.OpenDB(// pass arguments here)
// Truncate tables.
test.TruncateTables(db, []string{"table1", "table2", // ....})
// Do something else.
// Close database.
test.CloseDB(db)