reset db: use transaction when turncating tables
This commit is contained in:
parent
dcb67fba63
commit
f70fb20a70
1 changed files with 17 additions and 8 deletions
|
@ -1,20 +1,29 @@
|
||||||
package fic
|
package fic
|
||||||
|
|
||||||
import ()
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
// truncateTable performs an insecure wipe on the given tables.
|
// truncateTable performs an insecure wipe on the given tables.
|
||||||
func truncateTable(tables ...string) (error) {
|
func truncateTable(tables ...string) (error) {
|
||||||
if _, err := DBExec("SET FOREIGN_KEY_CHECKS = 0;"); err != nil {
|
if tx, err := db.BeginTx(context.TODO(), nil); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
if _, err := tx.Exec("SET FOREIGN_KEY_CHECKS = 0;"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
if _, err := DBExec("TRUNCATE TABLE " + table + ";"); err != nil {
|
if _, err := tx.Exec("TRUNCATE TABLE " + table + ";"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, err := DBExec("SET FOREIGN_KEY_CHECKS = 1;"); err != nil {
|
if _, err := tx.Exec("SET FOREIGN_KEY_CHECKS = 1;"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue