Move the sequence fixer to a separate tool which can be run
This commit is contained in:
parent
b9f47f6761
commit
bd5b44cbd2
2 changed files with 53 additions and 31 deletions
|
@ -15,39 +15,10 @@ import sqlalchemy as sa
|
|||
|
||||
import uuid
|
||||
from peewee import CharField, IntegrityError
|
||||
|
||||
from util.migrate.fixsequences import reset_enum_sequences
|
||||
|
||||
def upgrade(tables):
|
||||
from data.database import BaseModel
|
||||
|
||||
class_names = [
|
||||
'TeamRole',
|
||||
'LoginService',
|
||||
'Visibility',
|
||||
'Role',
|
||||
'AccessTokenKind',
|
||||
'BuildTriggerService',
|
||||
'ImageStorageTransformation',
|
||||
'ImageStorageSignatureKind',
|
||||
'ImageStorageLocation',
|
||||
'LogEntryKind',
|
||||
'NotificationKind',
|
||||
'ExternalNotificationEvent',
|
||||
'ExternalNotificationMethod',
|
||||
]
|
||||
|
||||
unique_name = '%s' % uuid.uuid4()
|
||||
|
||||
for class_name in class_names:
|
||||
Model = type(class_name, (BaseModel,), {'name': CharField(index=True)})
|
||||
|
||||
for _ in xrange(50):
|
||||
try:
|
||||
Model.create(name=unique_name).delete_instance()
|
||||
break
|
||||
except IntegrityError:
|
||||
pass
|
||||
|
||||
reset_enum_sequences()
|
||||
|
||||
def downgrade(tables):
|
||||
pass
|
||||
|
|
51
util/migrate/fixsequences.py
Normal file
51
util/migrate/fixsequences.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
import uuid
|
||||
import logging
|
||||
|
||||
from peewee import IntegrityError, CharField
|
||||
|
||||
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()
|
Reference in a new issue