From 3e272d2bdaf116b5a1738fb80388a044547c0683 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Wed, 30 Aug 2023 11:45:20 -0400 Subject: [PATCH] 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 --- pkg/services/sqlstore/user.go | 6 +++-- pkg/services/sqlstore/user_test.go | 40 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 pkg/services/sqlstore/user_test.go diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index fd662fe85a2..591b485482e 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -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 } diff --git a/pkg/services/sqlstore/user_test.go b/pkg/services/sqlstore/user_test.go new file mode 100644 index 00000000000..03131de4665 --- /dev/null +++ b/pkg/services/sqlstore/user_test.go @@ -0,0 +1,40 @@ +package sqlstore + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" +) + +// testing a regression which shows up when the main org is created, but not the +// admin user: getOrCreateOrg was unable to find the existing org. +// https://github.com/grafana/grafana/issues/71781 +func TestIntegrationGetOrCreateOrg(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") + } + ss := InitTestDB(t) + + err := ss.WithNewDbSession(context.Background(), func(sess *DBSession) error { + // Create the org only: + ss.Cfg.AutoAssignOrg = true + ss.Cfg.DisableInitAdminCreation = true + ss.Cfg.AutoAssignOrgId = 1 + createdOrgID, err := ss.getOrCreateOrg(sess, mainOrgName) + require.NoError(t, err) + require.Equal(t, int64(1), createdOrgID) + return nil + }) + require.NoError(t, err) + + err = ss.WithNewDbSession(context.Background(), func(sess *DBSession) error { + // Run it a second time and verify that it finds the org that was + // created above. + gotOrgId, err := ss.getOrCreateOrg(sess, mainOrgName) + require.NoError(t, err) + require.Equal(t, int64(1), gotOrgId) + return nil + }) + require.NoError(t, err) +}