From 55ee3740b0d39144c4cad7896c8b386d89d40c15 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Mon, 28 Nov 2022 16:28:22 -0500 Subject: [PATCH] Coalesce updates to the database during migration https://git.0x0.st/mia/0x0/pulls/72#issuecomment-226 --- migrations/versions/939a08e1d6e5_.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/migrations/versions/939a08e1d6e5_.py b/migrations/versions/939a08e1d6e5_.py index 4245a8b..004d077 100644 --- a/migrations/versions/939a08e1d6e5_.py +++ b/migrations/versions/939a08e1d6e5_.py @@ -74,16 +74,17 @@ def upgrade(): File.sha256.in_(unexpired_files) ) ) + updates = [] # We coalesce updates to the database here for file in 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)}) - ) + updates.append({'id': file.id, 'expiration': int(file_birth + max_age)}) + + # Apply coalesced updates + session.bulk_update_mappings(File, updates) + session.commit() def downgrade(): op.drop_column('file', 'expiration')