import logging import uuid from data.database import User, configure from app import app logger = logging.getLogger(__name__) def backfill_user_uuids(): logger.setLevel(logging.DEBUG) logger.debug('User UUID Backfill: Began execution') # Make sure we have a reference to the current DB. configure(app.config) logger.debug('User UUID Backfill: Database configured') # Check to see if any users are missing uuids. has_missing_uuids = bool(list(User .select() .where(User.uuid >> None) .limit(1))) if not has_missing_uuids: logger.debug('User UUID Backfill: No migration needed') return logger.debug('User UUID Backfill: Starting migration') while True: batch_users = list(User .select() .where(User.uuid >> None) .limit(100)) if len(batch_users) == 0: # There are no users left to backfill. We're done! logging.debug('User UUID Backfill: Backfill completed') return logging.debug('User UUID Backfill: Found %s records to update' % len(batch_users)) for user in batch_users: user.uuid = str(uuid.uuid4()) user.save() if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) logging.getLogger('boto').setLevel(logging.CRITICAL) logging.getLogger('peewee').setLevel(logging.CRITICAL) backfill_user_uuids()