go-txdb

Immutable transaction isolated sql driver for golang

Github星跟蹤圖

Build Status
GoDoc

Single transaction based sql.Driver for GO

Package txdb is a single transaction based database sql driver. When the connection
is opened, it starts a transaction and all operations performed on this sql.DB
will be within that transaction. If concurrent actions are performed, the lock is
acquired and connection is always released the statements and rows are not holding the
connection.

Why is it useful. A very basic use case would be if you want to make functional tests
you can prepare a test database and within each test you do not have to reload a database.
All tests are isolated within transaction and though, performs fast. And you do not have
to interface your sql.DB reference in your code, txdb is like a standard sql.Driver.

This driver supports any sql.Driver connection to be opened. You can register txdb
for different sql drivers and have it under different driver names. Under the hood
whenever a txdb driver is opened, it attempts to open a real connection and starts
transaction. When close is called, it rollbacks transaction leaving your prepared
test database in the same state as before.

Given, you have a mysql database called txdb_test and a table users with a username
column.

    package main

    import (
        "database/sql"
        "log"

        "github.com/DATA-DOG/go-txdb"
        _ "github.com/go-sql-driver/mysql"
    )

    func init() {
        // we register an sql driver named "txdb"
        txdb.Register("txdb", "mysql", "root@/txdb_test")
    }

    func main() {
        // dsn serves as an unique identifier for connection pool
        db, err := sql.Open("txdb", "identifier")
        if err != nil {
            log.Fatal(err)
        }
        defer db.Close()

        if _, err := db.Exec(`INSERT INTO users(username) VALUES("gopher")`); err != nil {
            log.Fatal(err)
        }
    }

Every time you will run this application, it will remain in the same state as before.

Testing

Usage is mainly intended for testing purposes. See the db_test.go as an example.

In order to run tests, you need a mysql database with a root access without password locally.

make test

Documentation

See godoc for general API details.
See .travis.yml for supported go versions.

Contributions

Feel free to open a pull request. Note, if you wish to contribute an extension to public (exported methods or types) -
please open an issue before to discuss whether these changes can be accepted. All backward incompatible changes are
and will be treated cautiously.

The public API is locked since it is an sql.Driver and will not change.

License

txdb is licensed under the three clause BSD license

主要指標

概覽
名稱與所有者DATA-DOG/go-txdb
主編程語言Go
編程語言Makefile (語言數: 2)
平台
許可證Other
所有者活动
創建於2015-07-08 07:34:53
推送於2025-03-11 14:40:44
最后一次提交2025-03-11 10:38:26
發布數12
最新版本名稱v0.2.1 (發布於 )
第一版名稱v0.1.0 (發布於 )
用户参与
星數722
關注者數9
派生數48
提交數106
已啟用問題?
問題數37
打開的問題數1
拉請求數30
打開的拉請求數1
關閉的拉請求數5
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?