Spaces:
Sleeping
Sleeping
Download scripts/nuke_supabase.py from Abrar144/stylesense-backend: direct link, hf CLI and curl.
- Browser
- Download file 2.46 kB
-
https://huggingface.co/spaces/Abrar144/stylesense-backend/resolve/main/scripts/nuke_supabase.py
- Command line
-
hf download hf://spaces/Abrar144/stylesense-backend/scripts/nuke_supabase.py
-
curl -L -o nuke_supabase.py https://huggingface.co/spaces/Abrar144/stylesense-backend/resolve/main/scripts/nuke_supabase.py
2.46 kB
| """ | |
| 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() | |