File size: 2,460 Bytes
89b7f5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
STEP 0 β€” Nuke Supabase
Wipes ALL data: orders, product_embeddings, products, categories tables,
and deletes ALL files from the product-images storage bucket.
Run this FIRST before re-seeding.

Usage:
    cd backend
    .venv/Scripts/python scripts/nuke_supabase.py
"""

import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from dotenv import load_dotenv
load_dotenv()

from supabase import create_client

SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
BUCKET       = os.getenv("SUPABASE_STORAGE_BUCKET", "product-images")

supabase = create_client(SUPABASE_URL, SUPABASE_KEY)


def main():
    print("=" * 60)
    print("  STEP 0 β€” Nuke Supabase (Full Clean Slate)")
    print("=" * 60)

    # ── 1. Delete all table rows (order matters: FK constraints) ──
    print("\nπŸ—‘οΈ  Deleting all table rows...")

    tables_in_order = ["orders", "product_embeddings", "products", "categories"]
    for table in tables_in_order:
        try:
            result = supabase.table(table).delete().neq("id", -1).execute()
            count = len(result.data) if result.data else 0
            print(f"  βœ… {table}: deleted {count} rows")
        except Exception as e:
            print(f"  ⚠️  {table}: {e}")

    # ── 2. Delete all files from storage bucket ──
    print(f"\nπŸ—‘οΈ  Deleting all files from storage bucket '{BUCKET}'...")

    try:
        files = supabase.storage.from_(BUCKET).list("products", {"limit": 1000})
        if files:
            file_paths = [f"products/{f['name']}" for f in files]
            print(f"  Found {len(file_paths)} files to delete...")

            # Supabase delete accepts batches
            batch_size = 50
            deleted = 0
            for i in range(0, len(file_paths), batch_size):
                batch = file_paths[i:i + batch_size]
                supabase.storage.from_(BUCKET).remove(batch)
                deleted += len(batch)
                print(f"  Deleted batch: {deleted}/{len(file_paths)}")

            print(f"  βœ… Deleted {deleted} files from storage")
        else:
            print("  βœ… Storage already empty")
    except Exception as e:
        print(f"  ⚠️  Storage cleanup: {e}")

    print("\n" + "=" * 60)
    print("  βœ… NUKE COMPLETE β€” Supabase is clean")
    print("=" * 60)
    print("  Next: run scripts/seed_database.py")


if __name__ == "__main__":
    main()