Apply reviewed changes.

Adds a length to the UUID field, renames QuayDeferredPermissionUser
parameter id->uuid, adds transactions to backfill script.
This commit is contained in:
Jimmy Zelinskie 2014-11-18 16:25:11 -05:00
parent 10b627c2ad
commit 606ad21bec
4 changed files with 30 additions and 24 deletions

View file

@ -58,8 +58,8 @@ SCOPE_MAX_USER_ROLES.update({
class QuayDeferredPermissionUser(Identity): class QuayDeferredPermissionUser(Identity):
def __init__(self, id, auth_type, scopes): def __init__(self, uuid, auth_type, scopes):
super(QuayDeferredPermissionUser, self).__init__(id, auth_type) super(QuayDeferredPermissionUser, self).__init__(uuid, auth_type)
self._permissions_loaded = False self._permissions_loaded = False
self._scope_set = scopes self._scope_set = scopes

View file

@ -137,7 +137,7 @@ class BaseModel(ReadSlaveModel):
class User(BaseModel): class User(BaseModel):
uuid = CharField(default=uuid_generator) uuid = CharField(default=uuid_generator, max_length=36)
username = CharField(unique=True, index=True) username = CharField(unique=True, index=True)
password_hash = CharField(null=True) password_hash = CharField(null=True)
email = CharField(unique=True, index=True, email = CharField(unique=True, index=True,

View file

@ -16,7 +16,7 @@ from sqlalchemy.dialects import mysql
def upgrade(tables): def upgrade(tables):
### commands auto generated by Alembic - please adjust! ### ### 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 ### ### end Alembic commands ###

View file

@ -1,44 +1,50 @@
import logging import logging
import uuid import uuid
from data.database import User, configure from data.database import User, configure, db
from app import app from app import app
logger = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
def backfill_user_uuids(): def backfill_user_uuids():
logger.setLevel(logging.DEBUG) """ Generates UUIDs for any Users without them. """
logger.debug('User UUID Backfill: Began execution') LOGGER.setLevel(logging.DEBUG)
LOGGER.debug('User UUID Backfill: Began execution')
# Make sure we have a reference to the current DB. # Make sure we have a reference to the current DB.
configure(app.config) 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. # Check to see if any users are missing uuids.
has_missing_uuids = bool(list(User has_missing_uuids = bool(list(User
.select() .select(User.id)
.where(User.uuid >> None) .where(User.uuid >> None)
.limit(1))) .limit(1)))
if not has_missing_uuids: if not has_missing_uuids:
logger.debug('User UUID Backfill: No migration needed') LOGGER.debug('User UUID Backfill: No migration needed')
return return
logger.debug('User UUID Backfill: Starting migration') LOGGER.debug('User UUID Backfill: Starting migration')
while True: while True:
batch_users = list(User batch_user_ids = list(User
.select() .select(User.id)
.where(User.uuid >> None) .where(User.uuid >> None)
.limit(100)) .limit(100))
if len(batch_users) == 0: if len(batch_user_ids) == 0:
# There are no users left to backfill. We're done! # 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 return
logging.debug('User UUID Backfill: Found %s records to update' % len(batch_users)) LOGGER.debug('User UUID Backfill: Found %s records to update', len(batch_user_ids))
for user in batch_users: 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.uuid = str(uuid.uuid4())
user.save() user.save()
except User.DoesNotExist:
pass
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)