Coalesce updates to the database during migration

#72 (comment)
This commit is contained in:
Emi Simpson 2022-11-28 16:28:22 -05:00
parent 19d989b696
commit 55ee3740b0
No known key found for this signature in database
GPG key ID: 45E9C6E81BD86E7C

View file

@ -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')