[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 633b7875e7)

Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
This commit is contained in:
Grot (@grafanabot)
2023-05-18 10:40:39 +02:00
committed by GitHub
co-authored by Sofia Papagiannaki
parent 4cb0fb9c92
commit 1e9f73df62
+6
View File
@@ -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 {