From 6ecc05ba0bb795ee7c0a971247069e8ecd88f43b Mon Sep 17 00:00:00 2001 From: Arnaud Delcasse Date: Mon, 8 May 2023 01:16:08 +0200 Subject: [PATCH] fix postgresql issues --- config.go | 42 +++++++++++++++++++++++++++++++ handlers/incentives/incentives.go | 2 ++ storage/mongodb.go | 2 +- storage/postgresql.go | 4 +-- storage/postgresql/schema.hcl | 2 ++ storage/storage.go | 3 +++ 6 files changed, 52 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index b5f9c96..8cfb0fa 100644 --- a/config.go +++ b/config.go @@ -7,7 +7,49 @@ import ( ) func ReadConfig() (*viper.Viper, error) { + + defaults := map[string]any{ + "name": "COOPGO Carpool Incentives", + "dev_env": false, + "storage": map[string]any{ + "db": map[string]any{ + "type": "psql", + "mongodb": map[string]any{ + "host": "localhost", + "port": 27017, + "db_name": "coopgo_platform", + "collections": map[string]string{ + "incentive_subscriptions": "carpool_incentive_subscriptions", + "proofs": "carpool_proofs", + }, + }, + "psql": map[string]any{ + "host": "localhost", + "port": 5432, + "user": "postgres", + "password": "postgres", + "dbname": "coopgo_platform", + "schema": "carpool_incentives", + "sslmode": "disable", + "tables": map[string]string{ + "incentive_subscriptions": "incentive_subscriptions", + "proofs": "proofs", + }, + }, + }, + }, + "services": map[string]any{ + "grpc": map[string]any{ + "enable": true, + "port": 8080, + }, + }, + } + v := viper.New() + for key, value := range defaults { + v.SetDefault(key, value) + } v.SetConfigName("config") v.AddConfigPath(".") v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) diff --git a/handlers/incentives/incentives.go b/handlers/incentives/incentives.go index 6a28b32..478305a 100644 --- a/handlers/incentives/incentives.go +++ b/handlers/incentives/incentives.go @@ -6,6 +6,7 @@ import ( "git.coopgo.io/coopgo-platform/carpool-incentives/storage" "github.com/google/uuid" + "github.com/rs/zerolog/log" "github.com/spf13/viper" ) @@ -62,6 +63,7 @@ func (h *IncentivesHandler) GetAvailableIncentives(userid string) ([]storage.Inc incentiveSubscriptions, err := h.Storage.GetUserSubscriptions(userid) if err != nil { + log.Error().Err(err).Msg("error retrieving subscriptions") return nil, err } diff --git a/storage/mongodb.go b/storage/mongodb.go index b25073e..7a4a809 100644 --- a/storage/mongodb.go +++ b/storage/mongodb.go @@ -87,6 +87,6 @@ func (s MongoDBStorage) SubscribeIncentive(incentive_subscription IncentiveSubsc return nil } -func (psql MongoDBStorage) Migrate() error { +func (s MongoDBStorage) Migrate() error { return nil } diff --git a/storage/postgresql.go b/storage/postgresql.go index 920b571..6511dce 100644 --- a/storage/postgresql.go +++ b/storage/postgresql.go @@ -57,10 +57,10 @@ func NewPostgresqlStorage(cfg *viper.Viper) (PostgresqlStorage, error) { func (s PostgresqlStorage) GetUserSubscriptions(userid string) ([]IncentiveSubscription, error) { req := fmt.Sprintf(`SELECT id, incentive_id, user_id, identity_verification_ids, data, declined, subscription_datetime - FROM %s - WHERE user_id=$1`, s.Tables["incentive_subscriptions"]) + FROM %s WHERE user_id = $1`, s.Tables["incentive_subscriptions"]) rows, err := s.DbConnection.Query(req, userid) if err != nil { + log.Error().Str("request", req).Err(err).Msg("GetUserSubscriptions query error") return nil, err } defer rows.Close() diff --git a/storage/postgresql/schema.hcl b/storage/postgresql/schema.hcl index ad6d3a5..fddb8ee 100644 --- a/storage/postgresql/schema.hcl +++ b/storage/postgresql/schema.hcl @@ -15,6 +15,7 @@ table "incentive_subscriptions" { column "identity_verification_ids" { null = true type = sql("text[]") + default = sql("array[]::text[]") } column "data" { null = false @@ -27,6 +28,7 @@ table "incentive_subscriptions" { column "subscription_datetime" { null = true type = timestamptz + default = sql("now()") } primary_key { columns = [column.id] diff --git a/storage/storage.go b/storage/storage.go index a07d3fe..7c7185b 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -22,6 +22,9 @@ func NewStorage(cfg *viper.Viper) (Storage, error) { case "mongodb": s, err := NewMongoDBStorage(cfg) return s, err + case "psql": + s, err := NewPostgresqlStorage(cfg) + return s, err default: return nil, fmt.Errorf("storage type %v is not supported", storage_type) }