Skip to content

Commit 50905af

Browse files
authored
chore(eap-migration): Add explore foreign key to DiscoverSavedQuery model (#94933)
For the discover saved queries -> explore saved queries migration i've added an additional `DISCOVER_TRANSACTIONS` dataset type to track which queries are migrated. To keep track of the original saved query we're also adding a `explore_query` foreign key field to link the query to its new explore query. This change requires a migration to add the field to the model. I've also added a JSON field (`changed_reason`) to the explore saved query model which we will use to put our reasoning for dropping certain fields during the migration.
1 parent 8f96c06 commit 50905af

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

migrations_lockfile.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ ahead of you.
55
To resolve this, rebase against latest master and regenerate your migration. This file
66
will then be regenerated, and you should be able to merge without conflicts.
77

8-
discover: 0001_move_discover_models
8+
discover: 0002_link_migrated_explore_query_in_discover
99

10-
explore: 0005_explore_django_json_field
10+
explore: 0006_add_changed_reason_field_explore
1111

1212
feedback: 0001_squashed_0004_index_together
1313

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Generated by Django 5.2.1 on 2025-07-10 15:15
2+
3+
import django.db.models.deletion
4+
from django.db import migrations
5+
6+
import sentry.db.models.fields.foreignkey
7+
from sentry.new_migrations.migrations import CheckedMigration
8+
9+
10+
class Migration(CheckedMigration):
11+
# This flag is used to mark that a migration shouldn't be automatically run in production.
12+
# This should only be used for operations where it's safe to run the migration after your
13+
# code has deployed. So this should not be used for most operations that alter the schema
14+
# of a table.
15+
# Here are some things that make sense to mark as post deployment:
16+
# - Large data migrations. Typically we want these to be run manually so that they can be
17+
# monitored and not block the deploy for a long period of time while they run.
18+
# - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
19+
# run this outside deployments so that we don't block them. Note that while adding an index
20+
# is a schema change, it's completely safe to run the operation after the code has deployed.
21+
# Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment
22+
23+
is_post_deployment = False
24+
25+
dependencies = [
26+
("discover", "0001_move_discover_models"),
27+
("explore", "0006_add_changed_reason_field_explore"),
28+
]
29+
30+
operations = [
31+
migrations.AddField(
32+
model_name="discoversavedquery",
33+
name="explore_query",
34+
field=sentry.db.models.fields.foreignkey.FlexibleForeignKey(
35+
null=True,
36+
on_delete=django.db.models.deletion.SET_NULL,
37+
to="explore.exploresavedquery",
38+
),
39+
),
40+
]

src/sentry/discover/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ class DiscoverSavedQuery(Model):
108108
default=DatasetSourcesTypes.UNKNOWN.value,
109109
db_default=DatasetSourcesTypes.UNKNOWN.value,
110110
)
111+
# This field is used for the discover transactions -> explore migration.
112+
# Migrated discover transactions queries will have this reference along with DISCOVER_TRANSACTIONS as the dataset
113+
# in the ExploreSavedQuery.
114+
explore_query = FlexibleForeignKey(
115+
"explore.ExploreSavedQuery", null=True, on_delete=models.SET_NULL
116+
)
111117

112118
class Meta:
113119
app_label = "discover"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Generated by Django 5.2.1 on 2025-07-10 14:27
2+
3+
from django.db import migrations, models
4+
5+
from sentry.new_migrations.migrations import CheckedMigration
6+
7+
8+
class Migration(CheckedMigration):
9+
# This flag is used to mark that a migration shouldn't be automatically run in production.
10+
# This should only be used for operations where it's safe to run the migration after your
11+
# code has deployed. So this should not be used for most operations that alter the schema
12+
# of a table.
13+
# Here are some things that make sense to mark as post deployment:
14+
# - Large data migrations. Typically we want these to be run manually so that they can be
15+
# monitored and not block the deploy for a long period of time while they run.
16+
# - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
17+
# run this outside deployments so that we don't block them. Note that while adding an index
18+
# is a schema change, it's completely safe to run the operation after the code has deployed.
19+
# Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment
20+
21+
is_post_deployment = False
22+
23+
dependencies = [
24+
("explore", "0005_explore_django_json_field"),
25+
]
26+
27+
operations = [
28+
migrations.AddField(
29+
model_name="exploresavedquery",
30+
name="changed_reason",
31+
field=models.JSONField(default=None, null=True),
32+
),
33+
]

src/sentry/explore/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919
class ExploreSavedQueryDataset(TypesClass):
2020
SPANS = 0
2121
OURLOGS = 1
22+
# This is a temporary dataset to be used for the discover -> explore migration.
23+
# It will track which queries are generated from discover queries.
24+
SEGMENT_SPANS = 101
2225

2326
TYPES = [
2427
(SPANS, "spans"),
2528
(OURLOGS, "ourlogs"),
29+
(SEGMENT_SPANS, "segment_spans"),
2630
]
2731
TYPE_NAMES = [t[1] for t in TYPES]
2832

@@ -86,6 +90,9 @@ class ExploreSavedQuery(DefaultFieldsModel):
8690
# The version of the prebuilt query. If the version found in the explore_saved_queries.py hardcoded list is greater, then the saved
8791
# query out of date and should be updated..
8892
prebuilt_version = BoundedPositiveIntegerField(null=True, db_default=None)
93+
# This field is to be used for the discover -> explore migration. This contains the reason why any part
94+
# of the saved query was changed so we can display our reasonings in the UI
95+
changed_reason = models.JSONField(null=True, default=None)
8996

9097
class Meta:
9198
app_label = "explore"

0 commit comments

Comments
 (0)