0
  • I have some code that needs to be run in a Tx like here
  • I generated a go sql migration as mentioned here in the official docs for go goose

my questions are:

  • when are these transactions actually committed to the database?
  • I forced the migrations to be committed with tx.Commit(). Here's my migration file in go:
func Up00002(tx *sql.Tx) error {
    _, err := tx.Exec("UPDATE users SET username='admin' WHERE username='root';")
    if err != nil {
        return err
    }

    if err = tx.Commit(); err != nil {
        return fmt.Errorf("error during committing the transaction: %w", err)
    }


    return nil
}

I am unable to run the above migration after the down part as I get the ErrTxDone (source) when I run them. I still want to run the go migration Up00002 multiple times (for testing). How can I do this? What am I doing wrong here?

AthulMuralidhar
  • 662
  • 1
  • 9
  • 26

1 Answers1

1

So, just going through their documentations here shows that the commit already happens in an AddMigrations call - this is with transactions ofc and is handled internally by go goose - pretty nice :)

The same is true for sql as well, as we see it here

The pkg also provides --no-transaction option which we can use to have full control of the db object - awesome!

AthulMuralidhar
  • 662
  • 1
  • 9
  • 26