-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup-firestore.js
More file actions
executable file
·349 lines (287 loc) · 9.73 KB
/
cleanup-firestore.js
File metadata and controls
executable file
·349 lines (287 loc) · 9.73 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env node
/**
* Consolidated Firestore Cleanup Utility
* Combines complete cleanup and duplicate removal functionality
* Usage:
* node cleanup-firestore.js --all # Clean all collections
* node cleanup-firestore.js --duplicates # Remove duplicates only
* node cleanup-firestore.js --collection products # Clean specific collection
*/
const admin = require('firebase-admin');
// Optional axios import - only if available
let axios;
try {
axios = require('axios');
} catch (e) {
console.log('⚠️ axios not installed - vector database cleanup will be skipped');
}
// Initialize Firebase Admin
if (!admin.apps.length) {
const projectId = process.env.FIREBASE_PROJECT_ID;
if (!projectId) {
console.error('❌ FIREBASE_PROJECT_ID environment variable is required');
console.error('💡 Set it in your .env file or environment');
process.exit(1);
}
admin.initializeApp({
projectId: projectId
});
}
const db = admin.firestore();
// Vector database configuration (optional)
const QDRANT_URL = process.env.QDRANT_URL;
const QDRANT_API_KEY = process.env.QDRANT_API_KEY;
// Default collections that can be cleaned
const DEFAULT_COLLECTIONS = [
'processing_queue',
'products',
'brands',
'pipeline_progress',
'system',
'issues',
'scores',
'sustainability_data',
'legal_compliance_data',
'product_images',
'unknown_products',
'categories',
'product_categories',
'extraction_results',
'enrichment_results'
];
/**
* Clean all documents from specified collections
*/
async function cleanAllCollections(collections = DEFAULT_COLLECTIONS) {
console.log('🧹 COMPLETE DATABASE CLEANUP - This will delete ALL data!');
console.log('⚠️ WARNING: This is a destructive operation!');
let totalDeleted = 0;
for (const collectionName of collections) {
try {
console.log(`📂 Cleaning collection: ${collectionName}`);
const collectionRef = db.collection(collectionName);
const snapshot = await collectionRef.get();
if (snapshot.empty) {
console.log(` ✅ ${collectionName} is already empty`);
continue;
}
// Delete in batches to avoid timeout
const batchSize = 100;
let deletedInCollection = 0;
while (true) {
const batch = db.batch();
const docs = await collectionRef.limit(batchSize).get();
if (docs.empty) break;
docs.forEach(doc => {
batch.delete(doc.ref);
});
await batch.commit();
deletedInCollection += docs.size;
totalDeleted += docs.size;
console.log(` 🗑️ Deleted ${deletedInCollection} documents from ${collectionName}`);
if (docs.size < batchSize) break;
}
} catch (error) {
console.error(`❌ Error cleaning ${collectionName}:`, error.message);
}
}
// Clean vector database if configured
if (QDRANT_URL) {
await cleanQdrantCollections();
}
console.log(`✅ Cleanup complete! Total documents deleted: ${totalDeleted}`);
}
/**
* Remove duplicate documents from collections
*/
async function removeDuplicates(collections = DEFAULT_COLLECTIONS) {
console.log('🔍 Finding and removing duplicate documents...');
let totalDuplicatesRemoved = 0;
for (const collectionName of collections) {
try {
console.log(`📂 Checking for duplicates in: ${collectionName}`);
const collectionRef = db.collection(collectionName);
const snapshot = await collectionRef.get();
if (snapshot.empty) {
console.log(` ✅ ${collectionName} is empty`);
continue;
}
// Group documents by content hash or key fields
const documentGroups = new Map();
snapshot.forEach(doc => {
const data = doc.data();
// Create a hash based on key fields (customize per collection)
let hashKey;
if (collectionName === 'products' && data.name && data.brand_id) {
hashKey = `${data.name}_${data.brand_id}`;
} else if (collectionName === 'brands' && data.name) {
hashKey = data.name;
} else if (data.id) {
hashKey = data.id;
} else {
// Use a hash of the entire document for generic deduplication
hashKey = JSON.stringify(data);
}
if (!documentGroups.has(hashKey)) {
documentGroups.set(hashKey, []);
}
documentGroups.get(hashKey).push({ id: doc.id, data });
});
// Remove duplicates (keep the first, delete the rest)
let duplicatesInCollection = 0;
const batch = db.batch();
let batchOps = 0;
for (const [hashKey, docs] of documentGroups) {
if (docs.length > 1) {
// Keep the first document, delete the rest
for (let i = 1; i < docs.length; i++) {
batch.delete(collectionRef.doc(docs[i].id));
batchOps++;
duplicatesInCollection++;
// Commit batch if it gets too large
if (batchOps >= 400) {
await batch.commit();
batchOps = 0;
}
}
console.log(` 🔍 Found ${docs.length - 1} duplicates for key: ${hashKey.substring(0, 50)}...`);
}
}
// Commit any remaining operations
if (batchOps > 0) {
await batch.commit();
}
totalDuplicatesRemoved += duplicatesInCollection;
console.log(` ✅ Removed ${duplicatesInCollection} duplicates from ${collectionName}`);
} catch (error) {
console.error(`❌ Error removing duplicates from ${collectionName}:`, error.message);
}
}
console.log(`✅ Duplicate removal complete! Total duplicates removed: ${totalDuplicatesRemoved}`);
}
/**
* Clean specific collection
*/
async function cleanCollection(collectionName) {
console.log(`🧹 Cleaning specific collection: ${collectionName}`);
await cleanAllCollections([collectionName]);
}
/**
* Clean Qdrant vector database collections
*/
async function cleanQdrantCollections() {
if (!QDRANT_URL) {
console.log('⚠️ Qdrant URL not configured, skipping vector cleanup');
return;
}
if (!axios) {
console.log('⚠️ axios not available, skipping vector cleanup');
return;
}
console.log('🧹 Cleaning Qdrant vector database...');
const collections = ['products', 'categories', 'embeddings'];
for (const collection of collections) {
try {
const headers = {};
if (QDRANT_API_KEY) {
headers['api-key'] = QDRANT_API_KEY;
}
await axios.delete(`${QDRANT_URL}/collections/${collection}`, { headers });
console.log(` ✅ Deleted Qdrant collection: ${collection}`);
} catch (error) {
if (error.response?.status === 404) {
console.log(` ✅ Qdrant collection ${collection} doesn't exist`);
} else {
console.error(` ❌ Error deleting Qdrant collection ${collection}:`, error.message);
}
}
}
}
/**
* Show usage information
*/
function showUsage() {
console.log('🧹 Consolidated Firestore Cleanup Utility');
console.log('');
console.log('Usage:');
console.log(' node cleanup-firestore.js --all # Clean all collections');
console.log(' node cleanup-firestore.js --duplicates # Remove duplicates only');
console.log(' node cleanup-firestore.js --collection products # Clean specific collection');
console.log(' node cleanup-firestore.js --qdrant # Clean vector database only');
console.log(' node cleanup-firestore.js --help # Show this help');
console.log('');
console.log('Environment Variables:');
console.log(' FIREBASE_PROJECT_ID Required - Firebase project ID');
console.log(' QDRANT_URL Optional - Qdrant vector database URL');
console.log(' QDRANT_API_KEY Optional - Qdrant API key');
console.log('');
console.log('Examples:');
console.log(' FIREBASE_PROJECT_ID=my-project node cleanup-firestore.js --all');
console.log(' node cleanup-firestore.js --collection products --collection brands');
}
/**
* Main execution
*/
async function main() {
const args = process.argv.slice(2);
if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
showUsage();
return;
}
try {
if (args.includes('--all')) {
// Get custom collections if specified
const customCollections = [];
for (let i = 0; i < args.length; i++) {
if (args[i] === '--collection' && args[i + 1]) {
customCollections.push(args[i + 1]);
}
}
await cleanAllCollections(customCollections.length > 0 ? customCollections : DEFAULT_COLLECTIONS);
} else if (args.includes('--duplicates')) {
await removeDuplicates();
} else if (args.includes('--qdrant')) {
await cleanQdrantCollections();
} else if (args.includes('--collection')) {
const collections = [];
for (let i = 0; i < args.length; i++) {
if (args[i] === '--collection' && args[i + 1]) {
collections.push(args[i + 1]);
}
}
if (collections.length === 0) {
console.error('❌ No collection specified');
showUsage();
return;
}
for (const collection of collections) {
await cleanCollection(collection);
}
} else {
console.error('❌ Invalid arguments');
showUsage();
}
} catch (error) {
console.error('❌ Cleanup failed:', error.message);
process.exit(1);
}
}
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('\n🛑 Cleanup interrupted by user');
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('\n🛑 Cleanup terminated');
process.exit(0);
});
// Run the script
if (require.main === module) {
main();
}
module.exports = {
cleanAllCollections,
removeDuplicates,
cleanCollection,
cleanQdrantCollections
};