Spaces:
Running
Running
File size: 2,561 Bytes
d8cd523 | 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 77 78 79 80 81 82 83 84 85 | """One-off: delete videos with status='failed' and related rows."""
from dotenv import load_dotenv
load_dotenv()
from sqlalchemy import create_engine, text, inspect
from app.core.config import DATABASE_URL
engine = create_engine(DATABASE_URL)
CANDIDATE_TABLES = [
"views",
"likes",
"comments",
"challenge_entries",
"reposts",
"watch_later",
"video_interactions",
"content_reports",
"watch_history",
"library_watch_later",
"liked_videos",
]
def main():
insp = inspect(engine)
with engine.connect() as c:
rows = c.execute(
text("SELECT status, COUNT(*) AS n FROM videos GROUP BY status ORDER BY n DESC")
).fetchall()
print("STATUS COUNTS:")
for r in rows:
print(f" {r[0]!r}: {r[1]}")
failed_total = c.execute(
text("SELECT COUNT(*) FROM videos WHERE status = 'failed'")
).scalar()
print(f"FAILED TOTAL: {failed_total}")
if not failed_total:
return
with engine.begin() as c:
pinned = c.execute(
text(
"UPDATE users SET pinned_video_id = NULL "
"WHERE pinned_video_id IN (SELECT id FROM videos WHERE status = 'failed')"
)
)
print(f"Cleared pinned_video_id on users: {pinned.rowcount}")
for table in CANDIDATE_TABLES:
if not insp.has_table(table):
print(f"Skip {table}: table missing")
continue
cols = {col["name"] for col in insp.get_columns(table)}
if "video_id" not in cols:
print(f"Skip {table}: no video_id column")
continue
res = c.execute(
text(
f"DELETE FROM {table} WHERE video_id IN "
f"(SELECT id FROM videos WHERE status = 'failed')"
)
)
print(f"Deleted from {table}: {res.rowcount}")
deleted = c.execute(text("DELETE FROM videos WHERE status = 'failed'"))
print(f"Deleted failed videos: {deleted.rowcount}")
with engine.connect() as c:
left = c.execute(text("SELECT COUNT(*) FROM videos WHERE status = 'failed'")).scalar()
print(f"Remaining failed: {left}")
rows = c.execute(
text("SELECT status, COUNT(*) AS n FROM videos GROUP BY status ORDER BY n DESC")
).fetchall()
print("STATUS COUNTS AFTER:")
for r in rows:
print(f" {r[0]!r}: {r[1]}")
if __name__ == "__main__":
main()
|