File tree Expand file tree Collapse file tree 16 files changed +238
-190
lines changed Expand file tree Collapse file tree 16 files changed +238
-190
lines changed Original file line number Diff line number Diff line change 1- # Database: Postgresql, Redis, etc.
2- This project shows the implementation of clients for postgresql, Redis and other databases in Golang.
1+ # Kit
2+ Set of common packages used by all or most projects in my repositories.
33
44## Table of Contents
55
6- - [ Implementation] ( #implementation )
6+ - [ Implementation examples ] ( #implementation-examples )
77 - [ Postgresql] ( #postgresql )
88 - [ Redis] ( #redis )
9+ - [ Testing] ( #testing )
910
10- ## Implementation
11+ ## Implementation examples
1112
1213### Postgresql
1314
14- See the examples already implemented in the folder ` /pgdb ` .
15+ See the examples already implemented in the folder ` /postgresql ` .
1516
1617### Redis
1718
18- See the examples already implemented in the folder ` /redisdb ` .
19+ See the examples already implemented in the folder ` /redis ` .
20+
21+ ### Testing
22+
23+ To run the tests simply execute the following command:
24+
25+ ``` shell
26+ make test
27+ ```
28+
29+ This will stop any containers defined by the compose file for tests if already running
30+ and then rebuild the containers using the compose file.
31+
32+ To down the containers simply execute the following command:
33+
34+ ``` shell
35+ make test-down
36+ ```
37+
38+ lairon14@gmail.com
39+ -- Lairon
Original file line number Diff line number Diff line change 1+ version : ' 3'
2+
3+ networks :
4+ kit-go-network :
5+ driver : bridge
6+
7+ services :
8+ kit_tests :
9+ build :
10+ context : .
11+ dockerfile : ./Dockerfile.test
12+ volumes :
13+ - $PWD:/go/src/github.com/laironacosta/kit-go
14+ depends_on :
15+ - db-test
16+ networks :
17+ - kit-go-network
18+ db-test :
19+ image : postgres:13.2
20+ ports :
21+ - " 5432:5432"
22+ expose :
23+ - " 5432"
24+ environment :
25+ POSTGRES_USER : root
26+ POSTGRES_PASSWORD : root
27+ POSTGRES_DB : testdb
28+ restart : on-failure
29+ networks :
30+ - kit-go-network
31+ redis-test :
32+ image : redis
33+ ports :
34+ - " 6379:6379"
35+ expose :
36+ - " 6379"
37+ restart : on-failure
38+ networks :
39+ - kit-go-network
Original file line number Diff line number Diff line change 1- module github.com/Lairon/db -go
1+ module github.com/laironacosta/kit -go
22
33go 1.16
44
55require (
66 github.com/go-pg/pg/v10 v10.9.0
77 github.com/go-redis/redis/v8 v8.8.0
8+ github.com/sirupsen/logrus v1.8.1
89 github.com/stretchr/testify v1.7.0
910)
Original file line number Diff line number Diff line change @@ -63,7 +63,10 @@ github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7
6363github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
6464github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
6565github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 /go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA =
66+ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE =
67+ github.com/sirupsen/logrus v1.8.1 /go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0 =
6668github.com/stretchr/objx v0.1.0 /go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME =
69+ github.com/stretchr/testify v1.2.2 /go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs =
6770github.com/stretchr/testify v1.5.1 /go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA =
6871github.com/stretchr/testify v1.6.1 /go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg =
6972github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY =
@@ -122,6 +125,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
122125golang.org/x/sys v0.0.0-20190412213103-97732733099d /go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs =
123126golang.org/x/sys v0.0.0-20190904154756-749cb33beabd /go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs =
124127golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9 /go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs =
128+ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 /go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs =
125129golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e /go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs =
126130golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd /go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs =
127131golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 /go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs =
Original file line number Diff line number Diff line change 1+ run-test : test-up test-down
2+
3+ mod :
4+ # This make rule requires Go 1.11+
5+ GO111MODULE=on go mod tidy
6+
7+ # Test Rules
8+ test :
9+ docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit
10+ docker-compose -f docker-compose.test.yml down --volumes
11+
12+ test-up :
13+ docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit
14+
15+ test-down :
16+ docker-compose -f docker-compose.test.yml down --volumes
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11package main
22
33import (
4- "fmt"
54 "github.com/go-pg/pg/v10"
65 "github.com/go-pg/pg/v10/orm"
6+ log "github.com/sirupsen/logrus"
77)
88
99type User struct {
@@ -25,9 +25,9 @@ func main() {
2525
2626func exampleDBModel () {
2727 db := pg .Connect (& pg.Options {
28- User : "postgres " ,
29- Password : "postgres " ,
30- Database : "pg- db-go " ,
28+ User : "root " ,
29+ Password : "root " ,
30+ Database : "db-test " ,
3131 })
3232 defer db .Close ()
3333
@@ -89,9 +89,9 @@ func exampleDBModel() {
8989 panic (err )
9090 }
9191
92- fmt . Printf ("%+v \n " , user )
93- fmt . Printf ("%+v \n " , users )
94- fmt . Printf ("%+v \n " , book )
92+ log . Infof ("%+v \n " , user )
93+ log . Infof ("%+v \n " , users )
94+ log . Infof ("%+v \n " , book )
9595
9696}
9797
Original file line number Diff line number Diff line change 11package main
22
33import (
4- "fmt"
5- "github.com/Lairon/db-go/pgdb"
64 "github.com/go-pg/pg/v10"
75 "github.com/go-pg/pg/v10/orm"
6+ pgKit "github.com/laironacosta/kit-go/postgresql"
7+ log "github.com/sirupsen/logrus"
88)
99
1010type User struct {
@@ -33,10 +33,10 @@ func main() {
3333
3434func NewPgClient () * PgClient {
3535 return & PgClient {
36- db : pgdb .NewPgDB (& pg.Options {
37- User : "postgres " ,
38- Password : "postgres " ,
39- Database : "pg- db-go " ,
36+ db : pgKit .NewPgDB (& pg.Options {
37+ User : "root " ,
38+ Password : "root " ,
39+ Database : "db-test " ,
4040 }),
4141 }
4242}
@@ -102,9 +102,9 @@ func (c *PgClient) createSchemeInsertSelect() {
102102 panic (err )
103103 }
104104
105- fmt . Printf ("%+v \n " , user )
106- fmt . Printf ("%+v \n " , users )
107- fmt . Printf ("%+v \n " , book )
105+ log . Infof ("%+v \n " , user )
106+ log . Infof ("%+v \n " , users )
107+ log . Infof ("%+v \n " , book )
108108
109109}
110110
Original file line number Diff line number Diff line change 1+ package postgresql
2+
3+ import (
4+ "github.com/go-pg/pg/v10"
5+ log "github.com/sirupsen/logrus"
6+ "sync"
7+ "time"
8+ )
9+
10+ var (
11+ instance * pg.DB
12+ once sync.Once
13+ )
14+
15+ func NewPgDB (pgOptions * pg.Options ) * pg.DB {
16+ once .Do (func () {
17+ instance = Connect (pgOptions )
18+ })
19+
20+ return instance
21+ }
22+
23+ func Connect (pgOptions * pg.Options ) * pg.DB {
24+ log .Infoln ("Connecting to postgres database..." )
25+ db := pg .Connect (pgOptions )
26+
27+ var n int
28+ if _ , err := db .QueryOne (pg .Scan (& n ), "SELECT 1" ); err != nil {
29+ log .Errorln (err )
30+ time .Sleep (2 * time .Second )
31+
32+ db = pg .Connect (pgOptions )
33+ if _ , err := db .QueryOne (pg .Scan (& n ), "SELECT 1" ); err != nil {
34+ log .Panicf ("Postgres connection error %+v\n " , err )
35+ }
36+ }
37+
38+ log .Infoln ("Connection to postgres verified and successfully connected..." )
39+ return db
40+ }
You can’t perform that action at this time.
0 commit comments