@@ -15,23 +15,26 @@ import (
1515 "github.com/stretchr/testify/require"
1616 "github.com/stretchr/testify/suite"
1717 "github.com/supabase/auth/internal/conf"
18+ "github.com/supabase/auth/internal/models"
1819 "github.com/supabase/auth/internal/storage"
1920)
2021
2122type IndexWorkerTestSuite struct {
2223 suite.Suite
23- config * conf.GlobalConfiguration
24- db * storage.Connection
25- popDB * pop.Connection
26- namespace string
27- logger * logrus.Entry
24+ config * conf.GlobalConfiguration
25+ db * storage.Connection
26+ popDB * pop.Connection
27+ namespace string
28+ logger * logrus.Entry
29+ maxUsersThreshold int64
2830}
2931
3032func (ts * IndexWorkerTestSuite ) SetupSuite () {
3133 // Load test configuration
3234 config , err := conf .LoadGlobal ("../../hack/test.env" )
3335 require .NoError (ts .T (), err )
3436 ts .config = config
37+ ts .maxUsersThreshold = config .IndexWorker .MaxUsersThreshold
3538 ts .namespace = config .DB .Namespace
3639 ts .logger = logrus .NewEntry (logrus .New ())
3740 ts .logger .Logger .SetLevel (logrus .DebugLevel )
@@ -69,8 +72,9 @@ func (ts *IndexWorkerTestSuite) TearDownSuite() {
6972}
7073
7174func (ts * IndexWorkerTestSuite ) SetupTest () {
72- // Clean up before each test
75+ models . TruncateAll ( ts . db )
7376 ts .cleanupIndexes ()
77+ ts .config .IndexWorker .MaxUsersThreshold = ts .maxUsersThreshold
7478}
7579
7680func (ts * IndexWorkerTestSuite ) cleanupIndexes () {
@@ -282,6 +286,55 @@ func getIndexNames(indexes []struct {
282286 return names
283287}
284288
289+ func (ts * IndexWorkerTestSuite ) TestMaxUsersThresholdSkipsIndexCreation () {
290+ ctx := context .Background ()
291+
292+ // SetupTest already called TruncateAll, so the users table is empty.
293+ // Insert test users so the approximate count exceeds the threshold.
294+ for i := 0 ; i < 5 ; i ++ {
295+ insertQuery := fmt .Sprintf (
296+ `INSERT INTO %q.users (instance_id, id, aud, role, email, encrypted_password, created_at, updated_at) VALUES ('00000000-0000-0000-0000-000000000000', gen_random_uuid(), 'authenticated', 'authenticated', 'threshold_test_%d@example.com', '', now(), now())` ,
297+ ts .namespace , i ,
298+ )
299+ require .NoError (ts .T (), ts .db .RawQuery (insertQuery ).Exec ())
300+ }
301+ // Update pg_class.reltuples so getApproximateUserCount reflects the inserts
302+ analyzeQuery := fmt .Sprintf (`ANALYZE %q.users` , ts .namespace )
303+ require .NoError (ts .T (), ts .db .RawQuery (analyzeQuery ).Exec ())
304+
305+ ts .config .IndexWorker .MaxUsersThreshold = 1
306+
307+ indexes := getUsersIndexes (ts .namespace )
308+ existingIndexes , err := getIndexStatuses (ts .popDB , ts .namespace , getIndexNames (indexes ))
309+ require .NoError (ts .T (), err )
310+ assert .Empty (ts .T (), existingIndexes , "No indexes should exist before the test" )
311+
312+ err = CreateIndexes (ctx , ts .config , ts .logger )
313+ require .NoError (ts .T (), err )
314+
315+ existingIndexes , err = getIndexStatuses (ts .popDB , ts .namespace , getIndexNames (indexes ))
316+ require .NoError (ts .T (), err )
317+ assert .Empty (ts .T (), existingIndexes , "No indexes should be created when user count exceeds threshold" )
318+ }
319+
320+ func (ts * IndexWorkerTestSuite ) TestMaxUsersThresholdZeroCreatesIndexes () {
321+ ctx := context .Background ()
322+
323+ ts .config .IndexWorker .MaxUsersThreshold = 0
324+
325+ err := CreateIndexes (ctx , ts .config , ts .logger )
326+ require .NoError (ts .T (), err )
327+
328+ indexes := getUsersIndexes (ts .namespace )
329+ existingIndexes , err := getIndexStatuses (ts .popDB , ts .namespace , getIndexNames (indexes ))
330+ require .NoError (ts .T (), err )
331+ assert .Equal (ts .T (), len (indexes ), len (existingIndexes ), "All indexes should be created when threshold is 0 (no limit)" )
332+ for _ , idx := range existingIndexes {
333+ assert .True (ts .T (), idx .IsValid , "Index %s should be valid" , idx .IndexName )
334+ assert .True (ts .T (), idx .IsReady , "Index %s should be ready" , idx .IndexName )
335+ }
336+ }
337+
285338// TestCreateIndexesWithInvalidIndexes tests that CreateIndexes can recover from invalid indexes
286339// This test simulates a scenario where indexes become invalid (e.g., from interrupted CONCURRENT creation)
287340// and verifies that CreateIndexes properly handles them by dropping and recreating.
0 commit comments