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

53 lines
1.1 KiB
Python
Raw Normal View History

import uuid
import logging
from peewee import IntegrityError, CharField
2016-04-01 18:26:19 +00:00
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()