From 2034d96c9ed6aa7b963332e1e5226a132349918b Mon Sep 17 00:00:00 2001 From: Dongho Kim Date: Thu, 20 Nov 2025 10:27:30 +0100 Subject: [PATCH] migration --- backend/migrate_subscriber_categories.py | 79 ++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 backend/migrate_subscriber_categories.py diff --git a/backend/migrate_subscriber_categories.py b/backend/migrate_subscriber_categories.py new file mode 100644 index 0000000..d35b45a --- /dev/null +++ b/backend/migrate_subscriber_categories.py @@ -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()