Files
moyosapp_beta.0.0.3.3_beta1/scripts/cleanup-redis-test.ts
2026-01-16 19:04:48 +02:00

96 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env tsx
/**
* Clean up test data from Redis
*/
import { Redis } from "ioredis";
async function cleanupRedis() {
console.log("🧹 Cleaning up Redis test data...\n");
// Load environment variables
if (process.env.NODE_ENV !== "production") {
try {
const { config } = await import("dotenv");
config({ path: ".env.local" });
} catch (e) {
// dotenv not available
}
}
const redisUrl = process.env.REDIS_URL;
if (!redisUrl) {
console.error("❌ REDIS_URL not set");
process.exit(1);
}
const redis = new Redis(redisUrl);
try {
// Get all keys
const allKeys = await redis.keys("*");
console.log(`📊 Found ${allKeys.length} keys in Redis\n`);
// Patterns to delete
const patternsToDelete = [
"test:*", // All test keys
"rate_limit:test:*", // Test rate limit keys
"app:version", // App version
"app:environment", // App environment
"app:last_deploy", // Last deploy
"app:config", // App config hash
"app:recent_actions", // Recent actions list
"app:active_sessions", // Active sessions set
"app:metrics:*", // Metrics time-series
];
let deletedCount = 0;
for (const pattern of patternsToDelete) {
if (pattern.includes("*")) {
// Pattern matching
const keys = await redis.keys(pattern);
if (keys.length > 0) {
await redis.del(...keys);
console.log(`✅ Deleted ${keys.length} keys matching: ${pattern}`);
deletedCount += keys.length;
}
} else {
// Exact key
const exists = await redis.exists(pattern);
if (exists) {
await redis.del(pattern);
console.log(`✅ Deleted: ${pattern}`);
deletedCount++;
}
}
}
// Check for any remaining test keys
const remainingTestKeys = await redis.keys("test:*");
const remainingRateLimitTestKeys = await redis.keys("rate_limit:test:*");
if (remainingTestKeys.length > 0 || remainingRateLimitTestKeys.length > 0) {
console.log(`\n⚠ Found ${remainingTestKeys.length + remainingRateLimitTestKeys.length} remaining test keys`);
const allRemaining = [...remainingTestKeys, ...remainingRateLimitTestKeys];
await redis.del(...allRemaining);
console.log(`✅ Cleaned up remaining test keys`);
deletedCount += allRemaining.length;
}
const finalCount = await redis.dbsize();
console.log("\n" + "=".repeat(50));
console.log(`✅ Deleted ${deletedCount} test keys`);
console.log(`📊 Remaining keys in Redis: ${finalCount}`);
console.log("=".repeat(50) + "\n");
await redis.quit();
} catch (error) {
console.error("❌ Error:", error instanceof Error ? error.message : String(error));
await redis.quit();
process.exit(1);
}
}
cleanupRedis();