From 10b627c2ad83fc706d4376297fb41cb79adf818d Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Wed, 12 Nov 2014 17:03:10 -0500 Subject: [PATCH] Add user uuid backfill --- util/backfill_user_uuids.py | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 util/backfill_user_uuids.py diff --git a/util/backfill_user_uuids.py b/util/backfill_user_uuids.py new file mode 100644 index 000000000..cfc0dcf64 --- /dev/null +++ b/util/backfill_user_uuids.py @@ -0,0 +1,48 @@ +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()