Advertisement
Guest User

Migrate 1

a guest
Jun 12th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. from sentry.models import *
  2. dupe_envs = Environment.objects.values('name', 'organization_id').annotate(ecount=Count('id')).filter(ecount__gt=1)
  3. for env in dupe_envs:
  4. name = env['name']
  5. organization_id = env['organization_id']
  6. envs = list(Environment.objects.filter(
  7. name=name,
  8. organization_id=organization_id,
  9. ).order_by('date_added'))
  10. to_env = envs[0]
  11. from_envs = envs[1:]
  12. try:
  13. with transaction.atomic():
  14. EnvironmentProject.objects.filter(
  15. environment__in=from_envs,
  16. ).update(environment=to_env)
  17. except IntegrityError:
  18. for ep in EnvironmentProject.objects.filter(environment__in=from_envs):
  19. try:
  20. with transaction.atomic():
  21. EnvironmentProject.objects.filter(
  22. id=ep.id,
  23. ).update(environment=to_env)
  24. except IntegrityError:
  25. ep.delete()
  26. from_env_ids = [e.id for e in from_envs]
  27. try:
  28. with transaction.atomic():
  29. ReleaseEnvironment.objects.filter(
  30. environment_id__in=from_env_ids,
  31. ).update(environment_id=to_env.id)
  32. except IntegrityError:
  33. for re in ReleaseEnvironment.objects.filter(environment_id__in=from_env_ids):
  34. try:
  35. with transaction.atomic():
  36. ReleaseEnvironment.objects.filter(
  37. id=re.id,
  38. ).update(environment_id=to_env.id)
  39. except IntegrityError:
  40. re.delete()
  41. Environment.objects.filter(id__in=from_env_ids).delete()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement