Skip to content

Commit c42e59b

Browse files
corpsschew2381
authored andcommitted
ref(hc): Adding back in actor constraint idempotently (#47107)
Idempotently adds in back the constraints that were not set in production from 0403_backfill_actors.py This will be run in a batch after 0415_backfill_actor_team_and_user.py has run in production. This is written with idempotency in mind -- any dev / self hosted environment that already did successfully apply 403 will remove and re-add the index / constraint. Unfortunately `IF NOT EXISTS` does not work well for constraints, so the only safe option here was to drop and recreate the unique indexes and constraints. Second half of this migration is related to adding the null=False check after 0413_backfill_organization_integration_denormalization.py is also run in production. So we have basically 4 is_dangerous=True migration's we'll be running one after the other.
1 parent 2fa136c commit c42e59b

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

migrations_lockfile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ To resolve this, rebase against latest master and regenerate your migration. Thi
66
will then be regenerated, and you should be able to merge without conflicts.
77

88
nodestore: 0002_nodestore_no_dictfield
9-
sentry: 0417_backfill_groupedmessage_substatus
9+
sentry: 0418_add_actor_constraints
1010
social_auth: 0001_initial
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Generated by Django 2.2.28 on 2023-01-31 20:37
2+
3+
from django.db import migrations
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. For
10+
# the most part, this should only be used for operations where it's safe to run the migration
11+
# after your code has deployed. So this should not be used for most operations that alter the
12+
# schema of a table.
13+
# Here are some things that make sense to mark as dangerous:
14+
# - Large data migrations. Typically we want these to be run manually by ops so that they can
15+
# be 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+
# have ops run this and not block the deploy. Note that while adding an index is a schema
18+
# change, it's completely safe to run the operation after the code has deployed.
19+
is_dangerous = True
20+
21+
dependencies = [
22+
("sentry", "0417_backfill_groupedmessage_substatus"),
23+
]
24+
25+
operations = (
26+
[
27+
migrations.RunSQL(
28+
sql=line,
29+
reverse_sql="",
30+
hints={"tables": ["sentry_actor"]},
31+
)
32+
for line in """
33+
ALTER TABLE "sentry_actor" DROP CONSTRAINT IF EXISTS "sentry_actor_team_id_6ca8eba5_fk_sentry_team_id";
34+
ALTER TABLE "sentry_actor" DROP CONSTRAINT IF EXISTS "sentry_actor_team_id_6ca8eba5_uniq";
35+
DROP INDEX CONCURRENTLY IF EXISTS "sentry_actor_team_id_6ca8eba5";
36+
DROP INDEX CONCURRENTLY IF EXISTS "sentry_actor_team_id_6ca8eba5_uniq";
37+
38+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "sentry_actor_team_id_6ca8eba5_uniq" ON "sentry_actor" ("team_id");
39+
ALTER TABLE "sentry_actor" ADD CONSTRAINT "sentry_actor_team_id_6ca8eba5_fk_sentry_team_id" FOREIGN KEY ("team_id") REFERENCES "sentry_team" ("id") DEFERRABLE INITIALLY DEFERRED NOT VALID;
40+
ALTER TABLE "sentry_actor" VALIDATE CONSTRAINT "sentry_actor_team_id_6ca8eba5_fk_sentry_team_id";
41+
42+
ALTER TABLE "sentry_actor" DROP CONSTRAINT IF EXISTS "sentry_actor_user_id_c832ff63_uniq";
43+
DROP INDEX CONCURRENTLY IF EXISTS "sentry_actor_user_id_c832ff63";
44+
DROP INDEX CONCURRENTLY IF EXISTS "sentry_actor_user_id_c832ff63_uniq";
45+
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "sentry_actor_user_id_c832ff63_uniq" ON "sentry_actor" ("user_id");
46+
""".splitlines()
47+
if line.strip()
48+
]
49+
+ [
50+
migrations.RunSQL(
51+
sql="SELECT 1",
52+
reverse_sql="""
53+
ALTER TABLE "sentry_actor" ADD CONSTRAINT "sentry_actor_team_id_6ca8eba5_uniq" UNIQUE USING INDEX "sentry_actor_team_id_6ca8eba5_uniq";
54+
""",
55+
hints={"tables": ["sentry_actor"]},
56+
),
57+
migrations.RunSQL(
58+
sql="SELECT 1",
59+
reverse_sql="""
60+
ALTER TABLE "sentry_actor" ADD CONSTRAINT "sentry_actor_user_id_c832ff63_uniq" UNIQUE USING INDEX "sentry_actor_user_id_c832ff63_uniq";
61+
""",
62+
hints={"tables": ["sentry_actor"]},
63+
),
64+
]
65+
)

src/sentry/models/integrations/pagerduty_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
@region_silo_only_model
16-
class PagerDutyService(DefaultFieldsModel, OrganizationIntegrityBackfillMixin):
16+
class PagerDutyService(OrganizationIntegrityBackfillMixin, DefaultFieldsModel):
1717
__include_in_export__ = False
1818

1919
organization_integration = FlexibleForeignKey("sentry.OrganizationIntegration")

src/sentry/models/integrations/repository_project_path_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
@region_silo_only_model
16-
class RepositoryProjectPathConfig(DefaultFieldsModel, OrganizationIntegrityBackfillMixin):
16+
class RepositoryProjectPathConfig(OrganizationIntegrityBackfillMixin, DefaultFieldsModel):
1717
__include_in_export__ = False
1818

1919
repository = FlexibleForeignKey("sentry.Repository")

0 commit comments

Comments
 (0)