From d73cf5545c02c8d4282b1d7d4e974a2eabbf893c Mon Sep 17 00:00:00 2001 From: Ryan Chen Date: Fri, 3 Apr 2026 10:27:40 -0400 Subject: [PATCH] Add transfer-questions CLI command for bulk ownership reassignment Co-Authored-By: Claude Opus 4.6 --- backend/app.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/backend/app.py b/backend/app.py index 6444b96..8a2cfe1 100644 --- a/backend/app.py +++ b/backend/app.py @@ -1,4 +1,5 @@ import os +import click from flask import Flask, send_from_directory from flask_cors import CORS from flask_migrate import Migrate @@ -87,4 +88,35 @@ def create_app(config_name=None): """Health check endpoint""" return {'status': 'ok', 'message': 'Trivia Game API is running'}, 200 + @app.cli.command('transfer-questions') + @click.argument('username') + def transfer_questions(username): + """Transfer ownership of ALL questions to the specified user. + + USERNAME is the preferred_username of the target user. + """ + from backend.models import User, Question + + user = User.query.filter_by(preferred_username=username).first() + if not user: + click.echo(f"Error: No user found with username '{username}'") + click.echo("Available users:") + for u in User.query.all(): + click.echo(f" - {u.preferred_username} (id={u.id}, name={u.name})") + raise SystemExit(1) + + count = Question.query.count() + if count == 0: + click.echo("No questions found in the database.") + return + + click.echo(f"This will transfer ownership of {count} question(s) to '{username}' (id={user.id}).") + if not click.confirm("Proceed?"): + click.echo("Aborted.") + return + + Question.query.update({Question.created_by: user.id}) + db.session.commit() + click.echo(f"Successfully transferred {count} question(s) to '{username}'.") + return app