From d58208f5ac531a29fcb5ffcaf7f879f3be2122d2 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 24 Nov 2022 13:41:36 +0100 Subject: [PATCH] fix(backend): don't crash if redis isn't set, use tx in CreateMember --- backend/db/db.go | 3 ++- backend/db/member.go | 2 +- backend/db/redis.go | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 backend/db/redis.go diff --git a/backend/db/db.go b/backend/db/db.go index 664157c..77c8a35 100644 --- a/backend/db/db.go +++ b/backend/db/db.go @@ -41,7 +41,8 @@ func New() (*DB, error) { return nil, errors.Wrap(err, "creating redis client") } } else { - log.Warn("$REDIS was empty! ANY FUNCTIONALITY using redis will CRASH the server") + log.Warn("$REDIS was empty! Any functionality using Redis (such as authentication) will not work") + redis = &dummyRedis{} } minioClient, err := minio.New(os.Getenv("MINIO_ENDPOINT"), &minio.Options{ diff --git a/backend/db/member.go b/backend/db/member.go index 6e66463..220b36d 100644 --- a/backend/db/member.go +++ b/backend/db/member.go @@ -94,7 +94,7 @@ func (db *DB) CreateMember(ctx context.Context, tx pgx.Tx, userID xid.ID, name s return m, errors.Wrap(err, "building sql") } - err = pgxscan.Get(ctx, db, &m, sql, args...) + err = pgxscan.Get(ctx, tx, &m, sql, args...) if err != nil { pge := &pgconn.PgError{} if errors.As(err, &pge) { diff --git a/backend/db/redis.go b/backend/db/redis.go new file mode 100644 index 0000000..741ac41 --- /dev/null +++ b/backend/db/redis.go @@ -0,0 +1,20 @@ +package db + +import ( + "context" + "net" + + "emperror.dev/errors" + "github.com/mediocregopher/radix/v4" +) + +var _ radix.Client = (*dummyRedis)(nil) + +type dummyRedis struct{} + +func (*dummyRedis) Addr() net.Addr { return &net.IPAddr{} } +func (*dummyRedis) Close() error { return nil } + +func (*dummyRedis) Do(context.Context, radix.Action) error { + return errors.Sentinel("this is a dummy client") +}