This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/util/migrate/fixsequences.py
2016-04-01 14:26:19 -04:00

52 lines
1.1 KiB
Python

import uuid
import logging
from peewee import IntegrityError, CharField
from app import app
from data.database import BaseModel
logger = logging.getLogger(__name__)
ENUM_CLASSES_WITH_SEQUENCES = [
'TeamRole',
'LoginService',
'Visibility',
'Role',
'AccessTokenKind',
'BuildTriggerService',
'ImageStorageTransformation',
'ImageStorageSignatureKind',
'ImageStorageLocation',
'LogEntryKind',
'NotificationKind',
'ExternalNotificationEvent',
'ExternalNotificationMethod',
]
def reset_enum_sequences():
for class_name in ENUM_CLASSES_WITH_SEQUENCES:
reset_sequence(class_name)
def reset_sequence(class_name):
logger.info('Resetting sequence for table: %s', class_name.lower())
unique_name = '%s' % uuid.uuid4()
Model = type(class_name, (BaseModel,), {'name': CharField(index=True)})
for skips in xrange(50):
try:
Model.create(name=unique_name).delete_instance()
logger.info('Advanced sequence %s numbers', skips)
break
except IntegrityError:
pass
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
reset_enum_sequences()