fix: recursively clean up MotorCloud account data

This commit is contained in:
Developer 2026-09-03 09:16:14 -04:00
parent 4ac29ab44c
commit cdcbfbf108
1 changed files with 23 additions and 7 deletions

View File

@ -49,15 +49,31 @@ class AccountCloudCleanupService implements CloudAccountCleanup {
if (firestore == null) throw StateError('MotorCloud database is unavailable.');
final userRef = firestore.collection('users').doc(user.uid);
final rides = await userRef.collection('rides').get();
final motorcycles = await userRef.collection('motorcycles').get();
final batch = firestore.batch();
for (final doc in rides.docs) {
batch.delete(doc.reference);
await _deleteCollection(doc.reference.collection('events'));
await _deleteCollection(doc.reference.collection('performance'));
}
for (final doc in motorcycles.docs) {
batch.delete(doc.reference);
await _deleteCollection(userRef.collection('rides'));
await _deleteCollection(userRef.collection('motorcycles'));
await _deleteCollection(userRef.collection('trips'));
await _deleteCollection(userRef.collection('events'));
await _deleteCollection(userRef.collection('friends'));
// Delete the profile last so retries can still discover the account scope.
final profileBatch = firestore.batch()..delete(userRef);
await profileBatch.commit();
}
Future<void> _deleteCollection(CollectionReference<Map<String, dynamic>> collection) async {
final snapshot = await collection.get();
for (var offset = 0; offset < snapshot.docs.length; offset += 450) {
final batch = _firestore!.batch();
final end = (offset + 450).clamp(0, snapshot.docs.length);
for (final doc in snapshot.docs.sublist(offset, end)) {
batch.delete(doc.reference);
}
await batch.commit();
}
batch.delete(userRef);
await batch.commit();
}
}