File size: 2,690 Bytes
4782147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env tsx
/**
 * Script to clean up ALL test data including seeded users
 * Use this when you want to completely reset the test environment
 *
 * Usage:
 *   npm run cleanup:all-test-data
 *   pnpm cleanup:all-test-data
 */

import { config } from "dotenv";

// Load environment variables FIRST
if (process.env.CI) {
  config({ path: ".env.test" });
} else {
  config();
}

import { sql } from "drizzle-orm";
import { drizzle } from "drizzle-orm/node-postgres";
import { UserTable } from "../src/lib/db/pg/schema.pg";
import { like } from "drizzle-orm";

// Create database connection
const db = drizzle(process.env.POSTGRES_URL!);

async function cleanupAllTestData() {
  console.log("🧹 Cleaning up ALL test data including seeded users...");

  try {
    // Define all test email patterns to completely clean up
    const allTestPatterns = [
      "%@test-seed.local%", // Our main seeded test domain
      "%playwright%", // Dynamically created playwright users
      "%@example.com%", // General test signup users
      "%@temp-test.%", // Temporary test users
      "%testuser%@testuser.com%", // Legacy test users
      "%testuser%@gmail.com%", // Legacy test users
    ];

    console.log("Deleting users matching ALL test patterns...");

    for (const pattern of allTestPatterns) {
      await db.delete(UserTable).where(like(UserTable.email, pattern));
      console.log(`  Deleted users matching pattern: ${pattern}`);
    }

    // Also clean up any remaining legacy test users by exact email match
    const legacyTestEmails = [
      "admin@testuser.com",
      "editor@testuser.com",
      "user@testuser.com",
    ];

    for (let i = 4; i <= 50; i++) {
      legacyTestEmails.push(`testuser${i}@testuser.com`);
      legacyTestEmails.push(`testuser${i}@gmail.com`);
    }

    if (legacyTestEmails.length > 0) {
      console.log("Cleaning up any remaining legacy test emails...");
      for (const email of legacyTestEmails) {
        await db.delete(UserTable).where(sql`email = ${email}`);
      }
    }

    console.log(`✅ Cleanup completed!`);

    // Check remaining user count
    const remainingUsers = await db.$count(UserTable);
    console.log(`📊 Remaining users in database: ${remainingUsers}`);
  } catch (error) {
    console.error("❌ Error during cleanup:", error);
    throw error;
  }
}

// Run the cleanup if this script is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
  cleanupAllTestData()
    .then(() => {
      console.log("🎉 All test data cleanup completed!");
      process.exit(0);
    })
    .catch((error) => {
      console.error("💥 Cleanup failed:", error);
      process.exit(1);
    });
}