Skip to content

Commit 83f8192

Browse files
committed
fix(app-platform): ApiToken Cleanup Fix
Cleanup was failing due to foreign key constraints on ApiTokens that were associated with a SentryAppInstallation. It's okay if they stick around, so this just excludes them from the query to find all ApiTokens to delete.
1 parent d6d1d78 commit 83f8192

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/sentry/runner/commands/cleanup.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,18 @@ def is_filtered(model):
224224
if not silent:
225225
click.echo(u'>> Skipping {}'.format(model.__name__))
226226
else:
227-
model.objects.filter(
227+
queryset = model.objects.filter(
228228
expires_at__lt=(timezone.now() - timedelta(days=API_TOKEN_TTL_IN_DAYS)),
229-
).delete()
229+
)
230+
231+
# SentryAppInstallations are associated to ApiTokens. We're okay
232+
# with these tokens sticking around so that the Integration can
233+
# refresh them, but all other non-associated tokens should be
234+
# deleted.
235+
if model is models.ApiToken:
236+
queryset = queryset.filter(sentry_app_installation__isnull=True)
237+
238+
queryset.delete()
230239

231240
project_id = None
232241
if project:

0 commit comments

Comments
 (0)