forked from go-testfixtures/testfixtures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_helper.go
50 lines (39 loc) · 923 Bytes
/
mysql_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package testfixtures
import (
"database/sql"
"fmt"
)
// MySQLHelper is the MySQL helper for this package
type MySQLHelper struct{}
func (MySQLHelper) paramType() int {
return paramTypeQuestion
}
func (MySQLHelper) quoteKeyword(str string) string {
return fmt.Sprintf("`%s`", str)
}
func (MySQLHelper) databaseName(db *sql.DB) (dbName string) {
db.QueryRow("SELECT DATABASE()").Scan(&dbName)
return
}
func (MySQLHelper) whileInsertOnTable(tx *sql.Tx, tableName string, fn func() error) error {
return fn()
}
func (h *MySQLHelper) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) error {
// re-enable after load
defer db.Exec("SET FOREIGN_KEY_CHECKS = 1")
tx, err := db.Begin()
if err != nil {
return err
}
_, err = tx.Exec("SET FOREIGN_KEY_CHECKS = 0")
if err != nil {
return err
}
err = loadFn(tx)
if err != nil {
tx.Rollback()
return err
}
err = tx.Commit()
return err
}