Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
# Database: Postgresql, Redis, etc.
This project shows the implementation of clients for postgresql, Redis and other databases in Golang.
# Kit
Set of common packages used by all or most projects in my repositories.

## Table of Contents

- [Implementation](#implementation)
- [Implementation examples](#implementation-examples)
- [Postgresql](#postgresql)
- [Redis](#redis)
- [Testing](#testing)

## Implementation
## Implementation examples

### Postgresql

See the examples already implemented in the folder `/pgdb`.
See the examples already implemented in the folder `/postgresql`.

### Redis

See the examples already implemented in the folder `/redisdb`.
See the examples already implemented in the folder `/redis`.

### Testing

To run the tests simply execute the following command:

```shell
make test
```

This will stop any containers defined by the compose file for tests if already running
and then rebuild the containers using the compose file.

To down the containers simply execute the following command:

```shell
make test-down
```

lairon14@gmail.com
-- Lairon
39 changes: 39 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: '3'

networks:
kit-go-network:
driver: bridge

services:
kit_tests:
build:
context: .
dockerfile: ./Dockerfile.test
volumes:
- $PWD:/go/src/github.com/laironacosta/kit-go
depends_on:
- db-test
networks:
- kit-go-network
db-test:
image: postgres:13.2
ports:
- "5432:5432"
expose:
- "5432"
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: testdb
restart: on-failure
networks:
- kit-go-network
redis-test:
image: redis
ports:
- "6379:6379"
expose:
- "6379"
restart: on-failure
networks:
- kit-go-network
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/Lairon/db-go
module github.com/laironacosta/kit-go

go 1.16

require (
github.com/go-pg/pg/v10 v10.9.0
github.com/go-redis/redis/v8 v8.8.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7
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/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
Expand Down Expand Up @@ -122,6 +125,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
16 changes: 16 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
run-test: test-up test-down

mod:
# This make rule requires Go 1.11+
GO111MODULE=on go mod tidy

# Test Rules
test:
docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit
docker-compose -f docker-compose.test.yml down --volumes

test-up:
docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit

test-down:
docker-compose -f docker-compose.test.yml down --volumes
34 changes: 0 additions & 34 deletions pgdb/pg_db.go

This file was deleted.

49 changes: 0 additions & 49 deletions pgdb/pg_db_test.go

This file was deleted.

14 changes: 7 additions & 7 deletions pgdb/example1/example1.go → postgresql/example1/example1.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
log "github.com/sirupsen/logrus"
)

type User struct {
Expand All @@ -25,9 +25,9 @@ func main() {

func exampleDBModel() {
db := pg.Connect(&pg.Options{
User: "postgres",
Password: "postgres",
Database: "pg-db-go",
User: "root",
Password: "root",
Database: "db-test",
})
defer db.Close()

Expand Down Expand Up @@ -89,9 +89,9 @@ func exampleDBModel() {
panic(err)
}

fmt.Printf("%+v \n", user)
fmt.Printf("%+v \n", users)
fmt.Printf("%+v \n", book)
log.Infof("%+v \n", user)
log.Infof("%+v \n", users)
log.Infof("%+v \n", book)

}

Expand Down
18 changes: 9 additions & 9 deletions pgdb/example2/example2.go → postgresql/example2/example2.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"fmt"
"github.com/Lairon/db-go/pgdb"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
pgKit "github.com/laironacosta/kit-go/postgresql"
log "github.com/sirupsen/logrus"
)

type User struct {
Expand Down Expand Up @@ -33,10 +33,10 @@ func main() {

func NewPgClient() *PgClient {
return &PgClient{
db: pgdb.NewPgDB(&pg.Options{
User: "postgres",
Password: "postgres",
Database: "pg-db-go",
db: pgKit.NewPgDB(&pg.Options{
User: "root",
Password: "root",
Database: "db-test",
}),
}
}
Expand Down Expand Up @@ -102,9 +102,9 @@ func (c *PgClient) createSchemeInsertSelect() {
panic(err)
}

fmt.Printf("%+v \n", user)
fmt.Printf("%+v \n", users)
fmt.Printf("%+v \n", book)
log.Infof("%+v \n", user)
log.Infof("%+v \n", users)
log.Infof("%+v \n", book)

}

Expand Down
40 changes: 40 additions & 0 deletions postgresql/pg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package postgresql

import (
"github.com/go-pg/pg/v10"
log "github.com/sirupsen/logrus"
"sync"
"time"
)

var (
instance *pg.DB
once sync.Once
)

func NewPgDB(pgOptions *pg.Options) *pg.DB {
once.Do(func() {
instance = Connect(pgOptions)
})

return instance
}

func Connect(pgOptions *pg.Options) *pg.DB {
log.Infoln("Connecting to postgres database...")
db := pg.Connect(pgOptions)

var n int
if _, err := db.QueryOne(pg.Scan(&n), "SELECT 1"); err != nil {
log.Errorln(err)
time.Sleep(2 * time.Second)

db = pg.Connect(pgOptions)
if _, err := db.QueryOne(pg.Scan(&n), "SELECT 1"); err != nil {
log.Panicf("Postgres connection error %+v\n", err)
}
}

log.Infoln("Connection to postgres verified and successfully connected...")
return db
}
25 changes: 25 additions & 0 deletions postgresql/pg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package postgresql

import (
"github.com/go-pg/pg/v10"
"github.com/stretchr/testify/assert"
"testing"
)

var pgOptions = &pg.Options{
Addr: "db-test:5432",
User: "root",
Password: "root",
Database: "testdb",
}

func TestNewPgDB_WhenOptionsOk_ThenCreateClient(t *testing.T) {
got := NewPgDB(pgOptions)
defer got.Close()

assert.NotNil(t, got)
assert.Equal(t, pgOptions.Addr, got.Options().Addr)
assert.Equal(t, pgOptions.User, got.Options().User)
assert.Equal(t, pgOptions.Password, got.Options().Password)
assert.Equal(t, pgOptions.Database, got.Options().Database)
}
11 changes: 5 additions & 6 deletions redisdb/example1/example1.go → redis/example1/example1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package main

import (
"context"
"fmt"
"github.com/Lairon/db-go/redisdb"
"github.com/go-redis/redis/v8"
"log"
redisKit "github.com/laironacosta/kit-go/redis"
log "github.com/sirupsen/logrus"
"time"
)

Expand All @@ -25,13 +24,13 @@ func main() {
client.SetKey(ctx, key, value, 10*time.Second)
val := client.GetKey(ctx, key)

fmt.Printf("Key: %s, Value: %s", key, val)
log.Infof("Key: %s, Value: %s", key, val)
}

func NewRedisClient() *RedisClient {
return &RedisClient{
db: redisdb.NewRedisDB(&redis.Options{
Addr: "localhost:6379",
db: redisKit.NewRedisDB(&redis.Options{
Addr: "redis-test:6379",
Password: "", // no password set
DB: 0, // use default DB
}),
Expand Down
Loading