Skip to content

Commit 9006441

Browse files
Copilotbitnimble
andcommitted
refactor: create Supabase admin client once at module level, delete auth users concurrently
Co-authored-by: bitnimble <4076797+bitnimble@users.noreply.github.com>
1 parent a57ba7e commit 9006441

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/services/jest_global_setup.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ export default async function globalSetup() {
1616
const pool = new Pool();
1717

1818
// Verify tables exist (they should from supabase start + migrations)
19+
const expectedTables = ['maps', 'users', 'favorites', 'difficulties'];
1920
const result = await pool.query(`
2021
SELECT table_name FROM information_schema.tables
21-
WHERE table_schema = 'public' AND table_name IN ('maps', 'users', 'favorites', 'difficulties')
22-
`);
23-
if (result.rows.length < 4) {
22+
WHERE table_schema = 'public' AND table_name = ANY($1)
23+
`, [expectedTables]);
24+
if (result.rows.length < expectedTables.length) {
2425
throw new Error(
2526
'Expected database tables not found. Make sure `supabase start` has been run ' +
2627
'and migrations have been applied.'

src/services/jest_setup.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@ import { getServerContext } from 'services/server_context';
77
const projectDir = process.cwd();
88
loadEnvConfig(projectDir);
99

10-
async function deleteAllAuthUsers() {
11-
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!;
12-
const supabaseServiceKey = process.env.SUPABASE_SECRET_KEY!;
13-
const supabase = createClient(supabaseUrl, supabaseServiceKey);
10+
const supabaseAdmin = createClient(
11+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
12+
process.env.SUPABASE_SECRET_KEY!
13+
);
1414

15-
const { data, error } = await supabase.auth.admin.listUsers();
15+
async function deleteAllAuthUsers() {
16+
const { data, error } = await supabaseAdmin.auth.admin.listUsers();
1617
if (error) {
1718
throw new Error(`Failed to list auth users: ${error.message}`);
1819
}
19-
for (const user of data.users) {
20-
const { error: deleteError } = await supabase.auth.admin.deleteUser(user.id);
21-
if (deleteError) {
22-
throw new Error(`Failed to delete auth user ${user.id}: ${deleteError.message}`);
23-
}
24-
}
20+
await Promise.all(
21+
data.users.map(async (user) => {
22+
const { error: deleteError } = await supabaseAdmin.auth.admin.deleteUser(user.id);
23+
if (deleteError) {
24+
throw new Error(`Failed to delete auth user ${user.id}: ${deleteError.message}`);
25+
}
26+
})
27+
);
2528
}
2629

2730
async function resetTestData() {

0 commit comments

Comments
 (0)