DATA-DOG / go-txdb

Immutable transaction isolated sql driver for golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there any way to support intended rollback?

Martin91 opened this issue · comments

Background

I am using go-txdb for unit tests in my project, and I expect that I can keep the same status of my database before running each test case. Currently it seems like that go-txdb rollback only when the sql.DB intends to close all driver connections so that it can not meet my requirement.

I have read the code in this repository but can not find any function or method to solve my problem. Have I missed something? If not, I think I can provide a PR to support this.

Proposal: manual rollback support

Besides original rollback strategy, we can add a new method of txdb.txDriver, for example, txDriver.ManualRollback(dsn string), and extract a new method of txdb.conn, for example, conn.Rollback.

txDriver.ManualRollback is responsible for searching the corresponding conn and then delegate the request to conn.Rollback method.

In conn.Rollback, it just needs to do the below 2 steps:

  1. Rollback the underlying db connection;
  2. reset the tx field of conn.
    That is, it does exactly like the partial of the original conn.Close method, see

    go-txdb/db.go

    Lines 183 to 186 in 8216b38

    if c.tx != nil {
    c.tx.Rollback()
    c.tx = nil
    }

Besides, we need to make txdb.Register returns the txdb.txDriver instance to the caller so that the latter is possible to call its ManualRollback method.