Bug(sqlstore): fix issue with postgres unable to find existing main organization (#73789)
* SQLStore: fix issue where postgres would not find the existing org Grafana using a postgres databases would fail to find the created org when the CreatedAt and UpdatedAt times are (inaccurately) populated. This issue only occurs in postgres, and only shows up when getOrCreateOrg run to create the admin user AND the organization already exists. See https://github.com/grafana/grafana/issues/71781 for more information and a reproduction. * add an integration test
This commit is contained in:
@@ -146,8 +146,6 @@ func verifyExistingOrg(sess *DBSession, orgId int64) error {
|
||||
|
||||
func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, error) {
|
||||
var org org.Org
|
||||
org.Created = time.Now()
|
||||
org.Updated = org.Created
|
||||
|
||||
if ss.Cfg.AutoAssignOrg {
|
||||
has, err := sess.Where("id=?", ss.Cfg.AutoAssignOrgId).Get(&org)
|
||||
@@ -167,6 +165,8 @@ func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, erro
|
||||
}
|
||||
|
||||
org.Name = mainOrgName
|
||||
org.Created = time.Now()
|
||||
org.Updated = org.Created
|
||||
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)
|
||||
@@ -178,6 +178,8 @@ func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, erro
|
||||
}
|
||||
} else {
|
||||
org.Name = orgName
|
||||
org.Created = time.Now()
|
||||
org.Updated = org.Created
|
||||
if _, err := sess.InsertOne(&org); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user