better sql query

master
Aevann 2024-10-24 19:35:34 +03:00
parent 41cf3b71e2
commit 655b583eb5
2 changed files with 7 additions and 6 deletions

View File

@ -16,6 +16,8 @@ class Media(Base):
referrer = Column(String)
purged_utc = Column(Integer)
usages = relationship("MediaUsage")
def __init__(self, *args, **kwargs):
if "created_utc" not in kwargs:
kwargs["created_utc"] = int(time.time())
@ -36,7 +38,6 @@ class MediaUsage(Base):
deleted_utc = Column(Integer)
removed_utc = Column(Integer)
media = relationship("Media")
post = relationship("Post", back_populates="media_usages")
comment = relationship("Comment", back_populates="media_usages")

View File

@ -462,18 +462,18 @@ def _cleanup_videos():
clean = [x[0] for x in db.query(MediaUsage.filename).filter_by(deleted_utc=None, removed_utc=None)]
to_delete = db.query(MediaUsage.filename, Media).join(MediaUsage.media).filter(
MediaUsage.filename.notin_(clean),
to_delete = db.query(Media).join(Media.usages).filter(
Media.filename.notin_(clean),
Media.purged_utc == None,
Media.user_id != 380983,
or_(
MediaUsage.deleted_utc < one_month_ago,
MediaUsage.removed_utc < one_month_ago,
),
Media.purged_utc == None,
Media.user_id != 380983,
).order_by(Media.size.desc())
total_saved = 0
for filename, media in to_delete:
for media in to_delete:
total_saved += media.size
print(media.filename, humanize.naturalsize(media.size, binary=True), flush=True)
# finish backing up first before uncommenting