From 606ad21bec8b81e96c42f33820c94f5884953cdc Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Tue, 18 Nov 2014 16:25:11 -0500 Subject: [PATCH] Apply reviewed changes. Adds a length to the UUID field, renames QuayDeferredPermissionUser parameter id->uuid, adds transactions to backfill script. --- auth/permissions.py | 4 +- data/database.py | 2 +- .../17f11e265e13_add_uuid_field_to_user.py | 2 +- util/backfill_user_uuids.py | 46 +++++++++++-------- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/auth/permissions.py b/auth/permissions.py index eee8d75ff..ae398092d 100644 --- a/auth/permissions.py +++ b/auth/permissions.py @@ -58,8 +58,8 @@ SCOPE_MAX_USER_ROLES.update({ class QuayDeferredPermissionUser(Identity): - def __init__(self, id, auth_type, scopes): - super(QuayDeferredPermissionUser, self).__init__(id, auth_type) + def __init__(self, uuid, auth_type, scopes): + super(QuayDeferredPermissionUser, self).__init__(uuid, auth_type) self._permissions_loaded = False self._scope_set = scopes diff --git a/data/database.py b/data/database.py index 0ccdbf54f..95c0331d7 100644 --- a/data/database.py +++ b/data/database.py @@ -137,7 +137,7 @@ class BaseModel(ReadSlaveModel): class User(BaseModel): - uuid = CharField(default=uuid_generator) + uuid = CharField(default=uuid_generator, max_length=36) username = CharField(unique=True, index=True) password_hash = CharField(null=True) email = CharField(unique=True, index=True, diff --git a/data/migrations/versions/17f11e265e13_add_uuid_field_to_user.py b/data/migrations/versions/17f11e265e13_add_uuid_field_to_user.py index 19b79df5e..7c193e054 100644 --- a/data/migrations/versions/17f11e265e13_add_uuid_field_to_user.py +++ b/data/migrations/versions/17f11e265e13_add_uuid_field_to_user.py @@ -16,7 +16,7 @@ from sqlalchemy.dialects import mysql def upgrade(tables): ### commands auto generated by Alembic - please adjust! ### - op.add_column('user', sa.Column('uuid', sa.String(length=255), nullable=False)) + op.add_column('user', sa.Column('uuid', sa.String(length=36), nullable=False)) ### end Alembic commands ### diff --git a/util/backfill_user_uuids.py b/util/backfill_user_uuids.py index cfc0dcf64..701004a23 100644 --- a/util/backfill_user_uuids.py +++ b/util/backfill_user_uuids.py @@ -1,44 +1,50 @@ import logging import uuid -from data.database import User, configure +from data.database import User, configure, db from app import app -logger = logging.getLogger(__name__) +LOGGER = logging.getLogger(__name__) def backfill_user_uuids(): - logger.setLevel(logging.DEBUG) - logger.debug('User UUID Backfill: Began execution') + """ Generates UUIDs for any Users without them. """ + 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') + 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))) + .select(User.id) + .where(User.uuid >> None) + .limit(1))) if not has_missing_uuids: - logger.debug('User UUID Backfill: No migration needed') + LOGGER.debug('User UUID Backfill: No migration needed') return - logger.debug('User UUID Backfill: Starting migration') + LOGGER.debug('User UUID Backfill: Starting migration') while True: - batch_users = list(User - .select() - .where(User.uuid >> None) - .limit(100)) + batch_user_ids = list(User + .select(User.id) + .where(User.uuid >> None) + .limit(100)) - if len(batch_users) == 0: + if len(batch_user_ids) == 0: # There are no users left to backfill. We're done! - logging.debug('User UUID Backfill: Backfill completed') + LOGGER.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() + 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 if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG)