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:
parent
10b627c2ad
commit
606ad21bec
4 changed files with 30 additions and 24 deletions
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 ###
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Reference in a new issue