import logging from data.database import Image, ImageStorage, db, db_for_update from app import app from util.migrate import yield_random_entries logger = logging.getLogger(__name__) def backfill_parent_id(): logger.setLevel(logging.DEBUG) logger.debug('backfill_parent_id: Starting') logger.debug('backfill_parent_id: This can be a LONG RUNNING OPERATION. Please wait!') def fetch_batch(): return (Image .select(Image.id, Image.ancestors) .join(ImageStorage) .where(Image.parent_id >> None, Image.ancestors != '/', ImageStorage.uploading == False)) for to_backfill in yield_random_entries(fetch_batch, 10000, 0.3): with app.config['DB_TRANSACTION_FACTORY'](db): try: image = db_for_update(Image .select() .where(Image.id == to_backfill.id)).get() image.parent_id = int(to_backfill.ancestors.split('/')[-2]) image.save() except Image.DoesNotExist: pass logger.debug('backfill_parent_id: Completed') if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) logging.getLogger('peewee').setLevel(logging.CRITICAL) backfill_parent_id()