Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Formatting fix for Sentry issue #5502
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()
dupe_release_envs = ReleaseEnvironment.objects.values(
'release_id', 'organization_id', 'environment_id'
).annotate(
recount=Count('id')
).filter(recount__gt=1)
for renv in dupe_release_envs:
release_id = renv['release_id']
organization_id = renv['organization_id']
environment_id = renv['environment_id']
renvs = list(ReleaseEnvironment.objects.filter(
release_id=release_id,
organization_id=organization_id,
environment_id=environment_id,
).order_by('first_seen'))
to_renv = renvs[0]
from_renvs = renvs[1:]
last_seen = max([re.last_seen for re in renvs])
ReleaseEnvironment.objects.filter(
id=to_renv.id,
).update(last_seen=last_seen)
ReleaseEnvironment.objects.filter(
id__in=[re.id for re in from_renvs],
).delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.