2014-11-12 22:03:10 +00:00
|
|
|
import logging
|
|
|
|
import uuid
|
|
|
|
|
2014-11-19 20:32:30 +00:00
|
|
|
from data.database import User, db
|
2014-11-12 22:03:10 +00:00
|
|
|
from app import app
|
|
|
|
|
2014-11-18 21:25:11 +00:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
2014-11-12 22:03:10 +00:00
|
|
|
|
|
|
|
def backfill_user_uuids():
|
2014-11-18 21:25:11 +00:00
|
|
|
""" 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.
|
2014-11-19 20:32:30 +00:00
|
|
|
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:
|
2014-11-18 21:25:11 +00:00
|
|
|
LOGGER.debug('User UUID Backfill: No migration needed')
|
2014-11-12 22:03:10 +00:00
|
|
|
return
|
|
|
|
|
2014-11-18 21:25:11 +00:00
|
|
|
LOGGER.debug('User UUID Backfill: Starting migration')
|
2014-11-12 22:03:10 +00:00
|
|
|
while True:
|
2014-11-18 21:25:11 +00:00
|
|
|
batch_user_ids = list(User
|
|
|
|
.select(User.id)
|
|
|
|
.where(User.uuid >> None)
|
|
|
|
.limit(100))
|
2014-11-12 22:03:10 +00:00
|
|
|
|
2014-11-18 21:25:11 +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!
|
2014-11-18 21:25:11 +00:00
|
|
|
LOGGER.debug('User UUID Backfill: Backfill completed')
|
2014-11-12 22:03:10 +00:00
|
|
|
return
|
|
|
|
|
2014-11-18 21:25:11 +00:00
|
|
|
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-19 20:32:30 +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()
|