import json
import config

# Establish database connection
db, cursor = config.gen_connection()

# Load update data from the JSON file
input_file = "update_data.json"
with open(input_file, 'r') as f:
    update_data = json.load(f)

print(f"Loaded {len(update_data)} records to update.")

# Process each record one by one
for count, item in enumerate(update_data, start=1):
    item_id = item["id"]
    file_size = item["file_size"]

    try:
        cursor.execute("UPDATE media SET file_size = %s WHERE id = %s", (file_size, item_id))
        db.commit()
        print(f"Processed {count}/{len(update_data)}: ID {item_id} updated with file size {file_size}.")
    except Exception as e:
        print(f"Error updating ID {item_id}: {e}")
        db.rollback()

print("All updates completed.")
cursor.close()
db.close()