migration
This commit is contained in:
79
backend/migrate_subscriber_categories.py
Normal file
79
backend/migrate_subscriber_categories.py
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
"""
|
||||||
|
Migration script to update subscriber categories
|
||||||
|
Ensures all subscribers have all available categories if they're missing some
|
||||||
|
"""
|
||||||
|
from pymongo import MongoClient
|
||||||
|
from config import Config
|
||||||
|
from database import subscribers_collection, rss_feeds_collection
|
||||||
|
|
||||||
|
def migrate_subscriber_categories():
|
||||||
|
"""Update all subscribers to have all available categories"""
|
||||||
|
|
||||||
|
print("\n" + "="*70)
|
||||||
|
print("📧 Subscriber Categories Migration")
|
||||||
|
print("="*70)
|
||||||
|
|
||||||
|
# Get all available categories from RSS feeds
|
||||||
|
available_categories = list(rss_feeds_collection.distinct('category'))
|
||||||
|
available_categories.sort()
|
||||||
|
|
||||||
|
print(f"\n✓ Available categories: {available_categories}")
|
||||||
|
|
||||||
|
# Get all subscribers
|
||||||
|
all_subscribers = list(subscribers_collection.find({}))
|
||||||
|
print(f"✓ Found {len(all_subscribers)} total subscribers")
|
||||||
|
|
||||||
|
updated_count = 0
|
||||||
|
no_change_count = 0
|
||||||
|
|
||||||
|
for subscriber in all_subscribers:
|
||||||
|
email = subscriber['email']
|
||||||
|
current_categories = subscriber.get('categories', [])
|
||||||
|
|
||||||
|
# Check if subscriber is missing any categories
|
||||||
|
if not current_categories:
|
||||||
|
# No categories set - give them all
|
||||||
|
print(f"\n {email}: No categories → Adding all {available_categories}")
|
||||||
|
subscribers_collection.update_one(
|
||||||
|
{'email': email},
|
||||||
|
{'$set': {'categories': available_categories}}
|
||||||
|
)
|
||||||
|
updated_count += 1
|
||||||
|
elif set(current_categories) != set(available_categories):
|
||||||
|
# Has some categories but not all
|
||||||
|
missing = set(available_categories) - set(current_categories)
|
||||||
|
print(f"\n {email}: {current_categories} → Adding all {available_categories}")
|
||||||
|
print(f" Missing: {list(missing)}")
|
||||||
|
subscribers_collection.update_one(
|
||||||
|
{'email': email},
|
||||||
|
{'$set': {'categories': available_categories}}
|
||||||
|
)
|
||||||
|
updated_count += 1
|
||||||
|
else:
|
||||||
|
# Already has all categories
|
||||||
|
no_change_count += 1
|
||||||
|
|
||||||
|
print("\n" + "="*70)
|
||||||
|
print("📊 Migration Complete")
|
||||||
|
print("="*70)
|
||||||
|
print(f"✓ Updated: {updated_count} subscribers")
|
||||||
|
print(f"✓ No change needed: {no_change_count} subscribers")
|
||||||
|
print(f"✓ Total: {len(all_subscribers)} subscribers")
|
||||||
|
print("="*70 + "\n")
|
||||||
|
|
||||||
|
return {
|
||||||
|
'total': len(all_subscribers),
|
||||||
|
'updated': updated_count,
|
||||||
|
'no_change': no_change_count
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
try:
|
||||||
|
result = migrate_subscriber_categories()
|
||||||
|
print(f"✅ Migration successful!")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"\n❌ Migration failed: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
Reference in New Issue
Block a user