From e4a877b123f1e3062889a1c6abe552438fdb2a98 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Fri, 16 Dec 2022 16:10:59 +0100 Subject: [PATCH] chore: attempt to fix flapping "organization not found" tests (#60418) we live in hope! --- pkg/services/sqlstore/sqlstore.go | 38 ++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/pkg/services/sqlstore/sqlstore.go b/pkg/services/sqlstore/sqlstore.go index bbe0a39a8c7..712890847a8 100644 --- a/pkg/services/sqlstore/sqlstore.go +++ b/pkg/services/sqlstore/sqlstore.go @@ -159,7 +159,19 @@ func (ss *SQLStore) Reset() error { return nil } - return ss.ensureMainOrgAndAdminUser() + return ss.ensureMainOrgAndAdminUser(false) +} + +// TestReset resets database state. If default org and user creation is enabled, +// it will be ensured they exist in the database. TestReset() is more permissive +// than Reset in that it will create the user and org whether or not there are +// already users in the database. +func (ss *SQLStore) TestReset() error { + if ss.skipEnsureDefaultOrgAndUser { + return nil + } + + return ss.ensureMainOrgAndAdminUser(true) } // Quote quotes the value in the used SQL dialect @@ -187,19 +199,23 @@ func (ss *SQLStore) GetSqlxSession() *session.SessionDB { return ss.sqlxsession } -func (ss *SQLStore) ensureMainOrgAndAdminUser() error { +func (ss *SQLStore) ensureMainOrgAndAdminUser(test bool) error { ctx := context.Background() err := ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error { ss.log.Debug("Ensuring main org and admin user exist") - var stats models.SystemUserCountStats - // TODO: Should be able to rename "Count" to "count", for more standard SQL style - // Just have to make sure it gets deserialized properly into models.SystemUserCountStats - rawSQL := `SELECT COUNT(id) AS Count FROM ` + dialect.Quote("user") - if _, err := sess.SQL(rawSQL).Get(&stats); err != nil { - return fmt.Errorf("could not determine if admin user exists: %w", err) - } - if stats.Count > 0 { - return nil + + // If this is a test database, don't exit early when any user is found. + if !test { + var stats models.SystemUserCountStats + // TODO: Should be able to rename "Count" to "count", for more standard SQL style + // Just have to make sure it gets deserialized properly into models.SystemUserCountStats + rawSQL := `SELECT COUNT(id) AS Count FROM ` + dialect.Quote("user") + if _, err := sess.SQL(rawSQL).Get(&stats); err != nil { + return fmt.Errorf("could not determine if admin user exists: %w", err) + } + if stats.Count > 0 { + return nil + } } // ensure admin user