Skip to content

Commit

Permalink
Merge branch 'release/v0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
diffuse committed Jun 21, 2020
2 parents 870f360 + 1c1b5c4 commit f344c29
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
31 changes: 15 additions & 16 deletions pgsql/db.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package pgsql

import (
"database/sql"
_ "github.com/jackc/pgx"
"github.com/lib/pq"
"context"
"github.com/jackc/pgx"
)

type Database struct {
db *sql.DB
db *pgx.Conn
ctx context.Context
}

// NewDatabase creates and initializes a new Database
func NewDatabase() *Database {
db := &Database{}
db := &Database{ctx: context.Background()}
db.Init()

return db
Expand All @@ -21,25 +21,24 @@ func NewDatabase() *Database {
// Init connects to a PostgreSQL instance and creates the
// tables this service relies on if they don't already exist
func (d *Database) Init() {
// connect to database using configuration created from environment variables
//
// from https://godoc.org/github.com/lib/pq
// "Most environment variables as specified at http://www.postgresql.org/docs/current/static/libpq-envars.html
// supported by libpq are also supported by pq."
connector, err := pq.NewConnector("")
// read the PostgreSQL connection info from the environment
config, err := pgx.ParseConfig("")
if err != nil {
panic(err)
}

d.db = sql.OpenDB(connector)
// connect to database using configuration created from environment variables
if d.db, err = pgx.ConnectConfig(d.ctx, config); err != nil {
panic(err)
}

// create tables
d.createTables()
}

// Close closes connections to the database
func (d *Database) Close() error {
return d.Close()
return d.db.Close(d.ctx)
}

// createTables creates the tables that this service relies on
Expand All @@ -51,7 +50,7 @@ func (d *Database) createTables() {
val INTEGER NOT NULL
)`

if _, err := d.db.Exec(query); err != nil {
if _, err := d.db.Exec(d.ctx, query); err != nil {
panic(err)
}
}
Expand All @@ -65,7 +64,7 @@ func (d *Database) IncrementCounter(counterId int) error {
DO UPDATE
SET val = counter.val + 1`

_, err := d.db.Exec(query, counterId)
_, err := d.db.Exec(d.ctx, query, counterId)
return err
}

Expand All @@ -74,5 +73,5 @@ func (d *Database) GetCounterVal(counterId int) (int, error) {
query := `SELECT val FROM counter WHERE counter_id = $1`

var val int
return val, d.db.QueryRow(query, counterId).Scan(&val)
return val, d.db.QueryRow(d.ctx, query, counterId).Scan(&val)
}
16 changes: 11 additions & 5 deletions pgsql/db_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package pgsql

import (
"context"
"os"
"testing"
)

var testDb *Database
var (
testDb *Database

// testing context
ctx = context.Background()
)

// NOTE: A PostgreSQL instance must be running on db:5432 with
// the below environment configuration for these tests to work
Expand All @@ -22,7 +28,7 @@ func init() {
testDb = NewDatabase()

// drop and recreate tables so tests have a known starting point
if _, err := testDb.db.Exec("DROP TABLE IF EXISTS counter"); err != nil {
if _, err := testDb.db.Exec(ctx, "DROP TABLE IF EXISTS counter"); err != nil {
panic(err)
}
testDb.createTables()
Expand All @@ -32,7 +38,7 @@ func init() {
// has a known starting point
func setupDbTest() {
// delete all rows
if _, err := testDb.db.Exec("DELETE FROM counter"); err != nil {
if _, err := testDb.db.Exec(ctx, "DELETE FROM counter"); err != nil {
panic(err)
}
}
Expand All @@ -42,7 +48,7 @@ func getCounterVal(counterId int, t *testing.T) int {
query := `SELECT val FROM counter WHERE counter_id = $1`

var val int
if err := testDb.db.QueryRow(query, counterId).Scan(&val); err != nil {
if err := testDb.db.QueryRow(ctx, query, counterId).Scan(&val); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -81,7 +87,7 @@ func TestGetCounterVal(t *testing.T) {

// insert a value at counterId 8
query := `INSERT INTO counter(counter_id, val) VALUES($1, $2)`
if _, err := testDb.db.Exec(query, counterId, counterVal); err != nil {
if _, err := testDb.db.Exec(ctx, query, counterId, counterVal); err != nil {
t.Fatal(err)
}

Expand Down

0 comments on commit f344c29

Please sign in to comment.