Skip to content

Commit

Permalink
Merge pull request #18 from daveworth/dw/add-sqlite-support
Browse files Browse the repository at this point in the history
Add sqlite support
  • Loading branch information
skateinmars authored Jun 24, 2021
2 parents e5e5a8c + 3d112ae commit bc03be6
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
POSTGRESQL_DATASOURCE ?= postgresql://sqalx:sqalx@localhost:5432/sqalx?sslmode=disable
MYSQL_DATASOURCE ?= sqalx:sqalx@tcp(localhost:3306)/sqalx
SQLITE_DATASOURCE ?= :memory:

.PHONY: test

test:
POSTGRESQL_DATASOURCE="$(POSTGRESQL_DATASOURCE)" \
MYSQL_DATASOURCE="$(MYSQL_DATASOURCE)" \
SQLITE_DATASOURCE="$(SQLITE_DATASOURCE)" \
go test -v -cover -race -timeout=1m ./... && echo OK || (echo FAIL && exit 1)
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,32 @@ 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:

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

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

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

To test against SQlite export the following DSN:

```sh
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](LICENSE) file.
21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: '3.6'

services:
postgres:
image: postgres:9.6-alpine
ports:
- 5432:5432
environment:
- POSTGRES_USER=sqalx
- POSTGRES_PASSWORD=sqalx

mysql:
image: mysql:8.0 # intel only
# image: mysql/mysql-server:8.0 # mac M1 preview
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=sqalx
- MYSQL_USER=sqalx
- MYSQL_PASSWORD=sqalx
- MYSQL_DATABASE=sqalx
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-sql-driver/mysql v1.4.0
github.com/jmoiron/sqlx v1.2.0
github.com/lib/pq v1.2.0
github.com/mattn/go-sqlite3 v1.14.7
github.com/rogpeppe/fastuuid v1.2.0
github.com/stretchr/testify v1.4.0
google.golang.org/appengine v1.6.5 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/fastuuid v1.2.0 h1:Ppwyp6VYCF1nvBTXL3trRso7mXMlRrw9ooo375wvi2s=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
6 changes: 4 additions & 2 deletions sqalx.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,12 @@ func (n *node) Tx() *sqlx.Tx {
// Option to configure sqalx
type Option func(*node) error

// SavePoint option enables PostgreSQL Savepoints for nested transactions.
// SavePoint option enables PostgreSQL and SQLite Savepoints for nested
// transactions.
func SavePoint(enabled bool) Option {
return func(n *node) error {
if enabled && n.Driver.DriverName() != "postgres" {
driverName := n.Driver.DriverName()
if enabled && driverName != "postgres" && driverName != "sqlite3" {
return ErrIncompatibleOption
}
n.savePointEnabled = enabled
Expand Down
15 changes: 15 additions & 0 deletions sqalx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/heetch/sqalx"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/require"
)

Expand All @@ -33,6 +34,17 @@ func TestSqalxConnectPostgreSQL(t *testing.T) {
testSqalxConnect(t, "postgres", dataSource, sqalx.SavePoint(true))
}

func TestSqalxConnectSqlite(t *testing.T) {
dataSource := os.Getenv("SQLITE_DATASOURCE")
if dataSource == "" {
t.Skip()
return
}

testSqalxConnect(t, "sqlite3", dataSource)
testSqalxConnect(t, "sqlite3", dataSource, sqalx.SavePoint(true))
}

func TestSqalxConnectMySQL(t *testing.T) {
dataSource := os.Getenv("MYSQL_DATASOURCE")
if dataSource == "" {
Expand Down Expand Up @@ -61,10 +73,12 @@ func TestSqalxTransactionViolations(t *testing.T) {
require.NoError(t, err)

require.Panics(t, func() {
//nolint:errcheck // the intended panic makes error checking irrelevant
node.Exec("UPDATE products SET views = views + 1")
})

require.Panics(t, func() {
//nolint:errcheck // the intended panic makes error checking irrelevant
node.Beginx()
})

Expand Down Expand Up @@ -222,6 +236,7 @@ func TestSqalxFromTransaction(t *testing.T) {
require.NoError(t, err)

ntx, err := node.Beginx()
require.NoError(t, err)
_, err = ntx.Exec("UPDATE products SET views = views + 1")
require.NoError(t, err)

Expand Down

0 comments on commit bc03be6

Please sign in to comment.