Spaces:
Running
Running
| """ | |
| Make a user permanent (is_permanent = True). | |
| Uses the app's SQLAlchemy models so it matches the app exactly. Targets by email, | |
| prints the matched record BEFORE updating, and asks for confirmation. | |
| Usage (from backend/): | |
| python scripts/make_user_permanent.py gourivb842@gmail.com | |
| python scripts/make_user_permanent.py gourivb842@gmail.com --yes # skip prompt | |
| Needs the same env as the app (POSTGRES_URI / DATABASE_URL, JWT_SECRET, etc.), | |
| e.g. load your .env first or run with the vars exported. | |
| """ | |
| import argparse | |
| import asyncio | |
| import sys | |
| from pathlib import Path | |
| # Ensure "app" imports work when running this file directly. | |
| BACKEND_ROOT = Path(__file__).resolve().parent.parent | |
| if str(BACKEND_ROOT) not in sys.path: | |
| sys.path.insert(0, str(BACKEND_ROOT)) | |
| from sqlalchemy import select | |
| from app.database import close_db, connect_db, get_session | |
| from app.models import User | |
| async def _run(email: str, assume_yes: bool) -> int: | |
| await connect_db() | |
| try: | |
| async with get_session() as session: | |
| matches = ( | |
| (await session.execute(select(User).where(User.email == email))) | |
| .scalars() | |
| .all() | |
| ) | |
| if not matches: | |
| print(f"No user found with email {email!r}.") | |
| return 1 | |
| if len(matches) > 1: | |
| print(f"WARNING: {len(matches)} records share email {email!r}:") | |
| for i, u in enumerate(matches): | |
| print( | |
| f" [{i}] id={u.id} role={u.role!r} " | |
| f"is_permanent={u.is_permanent!r} supabase_uid={u.supabase_uid!r}" | |
| ) | |
| if len(matches) > 1: | |
| print( | |
| "\nMultiple rows matched. Resolve the duplicate first. " | |
| "Not updating to stay safe." | |
| ) | |
| return 2 | |
| user = matches[0] | |
| if user.is_permanent is True: | |
| print("Already permanent. Nothing to do.") | |
| return 0 | |
| if not assume_yes: | |
| ans = ( | |
| input(f"Set is_permanent=True for {email} (id={user.id})? [y/N] ") | |
| .strip() | |
| .lower() | |
| ) | |
| if ans not in ("y", "yes"): | |
| print("Aborted.") | |
| return 0 | |
| user.is_permanent = True | |
| print(f"{email} is now permanent.") | |
| return 0 | |
| finally: | |
| await close_db() | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Make a user permanent.") | |
| parser.add_argument("email", help="email of the user to make permanent") | |
| parser.add_argument( | |
| "--yes", action="store_true", help="skip the confirmation prompt" | |
| ) | |
| args = parser.parse_args() | |
| sys.exit(asyncio.run(_run(args.email, args.yes))) | |
| if __name__ == "__main__": | |
| main() | |