fix postgresql issues

This commit is contained in:
Arnaud Delcasse 2023-05-08 01:16:08 +02:00
parent 9e2b9251db
commit 6ecc05ba0b
6 changed files with 52 additions and 3 deletions

View File

@ -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(".", "_"))

View File

@ -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
}

View File

@ -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
}

View File

@ -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()

View File

@ -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]

View File

@ -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)
}