Giter Club home page Giter Club logo

sqalx's Introduction

โš ๏ธ Warning: This repository is considered inactive and no change will be made to it except for security updates.

sqalx

GoDoc Go Report Card

sqalx (pronounced 'scale-x') is a library built on top of sqlx that allows to seamlessly create nested transactions and to avoid thinking about whether or not a function is called within a transaction. With sqalx you can easily create reusable and composable functions that can be called within or out of transactions and that can create transactions themselves.

Getting started

$ go get github.com/heetch/sqalx

Import sqalx

import "github.com/heetch/sqalx"

Usage

package main

import (
	"log"

	"github.com/heetch/sqalx"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
)

func main() {
	// Connect to PostgreSQL with sqlx.
	db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	// Pass the db to sqalx.
	// It returns a sqalx.Node. A Node is a wrapper around sqlx.DB or sqlx.Tx.
	node, err := sqalx.New(db)
	if err != nil {
		log.Fatal(err)
	}

	err = createUser(node)
	if err != nil {
		log.Fatal(err)
	}
}

func createUser(node sqalx.Node) error {
	// Exec a query
	_, _ = node.Exec("INSERT INTO ....") // you can use a node as if it were a *sqlx.DB or a *sqlx.Tx

	// Let's create a transaction.
	// A transaction is also a sqalx.Node.
	tx, err := node.Beginx()
	if err != nil {
		return err
	}
	defer tx.Rollback()

	_, _ = tx.Exec("UPDATE ...")

	// Now we call another function and pass it the transaction.
	err = updateGroups(tx)
	if err != nil {
		return nil
	}

	return tx.Commit()
}

func updateGroups(node sqalx.Node) error {
	// Notice we are creating a new transaction.
	// This would normally cause a dead lock without sqalx.
	tx, err := node.Beginx()
	if err != nil {
		return err
	}
	defer tx.Rollback()

	_, _ = tx.Exec("INSERT ...")
	_, _ = tx.Exec("UPDATE ...")
	_, _ = tx.Exec("DELETE ...")

	return tx.Commit()
}

PostgreSQL Savepoints

When using the PostgreSQL driver, an option can be passed to New to enable the use of PostgreSQL Savepoints for nested transactions.

node, err := sqalx.New(db, sqalx.SavePoint(true))

Issue

Please open an issue if you encounter any problem.

Development

sqalx is covered by a go test suite. In order to test against specific databases we include a docker-compose file that runs Postgres and MySQL.

Running all tests

To run the tests, first run docker-compose up to run both Postgres and MySQL in locally-exposed docker images. Then run your tests via make test which sets up the above described data sources and runs all tests.

Running specific tests

To test against the Postgres instance be sure to export the following DSN:

export POSTGRESQL_DATASOURCE="postgresql://sqalx:sqalx@localhost:5432/sqalx?sslmode=disable"

To test against the MySQL instance be sure to export the following DSN:

export MYSQL_DATASOURCE="sqalx:sqalx@tcp(localhost:3306)/sqalx"

To test against SQlite export the following DSN:

export SQLITE_DATASOURCE=":memory:"

Note: If you are developing on an M1 Mac you will need to use the officially supported by Oracle image rather than the default mysql:tag image. It is commented out in docker-compose.yml.

License

The library is released under the MIT license. See LICENSE file.

sqalx's People

Contributors

asdine avatar daveworth avatar espadrine avatar gyndav avatar nivl avatar rogpeppe avatar sixstone-qq avatar skateinmars avatar thara avatar thiht avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sqalx's Issues

github.com/satori/go.uuid interface change

Hi. I'm using dep to include sqalx.
Interface of github.com/satori/go.uuid changed and now we have
vendor/github.com/heetch/sqalx/sqalx.go:136:52: not enough arguments in call to uuid.Must
have (uuid.UUID)
want (uuid.UUID, error)
Is there any work around here?

Nested transactions in postgres

Hi, thanks for this lib as this is really needed.

Looking at the code, I'm not sure I understand how nested transactions work... As far as I understand, Beginx() uses the same Node object and just does a savepoint and overrides the last savepoint that happened?

If I understand code correctly, below code with 2 savepoints would not work?

create table testtable (
	field1 text,
	field2 int
)

begin transaction

insert into testtable (field1, field2) values ('edsa', 1)

SAVEPOINT sp_1;

insert into testtable (field1, field2) values ('eds22a', 13)

SAVEPOINT sp_2;

insert into testtable (field1, field2) values ('eds22eea', 123)

-- 3 rows
select * from testtable

ROLLBACK TO SAVEPOINT sp_2;

-- 2 rows
select * from testtable

ROLLBACK TO SAVEPOINT sp_1;

-- 1 row
select * from testtable

rollback transaction

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.