Skip to content

Commit 6ea1e93

Browse files
author
Georgi Dimitrov
committed
Add type for postgresql retriever, for further implementations / bug fix
1 parent 92e33e7 commit 6ea1e93

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

cmd/relayproxy/config/retriever.go

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type RetrieverConf struct {
3030
URI string `mapstructure:"uri" koanf:"uri"`
3131
Database string `mapstructure:"database" koanf:"database"`
3232
Collection string `mapstructure:"collection" koanf:"collection"`
33+
Type string `mapstructure:"type" koanf:"type"`
3334
Table string `mapstructure:"table" koanf:"table"`
3435
Column string `mapstructure:"column" koanf:"column"`
3536
RedisOptions *redis.Options `mapstructure:"redisOptions" koanf:"redisOptions"`
@@ -127,6 +128,10 @@ func (c *RetrieverConf) validatePostgreSQLRetriever() error {
127128
if c.URI == "" {
128129
return fmt.Errorf("invalid retriever: no \"uri\" property found for kind \"%s\"", c.Kind)
129130
}
131+
if c.Type == "" || !(c.Type == "json") {
132+
return fmt.Errorf("invalid retriever: no \"type\" property or not a valid type in kind \"%s\"", c.Kind)
133+
}
134+
130135
return nil
131136
}
132137

cmd/relayproxy/service/gofeatureflag.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/thomaspoignant/go-feature-flag/notifier/slacknotifier"
2727
"github.com/thomaspoignant/go-feature-flag/notifier/webhooknotifier"
2828
"github.com/thomaspoignant/go-feature-flag/retriever"
29+
"github.com/thomaspoignant/go-feature-flag/retriever/azblobstorageretriever"
2930
"github.com/thomaspoignant/go-feature-flag/retriever/bitbucketretriever"
3031
"github.com/thomaspoignant/go-feature-flag/retriever/fileretriever"
3132
"github.com/thomaspoignant/go-feature-flag/retriever/gcstorageretriever"
@@ -186,7 +187,7 @@ func initRetriever(c *config.RetrieverConf) (retriever.Retriever, error) {
186187
case config.MongoDBRetriever:
187188
return &mongodbretriever.Retriever{Database: c.Database, URI: c.URI, Collection: c.Collection}, nil
188189
case config.PostgreSQLRetriever:
189-
return &postgresqlretriever.Retriever{URI: c.URI, Table: c.Table, Column: c.Column}, nil
190+
return &postgresqlretriever.Retriever{Type: c.Type, URI: c.URI, Table: c.Table, Column: c.Column}, nil
190191
case config.RedisRetriever:
191192
return &redisretriever.Retriever{Options: c.RedisOptions, Prefix: c.RedisPrefix}, nil
192193
case config.AzBlobStorageRetriever:

examples/retriever_postgresql/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func main() {
2323
Context: context.Background(),
2424
Retriever: &postgresqlretriever.Retriever{
2525
Table: "flags",
26+
Type: "json",
2627
Column: "flag",
2728
URI: "postgres://root:example@localhost:5432/flags_db?sslmode=disable",
2829
},

retriever/postgresqlretriever/retriever.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"fmt"
77

88
"github.com/jackc/pgx/v5/pgxpool"
9-
_ "github.com/lib/pq"
109
"github.com/thomaspoignant/go-feature-flag/retriever"
1110
"github.com/thomaspoignant/go-feature-flag/utils/fflog"
1211
)
1312

1413
// Retriever is a configuration struct for a PostgreSQL connection
1514
type Retriever struct {
1615
// PostgreSQL connection URI
17-
URI string
16+
URI string
17+
Type string
1818
// PostgreSQL table where flag column is stored
1919
Table string
2020
// PostgreSQL column where flag definitions are stored
@@ -51,8 +51,8 @@ func (r *Retriever) Init(ctx context.Context, logger *fflog.FFLogger) error {
5151

5252
r.dbPool = pool
5353
r.status = retriever.RetrieverReady
54-
5554
}
55+
5656
return nil
5757
}
5858

@@ -65,15 +65,14 @@ func (r *Retriever) Status() retriever.Status {
6565
}
6666

6767
// Shutdown disconnects the retriever from Mongodb instance
68-
func (r *Retriever) Shutdown(ctx context.Context) error {
68+
func (r *Retriever) Shutdown(_ context.Context) error {
6969
r.dbPool.Close()
7070
return nil
7171
}
7272

7373
// Retrieve Reads flag configuration from postgreSQL and returns it
74-
// if a document does not comply with the specification it will be ignored
74+
// If a document does not comply with the specification it will be ignored
7575
func (r *Retriever) Retrieve(ctx context.Context) ([]byte, error) {
76-
7776
query := fmt.Sprintf("SELECT %s FROM %s WHERE %s IS NOT NULL", r.Column, r.Table, r.Column)
7877
rows, err := r.dbPool.Query(ctx, query)
7978
if err != nil {
@@ -117,5 +116,4 @@ func (r *Retriever) Retrieve(ctx context.Context) ([]byte, error) {
117116
}
118117

119118
return flags, nil
120-
121119
}

retriever/postgresqlretriever/retriever_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import (
1818
"go.mongodb.org/mongo-driver/bson"
1919
)
2020

21-
func Test_MongoDBRetriever_Retrieve(t *testing.T) {
21+
func Test_PostgreSQLRetriever_Retrieve(t *testing.T) {
2222
ctx := context.Background()
2323

24-
dbName := "users"
25-
dbUser := "user"
26-
dbPassword := "password"
24+
dbName := "flags_db"
25+
dbUser := "root"
26+
dbPassword := "example"
2727

2828
tests := []struct {
2929
name string
@@ -56,7 +56,7 @@ func Test_MongoDBRetriever_Retrieve(t *testing.T) {
5656
},
5757
}
5858

59-
// Start the postgres ctr and run any migrations on it
59+
// Start the postgres container
6060
ctr, err := postgres.Run(
6161
ctx,
6262
"postgres:16-alpine",
@@ -69,10 +69,11 @@ func Test_MongoDBRetriever_Retrieve(t *testing.T) {
6969
testcontainers.CleanupContainer(t, ctr)
7070
require.NoError(t, err)
7171

72+
// Run initialization query to create the table and the column
7273
_, _, err = ctr.Exec(ctx, []string{"psql", "-U", dbUser, "-d", dbName, "-c", "CREATE TABLE flags (id SERIAL PRIMARY KEY,flag JSONB)"})
7374
require.NoError(t, err)
7475

75-
//Create snapshot of the database, which is then restored before each test
76+
// Create snapshot of the database, which is then restored before each test
7677
err = ctr.Snapshot(ctx)
7778
require.NoError(t, err)
7879

@@ -89,7 +90,7 @@ func Test_MongoDBRetriever_Retrieve(t *testing.T) {
8990
defer conn.Close(context.Background())
9091

9192
if item.data != "" {
92-
// insert data
93+
// Insert data
9394
var documents []bson.M
9495
err = json.Unmarshal([]byte(item.data), &documents)
9596
require.NoError(t, err)
@@ -100,7 +101,7 @@ func Test_MongoDBRetriever_Retrieve(t *testing.T) {
100101
}
101102
}
102103

103-
// retriever
104+
// Initialize Retriever
104105
mdb := postgresqlretriever.Retriever{
105106
URI: dbURL,
106107
Table: "flags",
@@ -123,7 +124,6 @@ func Test_MongoDBRetriever_Retrieve(t *testing.T) {
123124
}
124125

125126
require.NoError(t, err)
126-
127127
}
128128
}
129129

0 commit comments

Comments
 (0)