-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
0660_fix_cron_monitor_invalid_orgs.py
58 lines (48 loc) · 2.5 KB
/
0660_fix_cron_monitor_invalid_orgs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Generated by Django 5.0.2 on 2024-03-01 23:48
from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps
from sentry.new_migrations.migrations import CheckedMigration
from sentry.utils.query import RangeQuerySetWrapperWithProgressBarApprox
def fix_cron_monitor_invalid_orgs(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
Monitor = apps.get_model("sentry", "Monitor")
Project = apps.get_model("sentry", "Project")
for monitor in RangeQuerySetWrapperWithProgressBarApprox(Monitor.objects.all()):
try:
project = Project.objects.get(id=monitor.project_id)
except Project.DoesNotExist:
continue
if project.organization_id != monitor.organization_id:
if Monitor.objects.filter(organization_id=project.organization_id, slug=monitor.slug):
# There are a small number of these and due to the way ingest works they can't
# receive checkins, so they're totally broken. Just delete.
monitor.delete()
else:
monitor.organization_id = project.organization_id
monitor.save(update_fields=["organization_id", "slug"])
class Migration(CheckedMigration):
# This flag is used to mark that a migration shouldn't be automatically run in production. For
# the most part, this should only be used for operations where it's safe to run the migration
# after your code has deployed. So this should not be used for most operations that alter the
# schema of a table.
# Here are some things that make sense to mark as post deployment:
# - Large data migrations. Typically we want these to be run manually by ops so that they can
# be monitored and not block the deploy for a long period of time while they run.
# - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
# have ops run this and not block the deploy. Note that while adding an index is a schema
# change, it's completely safe to run the operation after the code has deployed.
is_post_deployment = True
dependencies = [
("sentry", "0659_artifactbundleindex_cleanup"),
]
operations = [
migrations.RunPython(
fix_cron_monitor_invalid_orgs,
migrations.RunPython.noop,
hints={
"tables": [
"sentry_monitor",
]
},
)
]