From 1e9f73df621d02c94fd47516ecc51f9a35977bcf Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 18 May 2023 10:40:39 +0200 Subject: [PATCH] [v10.0.x] Chore: Ignore unique constrain failure when creating the main organization (#68674) Chore: Ignore unique constrain failure when creating the main organization (#68644) Chore: Ignore unique constrain failure when creatin the main organization (cherry picked from commit 633b7875e7185f6d2969a5655d57ec30784e7a3f) Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> --- pkg/services/sqlstore/user.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index 19bcc640cd5..fd662fe85a2 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -157,6 +157,7 @@ func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, erro if has { return org.ID, nil } + ss.log.Debug("auto assigned organization not found") if ss.Cfg.AutoAssignOrgId != 1 { ss.log.Error("Could not create user: organization ID does not exist", "orgID", @@ -168,6 +169,11 @@ func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, erro org.Name = mainOrgName org.ID = int64(ss.Cfg.AutoAssignOrgId) if err := sess.InsertId(&org, ss.Dialect); err != nil { + ss.log.Error("failed to insert organization with provided id", "org_id", org.ID, "err", err) + // ignore failure if for some reason the organization exists + if ss.GetDialect().IsUniqueConstraintViolation(err) { + return org.ID, nil + } return 0, err } } else {