From 19d989b69681cec34ce0cac700d63c89475f91e5 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Mon, 28 Nov 2022 16:24:26 -0500 Subject: [PATCH] More efficiently filter to unexpired files when migrating https://git.0x0.st/mia/0x0/pulls/72#issuecomment-224 --- migrations/versions/939a08e1d6e5_.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/migrations/versions/939a08e1d6e5_.py b/migrations/versions/939a08e1d6e5_.py index ae70c15..4245a8b 100644 --- a/migrations/versions/939a08e1d6e5_.py +++ b/migrations/versions/939a08e1d6e5_.py @@ -62,7 +62,7 @@ def upgrade(): # List of file hashes which have not expired yet # This could get really big for some servers try: - unexpired_files = set(os.listdir(storage)) + unexpired_files = os.listdir(storage) except FileNotFoundError: return # There are no currently unexpired files @@ -70,20 +70,20 @@ def upgrade(): files = session.scalars( sa.select(File) .where( - sa.not_(File.removed) + sa.not_(File.removed), + File.sha256.in_(unexpired_files) ) ) for file in files: - if file.sha256 in unexpired_files: - file_path = storage / file.sha256 - stat = os.stat(file_path) - max_age = get_max_lifespan(stat.st_size) # How long the file is allowed to live, in ms - file_birth = stat.st_mtime * 1000 # When the file was created, in ms - op.execute( - sa.update(UpdatedFile) - .where(UpdatedFile.c.id == file.id) - .values({'expiration': int(file_birth + max_age)}) - ) + file_path = storage / file.sha256 + stat = os.stat(file_path) + max_age = get_max_lifespan(stat.st_size) # How long the file is allowed to live, in ms + file_birth = stat.st_mtime * 1000 # When the file was created, in ms + op.execute( + sa.update(UpdatedFile) + .where(UpdatedFile.c.id == file.id) + .values({'expiration': int(file_birth + max_age)}) + ) def downgrade(): op.drop_column('file', 'expiration')