From e2922c4b1aa819561784f247a8d25fff05ad8f0a Mon Sep 17 00:00:00 2001 From: Henning Perl Date: Tue, 14 Nov 2023 15:33:49 +0100 Subject: [PATCH] fix: detect unique violation on MySQL --- persistence/sql/persister_consent.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/persistence/sql/persister_consent.go b/persistence/sql/persister_consent.go index 602730b279c..3b8405316c9 100644 --- a/persistence/sql/persister_consent.go +++ b/persistence/sql/persister_consent.go @@ -787,17 +787,23 @@ func (p *Persister) FlushInactiveLoginConsentRequests(ctx context.Context, notAf } func (p *Persister) mySQLConfirmLoginSession(ctx context.Context, session *flow.LoginSession) error { - err := p.Connection(ctx).Create(session) + err := sqlcon.HandleError(p.Connection(ctx).Create(session)) + if err == nil { + return nil + } + + if !errors.Is(err, sqlcon.ErrUniqueViolation) { + return err + } + + n, err := p.Connection(ctx). + Where("id = ? and nid = ?", session.ID, session.NID). + UpdateQuery(session, "authenticated_at", "subject", "identity_provider_session_id", "remember") if err != nil { - n, err := p.Connection(ctx). - Where("id = ? and nid = ?", session.ID, session.NID). - UpdateQuery(session, "authenticated_at", "subject", "identity_provider_session_id", "remember") - if err != nil { - return errors.WithStack(sqlcon.HandleError(err)) - } - if n == 0 { - return errorsx.WithStack(x.ErrNotFound) - } + return errors.WithStack(sqlcon.HandleError(err)) + } + if n == 0 { + return errorsx.WithStack(x.ErrNotFound) } return nil