-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from magicalbanana/enhancements
Enhancements
- Loading branch information
Showing
73 changed files
with
482 additions
and
12,174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
kind: pipeline | ||
name: build-test | ||
|
||
services: | ||
- name: postgres | ||
image: postgres:11 | ||
environment: | ||
POSTGRES_DB: dbal_development | ||
POSTGRES_USER: dbal | ||
POSTGRES_PASSWORD: dbal | ||
|
||
volumes: | ||
- name: deps | ||
temp: {} | ||
|
||
steps: | ||
- name: build-modules | ||
image: golang:1.14.2-alpine3.11 | ||
volumes: | ||
- name: deps | ||
path: /go | ||
commands: | ||
- apk add --no-cache git | ||
- go mod tidy | ||
- go build -v ./... | ||
- go mod vendor | ||
|
||
- name: wait-for-it | ||
image: svajone/wait-for-it:latest | ||
pull: always | ||
commands: | ||
- apk add --update --no-cache bash | ||
- wait-for-it.sh -h 'postgres' -p 5432 -t 180 | ||
|
||
- name: postgres-migrations | ||
image: svajone/pgmngr:v0.1.0 | ||
depends_on: | ||
- wait-for-it | ||
pull: always | ||
commands: | ||
- pgmngr -c .pgmngr.json migration forward | ||
|
||
- name: test | ||
# the reason we're using this image here is because of: | ||
# https://github.com/golang/go/issues/14481 | ||
image: golang:1.14.6-stretch | ||
environment: | ||
DBAL_PG_DB: postgres://dbal:dbal@postgres:5432/dbal_development?sslmode=disable | ||
volumes: | ||
- name: deps | ||
path: /go | ||
depends_on: | ||
- build-modules | ||
- postgres-migrations | ||
commands: | ||
- tests/scripts/testcov.sh | ||
|
||
- name: codecov | ||
image: plugins/codecov | ||
depends_on: | ||
- test | ||
volumes: | ||
- name: deps | ||
path: /go | ||
settings: | ||
required: true | ||
paths: | ||
# see tests/scripts/testcov.sh $workdir | ||
- .cover | ||
token: | ||
from_secret: codecov-token |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"connection": { | ||
"admin": { | ||
"ping_intervals": 5, | ||
"username": "dbal", | ||
"password": "dbal", | ||
"template_database": "postgres", | ||
"host": "postgres", | ||
"port": 5432, | ||
"query_params": { | ||
"sslmode": "disable" | ||
} | ||
}, | ||
"migration": { | ||
"ping_intervals": 5, | ||
"username": "dbal", | ||
"password": "dbal", | ||
"database": "dbal_development", | ||
"host": "postgres", | ||
"port": 5432, | ||
"query_params": { | ||
"sslmode": "disable" | ||
} | ||
} | ||
}, | ||
"migration": { | ||
"directory": "./tests/migrations/", | ||
"table": { | ||
"schema": "public", | ||
"table": "schema_migrations" | ||
} | ||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,33 @@ | ||
[Build Status]: https://travis-ci.org/magicalbanana/dbal | ||
[Build Status Badge]: https://travis-ci.org/magicalbanana/dbal.svg?branch=master | ||
|
||
[Coverage Status]: https://coveralls.io/github/magicalbanana/dbal?branch=master | ||
[Coverage Status Badge]: https://coveralls.io/repos/github/magicalbanana/dbal/badge.svg?branch=master | ||
|
||
[Doc]: https://godoc.org/github.com/magicalbanana/dbal | ||
[Doc Badge]: https://godoc.org/github.com/magicalbanana/dbal?status.svg | ||
|
||
[Go Report Card]: https://goreportcard.com/report/github.com/magicalbanana/dbal | ||
[Go Report Card Badge]: https://goreportcard.com/badge/github.com/magicalbanana/dbal | ||
|
||
[![Build Status][Build Status Badge]][Build Status] | ||
[![Coverage Status][Coverage Status Badge]][Coverage Status] | ||
[![Doc][Doc Badge]][Doc] | ||
[![Go Report Card][Go Report Card Badge]][Go Report Card] | ||
[codecov]: https://codecov.io/gh/magicalbanana/dbal | ||
[codecov badge master]: https://codecov.io/gh/magicalbanana/dbal/branch/master/graph/badge.svg | ||
[drone]: https://cloud.drone.io | ||
[drone repo]: https://cloud.drone.io/magicalbanana/dbal | ||
[drone badge master]: https://cloud.drone.io/api/badges/magicalbanana/dbal/status.svg | ||
[doc]: https://godoc.org/github.com/magicalbanana/dbal | ||
[doc badge]: https://godoc.org/github.com/magicalbanana/dbal?status.svg | ||
[go report card]: https://goreportcard.com/report/github.com/magicalbanana/dbal | ||
[go report card badge]: https://goreportcard.com/badge/github.com/magicalbanana/dbal | ||
|
||
| master | | ||
| - | | ||
| [![drone repo][drone badge master]][drone repo] | | ||
| [![codecov][codecov badge master]][codecov] | | ||
| [![doc][doc badge]][doc] | | ||
| [![go report card][go report card badge]][go report card] | | ||
|
||
# dbal - DB Access Layer | ||
|
||
## Description | ||
|
||
This package wraps the `database/sql` package. | ||
|
||
## Development | ||
|
||
This package is still under active development. It is wise to vendor this package because although not planned some breaking API changes may be introduced. | ||
|
||
## Required Interfaces | ||
This package wraps the `database/sql` package by allowing you to specify | ||
parameters in a map and converting the SQL parameters to positional parameters | ||
to leverage the SQL santizaton provided by the `database/sql` package. | ||
|
||
## Usage | ||
|
||
TBD ... | ||
|
||
## TODO | ||
|
||
- [ ] add support for transactions | ||
- [ ] benchmark SQL parsing |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,109 @@ | ||
package dbal | ||
|
||
var badSQLStmnt = `SSSSSS` | ||
var insertCustomer = `insert into customers(first_name, last_name, address) values(:first_name, :last_name, CAST(NULLIF(:address, '') as jsonb));` | ||
var selectCustomer = `select first_name, last_name, address from customers where first_name = :first_name AND last_name = :last_name;` | ||
var selectAllCustomers = `select first_name, last_name, address from customers;` | ||
var deleteCustomer = `delete from customers where first_name = :first_name and last_name = :last_name;` | ||
import ( | ||
"database/sql" | ||
"log" | ||
"testing" | ||
|
||
"github.com/c2fo/testify/require" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
t.Parallel() | ||
db, openErr := sql.Open("postgres", testPgDbCreds(t).String()) | ||
require.NoError(t, openErr) | ||
|
||
dal := New(db) | ||
require.NotNil(t, dal) | ||
defer db.Close() | ||
} | ||
|
||
func TestOpen(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
desc string | ||
driver string | ||
dbcreds func() string | ||
assertion func(t *testing.T, e error, m string) | ||
}{ | ||
{ | ||
// db connection fails because of the invalid driver being passed | ||
// to sqlx.Open | ||
desc: "database fails to open", | ||
driver: "manbearpig", | ||
dbcreds: func() string { | ||
return "dbname=piggly_wiggly" | ||
}, | ||
assertion: func(t *testing.T, e error, m string) { | ||
require.Error(t, e, m) | ||
}, | ||
}, | ||
{ | ||
desc: "database loads successfully", | ||
driver: "postgres", | ||
dbcreds: func() string { | ||
return "dbname=piggly_wiggly" | ||
}, | ||
assertion: func(t *testing.T, e error, m string) { | ||
require.NoError(t, e, m) | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
db, err := Open(test.driver, test.dbcreds()) | ||
defer func() { | ||
if db != nil { | ||
db.Close() | ||
} | ||
}() | ||
test.assertion(t, err, test.desc) | ||
} | ||
} | ||
|
||
func TestPingDataBase(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
desc string | ||
driver string | ||
db func() DB | ||
assertion func(t *testing.T, e error, m string) | ||
}{ | ||
{ | ||
// DB fails to ping because the database does not exist | ||
desc: "database ping reaches maximum ping time", | ||
driver: "postgres", | ||
db: func() DB { | ||
db, _ := Open("postgres", "dbname=piggly_wiggly") | ||
return db | ||
}, | ||
assertion: func(t *testing.T, e error, m string) { | ||
require.Error(t, e, m) | ||
}, | ||
}, | ||
{ | ||
desc: "database loads successfully", | ||
driver: "postgres", | ||
db: func() DB { | ||
db, _ := Open("postgres", testPgDbCreds(t).String()) | ||
return db | ||
}, | ||
assertion: func(t *testing.T, e error, m string) { | ||
require.NoError(t, e, m) | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
lgr := func(msg string) { | ||
log.Println(msg) | ||
} | ||
pingErr := PingDatabase(test.db(), 2, lgr) | ||
defer func() { | ||
if db := test.db(); db != nil { | ||
db.Close() | ||
} | ||
}() | ||
test.assertion(t, pingErr, test.desc) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
version: '3.7' | ||
|
||
services: | ||
postgres: | ||
image: postgres:11 | ||
environment: | ||
POSTGRES_DB: dbal_development | ||
POSTGRES_USER: dbal | ||
POSTGRES_PASSWORD: dbal | ||
ports: | ||
- 5432:5432 | ||
volumes: | ||
- postgres:/var/lib/postgresql/data | ||
|
||
postgres-migration: | ||
build: | ||
context: . | ||
dockerfile: tests/postgres-migration.Dockerfile | ||
volumes: | ||
- ./tests/migrations:/app/resources/postgres/migrations | ||
|
||
volumes: | ||
postgres: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.