This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/util/backfill_user_uuids.py

55 lines
1.5 KiB
Python
Raw Normal View History

2014-11-12 22:03:10 +00:00
import logging
import uuid
from data.database import User, db
2014-11-12 22:03:10 +00:00
from app import app
LOGGER = logging.getLogger(__name__)
2014-11-12 22:03:10 +00:00
def backfill_user_uuids():
""" Generates UUIDs for any Users without them. """
LOGGER.setLevel(logging.DEBUG)
LOGGER.debug('User UUID Backfill: Began execution')
2014-11-12 22:03:10 +00:00
# Check to see if any users are missing uuids.
has_missing_uuids = True
try:
User.select().where(User.uuid >> None).get()
except User.DoesNotExist:
has_missing_uuids = False
2014-11-12 22:03:10 +00:00
if not has_missing_uuids:
LOGGER.debug('User UUID Backfill: No migration needed')
2014-11-12 22:03:10 +00:00
return
LOGGER.debug('User UUID Backfill: Starting migration')
2014-11-12 22:03:10 +00:00
while True:
batch_user_ids = list(User
.select(User.id)
.where(User.uuid >> None)
.limit(100))
2014-11-12 22:03:10 +00:00
if len(batch_user_ids) == 0:
2014-11-12 22:03:10 +00:00
# There are no users left to backfill. We're done!
LOGGER.debug('User UUID Backfill: Backfill completed')
2014-11-12 22:03:10 +00:00
return
LOGGER.debug('User UUID Backfill: Found %s records to update', len(batch_user_ids))
for user_id in batch_user_ids:
with app.config['DB_TRANSACTION_FACTORY'](db):
try:
user = User.get(User.id == user_id)
user.uuid = str(uuid.uuid4())
user.save()
except User.DoesNotExist:
pass
2014-11-12 22:03:10 +00:00
2014-11-12 22:03:10 +00:00
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logging.getLogger('boto').setLevel(logging.CRITICAL)
logging.getLogger('peewee').setLevel(logging.CRITICAL)
backfill_user_uuids()