From 297c8ad29c25ee1785e785458a96131dbba5d6d3 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Mon, 22 Sep 2014 15:04:28 -0400 Subject: [PATCH] Add migration to backfill uncompressed image sizes on the storage --- ...fcf_add_the_uncompressed_size_to_image_.py | 29 +++++++++++++++++ tools/uncompressedsize.py | 31 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 data/migrations/versions/6f2ecf5afcf_add_the_uncompressed_size_to_image_.py create mode 100644 tools/uncompressedsize.py diff --git a/data/migrations/versions/6f2ecf5afcf_add_the_uncompressed_size_to_image_.py b/data/migrations/versions/6f2ecf5afcf_add_the_uncompressed_size_to_image_.py new file mode 100644 index 000000000..1081df651 --- /dev/null +++ b/data/migrations/versions/6f2ecf5afcf_add_the_uncompressed_size_to_image_.py @@ -0,0 +1,29 @@ +"""add the uncompressed size to image storage + +Revision ID: 6f2ecf5afcf +Revises: 3f6d26399bd2 +Create Date: 2014-09-22 14:39:13.470566 + +""" + +# revision identifiers, used by Alembic. +revision = '6f2ecf5afcf' +down_revision = '3f6d26399bd2' + +from alembic import op +from tools.uncompressedsize import backfill_sizes +import sqlalchemy as sa + + +def upgrade(tables): + ### commands auto generated by Alembic - please adjust! ### + op.add_column('imagestorage', sa.Column('uncompressed_size', sa.BigInteger(), nullable=True)) + ### end Alembic commands ### + + # Backfill the uncompressed size to the image storage table. + backfill_sizes() + +def downgrade(tables): + ### commands auto generated by Alembic - please adjust! ### + op.drop_column('imagestorage', 'uncompressed_size') + ### end Alembic commands ### diff --git a/tools/uncompressedsize.py b/tools/uncompressedsize.py new file mode 100644 index 000000000..530ba3836 --- /dev/null +++ b/tools/uncompressedsize.py @@ -0,0 +1,31 @@ +from data import model +from data.database import ImageStorage +from app import app, storage as store + +import logging + +def backfill_sizes(): + count = ImageStorage.select().where(ImageStorage.uncompressed_size == None).count() + counter = 0 + for image_storage in ImageStorage.select().where(ImageStorage.uncompressed_size == None): + logging.debug("Backfilling uncompressed size: %s of %s" % (counter, count)) + + # Lookup the JSON for the image. + uuid = image_storage.uuid + with_locations = model.get_storage_by_uuid(uuid) + + json_data = store.get_content(with_locations.locations, store.image_json_path(uuid)) + size = json_data.get('Size', None) + if size is None: + continue + + image_storage.uncompressed_size = size + image_storage.save() + counter += 1 + + +if __name__ == "__main__": + logging.basicConfig(level=logging.DEBUG) + logging.getLogger('boto').setLevel(logging.CRITICAL) + + backfill_sizes() \ No newline at end of file