Skip to content

Commit

Permalink
dont use tx for the drop and swap (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLeishman committed Jun 8, 2024
1 parent 7d2af2d commit d383cad
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,9 @@ func main() {
os.Exit(0)
}

tx, cancel, err := db.BeginTx()
defer cancel()
if err != nil {
panic(err)
}

// stop foreign key checks
log.Println("disabling foreign key checks for our connection")
err = tx.Exec("set foreign_key_checks=0")
err = db.Exec("set foreign_key_checks=0")
if err != nil {
panic(err)
}
Expand All @@ -471,7 +465,7 @@ func main() {

// drop the old table now that our temp table is done
log.Println("dropping the original table")
err = tx.Exec("drop table if exists`" + tableName + "`")
err = db.Exec("drop table if exists`" + tableName + "`")
if err != nil {
panic(err)
}
Expand All @@ -481,15 +475,15 @@ func main() {
// comma and adding the word "add" at the beginning of each line
if len(constraints) != 0 {
log.Println("adding constraints")
err = tx.Exec("alter table`" + tempTableName + "`" + strings.ReplaceAll(strings.TrimLeft(constraints, ","), "\n", "\nadd"))
err = db.Exec("alter table`" + tempTableName + "`" + strings.ReplaceAll(strings.TrimLeft(constraints, ","), "\n", "\nadd"))
if err != nil {
panic(err)
}
}

for _, r := range triggers {
log.Println("adding original triggers")
err = tx.Exec(renameTriggerTable(r.CreateMySQL, tempTableName))
err = db.Exec(renameTriggerTable(r.CreateMySQL, tempTableName))
if err != nil {
panic(err)
}
Expand All @@ -503,12 +497,7 @@ func main() {
// if you're doing this live, there *is* some down time, but other tools handle this the same
// way, so I don't think it's unreasonable if we do the same
log.Println("renaming temp table")
err = tx.Exec("alter table`" + tempTableName + "`rename`" + tableName + "`")
if err != nil {
panic(err)
}

err = tx.Commit()
err = db.Exec("alter table`" + tempTableName + "`rename`" + tableName + "`")
if err != nil {
panic(err)
}
Expand Down

0 comments on commit d383cad

Please sign in to comment.