removing invalid UUIDs

This commit is contained in:
sbouaram 2023-05-29 11:31:50 +02:00
parent e2ad1d9a50
commit e8c2fbb768
1 changed files with 25 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/google/uuid"
"github.com/lib/pq" "github.com/lib/pq"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -31,6 +32,15 @@ func NewPostgresqlStorage(cfg *viper.Viper) (PostgresqlStorage, error) {
pgtables_group_members = cfg.GetString("storage.db.psql.tables.group_members") pgtables_group_members = cfg.GetString("storage.db.psql.tables.group_members")
pgtables_groups = cfg.GetString("storage.db.psql.tables.groups") pgtables_groups = cfg.GetString("storage.db.psql.tables.groups")
) )
fmt.Println("host", host)
fmt.Println("port", port)
fmt.Println("user", user)
fmt.Println("pwd", password)
fmt.Println("dbname", dbname)
fmt.Println("sslmode", sslmode)
fmt.Println("pg_schena", pg_schema)
fmt.Println("group|_members", pgtables_group_members)
fmt.Println("groups", pgtables_groups)
portInt, _ := strconv.Atoi(port) portInt, _ := strconv.Atoi(port)
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", host, portInt, psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", host, portInt,
user, password, dbname, sslmode) user, password, dbname, sslmode)
@ -137,6 +147,11 @@ func (psql PostgresqlStorage) GetGroups(namespaces []string) ([]Group, error) {
} }
func (psql PostgresqlStorage) GetGroupsByIds(ids []string) ([]Group, error) { func (psql PostgresqlStorage) GetGroupsByIds(ids []string) ([]Group, error) {
for i := len(ids) - 1; i >= 0; i-- {
if !isValidUUID(ids[i]) {
ids = append(ids[:i], ids[i+1:]...)
}
}
groups := make([]Group, 0) groups := make([]Group, 0)
query := fmt.Sprintf(`SELECT id, namespace, members, data FROM %s WHERE id = ANY($1)`, psql.Tables["groups"]) query := fmt.Sprintf(`SELECT id, namespace, members, data FROM %s WHERE id = ANY($1)`, psql.Tables["groups"])
rows, err := psql.DbConnection.Query(query, pq.Array(ids)) rows, err := psql.DbConnection.Query(query, pq.Array(ids))
@ -258,6 +273,11 @@ func (psql PostgresqlStorage) GetGroupsMember(namespaces []string) ([]GroupMembe
} }
func (psql PostgresqlStorage) GetGroupsMemberByIds(ids []string) ([]GroupMember, error) { func (psql PostgresqlStorage) GetGroupsMemberByIds(ids []string) ([]GroupMember, error) {
for i := len(ids) - 1; i >= 0; i-- {
if !isValidUUID(ids[i]) {
ids = append(ids[:i], ids[i+1:]...)
}
}
groupMembers := make([]GroupMember, 0) groupMembers := make([]GroupMember, 0)
req := fmt.Sprintf(`SELECT id, member_id, group_id, data req := fmt.Sprintf(`SELECT id, member_id, group_id, data
FROM %s WHERE group_id = ANY($1)`, psql.Tables["group_members"]) FROM %s WHERE group_id = ANY($1)`, psql.Tables["group_members"])
@ -361,3 +381,8 @@ func (psql PostgresqlStorage) Migrate() error {
return nil return nil
} }
func isValidUUID(s string) bool {
_, err := uuid.Parse(s)
return err == nil
}