Skip to content

Commit

Permalink
Implement adapter Name and add Name constant (#65)
Browse files Browse the repository at this point in the history
* Implement adapter Name and add Name constant

* Update go-rel dependencies

* Update primaryreplica dependnecy

* Update to allow to use Open and MustOpen for pgx driver

* Update supported postgres versions to match official supported postgres server versions

* Do not run tests in parallel to not mess up pgx/postgres drivers

* Fix review comment
  • Loading branch information
lafriks authored Oct 13, 2023
1 parent 4ea1e88 commit db2201d
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 298 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: PostgreSQL
strategy:
matrix:
postgres: [9.6, 10, 11, 12, 13, 14]
postgres: [11, 12, 13, 14, 15, 16]
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
Expand All @@ -32,14 +32,14 @@ jobs:
POSTGRESQL_DATABASE: postgres://rel:rel@localhost/rel_test
run: |
sleep 15
go test -race ./...
go test -race -p 1 ./...
if: matrix.postgres != 13
- name: Coverage
env:
POSTGRESQL_DATABASE: postgres://rel:rel@localhost/rel_test
run: |
sleep 15
go test -race -coverprofile=coverage.txt -covermode=atomic ./...
go test -race -p 1 -coverprofile=coverage.txt -covermode=atomic ./...
if: matrix.postgres == 13
- name: Codecov
uses: codecov/codecov-action@v1
Expand All @@ -62,4 +62,4 @@ jobs:
TEST_PRIMARY_REPLICA: 'true'
run: |
sleep 15
go test -race -tags=primaryreplica ./...
go test -race -p 1 -tags=primaryreplica -run ^TestAdapter_PrimaryReplica_specs$ ./...
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,22 @@ func main() {
## Supported Driver

- github.com/lib/pq
- github.com/jackc/pgx/v4/stdlib
- github.com/jackc/pgx/v5/stdlib

## Supported Database

- PostgreSQL 9.6, 10, 11, 12, 13 and 14
- PostgreSQL 11, 12, 13, 14, 15 and 16

## Testing

### Start PostgreSQL server in Docker

```console
docker run -it --rm -p 5433:5432 -e "POSTGRES_USER=rel" -e "POSTGRES_PASSWORD=test" -e "POSTGRES_DB=rel_test" postgres:14-alpine
docker run -it --rm -p 25432:5432 -e "POSTGRES_USER=rel" -e "POSTGRES_PASSWORD=rel" -e "POSTGRES_DB=rel_test" postgres:14-alpine
```

### Run tests

```console
POSTGRESQL_DATABASE="postgres://rel:test@localhost:5433/rel_test" go test ./...
go test -p 1 ./...
```
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '2'

services:
postgresql-master:
image: docker.io/bitnami/postgresql:13
image: docker.io/bitnami/postgresql:16
ports:
- '25432:5432'
environment:
Expand All @@ -14,7 +14,7 @@ services:
- POSTGRESQL_DATABASE=rel_test
- ALLOW_EMPTY_PASSWORD=yes
postgresql-slave:
image: docker.io/bitnami/postgresql:13
image: docker.io/bitnami/postgresql:16
ports:
- '25433:5432'
depends_on:
Expand Down
20 changes: 9 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@ go 1.21
toolchain go1.21.0

require (
github.com/go-rel/primaryreplica v0.5.0
github.com/go-rel/rel v0.40.0
github.com/go-rel/sql v0.15.1-0.20230927020931-5b67559d2fe1
github.com/jackc/pgx/v4 v4.18.1
github.com/go-rel/primaryreplica v0.5.1-0.20231009233555-b640af5a3ec8
github.com/go-rel/rel v0.40.1-0.20231007112841-2e07fc196025
github.com/go-rel/sql v0.15.1-0.20231007214945-be5e6ac51f86
github.com/jackc/pgx/v5 v5.4.3
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
254 changes: 22 additions & 232 deletions go.sum

Large diffs are not rendered by default.

44 changes: 19 additions & 25 deletions pgx_test.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,57 @@
package postgres

import (
"database/sql"
"testing"
"time"

"github.com/go-rel/rel"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
"github.com/stretchr/testify/assert"

_ "github.com/jackc/pgx/v5/stdlib"
)

func init() {
// hack to make sure location it has the same location object as returned by pq driver.
// hack to make sure location it has the same location object as returned by pgx driver.
time.Local, _ = time.LoadLocation("Asia/Jakarta")
}

func pgxOpen(dsn string) (rel.Adapter, error) {
config, err := pgx.ParseConfig(dsn)
if err != nil {
return nil, err
}
database, err := sql.Open("pgx", stdlib.RegisterConnConfig(config))
if err != nil {
return nil, err
}
return New(database), err
}

func TestAdapterPgx_specs(t *testing.T) {
adapter, err := pgxOpen(dsn())
assert.Nil(t, err)
driverName = "pgx"
adapter := MustOpen(dsn())
defer adapter.Close()

repo := rel.New(adapter)
AdapterSpecs(t, repo)
}

func TestAdapterPgx_Transaction_commitError(t *testing.T) {
adapter, err := pgxOpen(dsn())
assert.Nil(t, err)
driverName = "pgx"
adapter := MustOpen(dsn())
defer adapter.Close()

assert.NotNil(t, adapter.Commit(ctx))
}

func TestAdapterPgx_Transaction_rollbackError(t *testing.T) {
adapter, err := pgxOpen(dsn())
assert.Nil(t, err)
driverName = "pgx"
adapter := MustOpen(dsn())
defer adapter.Close()

assert.NotNil(t, adapter.Rollback(ctx))
}

func TestAdapterPgx_Exec_error(t *testing.T) {
adapter, err := pgxOpen(dsn())
assert.Nil(t, err)
driverName = "pgx"
adapter := MustOpen(dsn())
defer adapter.Close()

_, _, err = adapter.Exec(ctx, "error", nil)
_, _, err := adapter.Exec(ctx, "error", nil)
assert.NotNil(t, err)
}

func TestAdapterPgx_InvalidDriverPanic(t *testing.T) {
assert.Panics(t, func() {
driverName = "pgx/v4"
MustOpen("postgres://test:test@localhost:1111/test?sslmode=disable&timezone=Asia/Jakarta")
})
}
30 changes: 23 additions & 7 deletions postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type Postgres struct {
sql.SQL
}

// Name of database type this adapter implements.
const Name string = "postgres"

var driverName string = "postgres"

// New postgres adapter using existing connection.
func New(database *db.DB) rel.Adapter {
var (
Expand Down Expand Up @@ -62,15 +67,22 @@ func New(database *db.DB) rel.Adapter {

// Open postgres connection using dsn.
func Open(dsn string) (rel.Adapter, error) {
database, err := db.Open("postgres", dsn)
database, err := db.Open(driverName, dsn)
return New(database), err
}

// MustOpen postgres connection using dsn.
func MustOpen(dsn string) rel.Adapter {
database, err := db.Open("postgres", dsn)
check(err)
return New(database)
adapter, err := Open(dsn)
if err != nil {
panic(err)
}
return adapter
}

// Name of database adapter.
func (Postgres) Name() string {
return Name
}

// Insert inserts a record to database and returns its id.
Expand Down Expand Up @@ -184,8 +196,12 @@ func columnMapper(column *rel.Column) (string, int, int) {
return typ, m, n
}

func check(err error) {
if err != nil {
panic(err)
func init() {
// Identify if pgx driver is available and default to that instead.
for _, drv := range db.Drivers() {
if drv == "pgx" {
driverName = "pgx"
break
}
}
}
30 changes: 17 additions & 13 deletions postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package postgres

import (
"context"
"errors"
"os"
"testing"
"time"
Expand All @@ -29,6 +28,14 @@ func dsn() string {
return "postgres://rel:rel@localhost:25432/rel_test?sslmode=disable&timezone=Asia/Jakarta"
}

func TestAdapter_Name(t *testing.T) {
driverName = "postgres"
adapter := MustOpen(dsn())
defer adapter.Close()

assert.Equal(t, Name, adapter.Name())
}

func AdapterSpecs(t *testing.T, repo rel.Repository) {
// Prepare tables
teardown := specs.Setup(repo)
Expand Down Expand Up @@ -108,6 +115,7 @@ func TestAdapter_specs(t *testing.T) {
return
}

driverName = "postgres"
adapter := MustOpen(dsn())
defer adapter.Close()

Expand All @@ -121,6 +129,7 @@ func TestAdapter_PrimaryReplica_specs(t *testing.T) {
return
}

driverName = "postgres"
adapter := primaryreplica.New(
MustOpen("postgres://rel:rel@localhost:25432/rel_test?sslmode=disable&timezone=Asia/Jakarta"),
MustOpen("postgres://rel:rel@localhost:25433/rel_test?sslmode=disable&timezone=Asia/Jakarta"),
Expand All @@ -133,22 +142,23 @@ func TestAdapter_PrimaryReplica_specs(t *testing.T) {
}

func TestAdapter_Transaction_commitError(t *testing.T) {
adapter, err := Open(dsn())
assert.Nil(t, err)
driverName = "postgres"
adapter := MustOpen(dsn())
defer adapter.Close()

assert.NotNil(t, adapter.Commit(ctx))
}

func TestAdapter_Transaction_rollbackError(t *testing.T) {
adapter, err := Open(dsn())
assert.Nil(t, err)
driverName = "postgres"
adapter := MustOpen(dsn())
defer adapter.Close()

assert.NotNil(t, adapter.Rollback(ctx))
}

func TestAdapter_Exec_error(t *testing.T) {
driverName = "postgres"
adapter, err := Open(dsn())
assert.Nil(t, err)
defer adapter.Close()
Expand All @@ -158,8 +168,8 @@ func TestAdapter_Exec_error(t *testing.T) {
}

func TestAdapter_TableBuilder(t *testing.T) {
adapter, err := Open(dsn())
assert.Nil(t, err)
driverName = "postgres"
adapter := MustOpen(dsn())
defer adapter.Close()

tests := []struct {
Expand Down Expand Up @@ -204,9 +214,3 @@ func TestAdapter_TableBuilder(t *testing.T) {
})
}
}

func TestCheck(t *testing.T) {
assert.Panics(t, func() {
check(errors.New("error"))
})
}

0 comments on commit db2201d

Please sign in to comment.