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,19 +1,28 @@
|
|||
package fic
|
||||
|
||||
import ()
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// truncateTable performs an insecure wipe on the given tables.
|
||||
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
|
||||
}
|
||||
for _, table := range tables {
|
||||
if _, err := DBExec("TRUNCATE TABLE " + table + ";"); err != nil {
|
||||
} else {
|
||||
if _, err := tx.Exec("SET FOREIGN_KEY_CHECKS = 0;"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, table := range tables {
|
||||
if _, err := tx.Exec("TRUNCATE TABLE " + table + ";"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := tx.Exec("SET FOREIGN_KEY_CHECKS = 1;"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := DBExec("SET FOREIGN_KEY_CHECKS = 1;"); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Reference in a new issue