Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sentry.models import *
- dupe_envs = Environment.objects.values('name', 'organization_id').annotate(ecount=Count('id')).filter(ecount__gt=1)
- for env in dupe_envs:
- name = env['name']
- organization_id = env['organization_id']
- envs = list(Environment.objects.filter(
- name=name,
- organization_id=organization_id,
- ).order_by('date_added'))
- to_env = envs[0]
- from_envs = envs[1:]
- try:
- with transaction.atomic():
- EnvironmentProject.objects.filter(
- environment__in=from_envs,
- ).update(environment=to_env)
- except IntegrityError:
- for ep in EnvironmentProject.objects.filter(environment__in=from_envs):
- try:
- with transaction.atomic():
- EnvironmentProject.objects.filter(
- id=ep.id,
- ).update(environment=to_env)
- except IntegrityError:
- ep.delete()
- from_env_ids = [e.id for e in from_envs]
- try:
- with transaction.atomic():
- ReleaseEnvironment.objects.filter(
- environment_id__in=from_env_ids,
- ).update(environment_id=to_env.id)
- except IntegrityError:
- for re in ReleaseEnvironment.objects.filter(environment_id__in=from_env_ids):
- try:
- with transaction.atomic():
- ReleaseEnvironment.objects.filter(
- id=re.id,
- ).update(environment_id=to_env.id)
- except IntegrityError:
- re.delete()
- Environment.objects.filter(id__in=from_env_ids).delete()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement