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:
|
2015-02-13 19:41:08 +00:00
|
|
|
User.select(User.id).where(User.uuid >> None).get()
|
2014-11-19 20:32:30 +00:00
|
|
|
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:
|
2015-02-13 19:41:08 +00:00
|
|
|
user = User.select(User.id, User.uuid).where(User.id == user_id).get()
|
2014-11-18 21:25:11 +00:00
|
|
|
user.uuid = str(uuid.uuid4())
|
2015-02-13 19:41:08 +00:00
|
|
|
user.save(only=[User.uuid])
|
2014-11-18 21:25:11 +00:00
|
|
|
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()
|