From 2c1b942ef4b3ef6e0dd636e8059cddc67e28b752 Mon Sep 17 00:00:00 2001 From: Lucas Delvoye Date: Tue, 10 Nov 2026 09:49:14 -0800 Subject: [PATCH] chore(releases): drop the grouprelease new_release_id column Removes the old int4 column that 1253 moved to pending. Also deletes the 1252 and 1253 migration tests: reversing the drop re-adds the column with a new attnum, so 1252's unswap guard no-ops and both tests fail. --- migrations_lockfile.txt | 2 +- .../1254_grouprelease_drop_new_release_id.py | 31 ++ .../test_1252_grouprelease_swap_release_id.py | 267 ------------------ ...rouprelease_drop_new_release_id_pending.py | 26 -- 4 files changed, 32 insertions(+), 294 deletions(-) create mode 100644 src/sentry/migrations/1254_grouprelease_drop_new_release_id.py delete mode 100644 tests/sentry/migrations/test_1252_grouprelease_swap_release_id.py delete mode 100644 tests/sentry/migrations/test_1253_grouprelease_drop_new_release_id_pending.py diff --git a/migrations_lockfile.txt b/migrations_lockfile.txt index ea17620cfca2..8d04fc9210cf 100644 --- a/migrations_lockfile.txt +++ b/migrations_lockfile.txt @@ -41,7 +41,7 @@ seer: 0046_add_workflow_config_backing_workflow seer_workflow_outputs: 0001_initial -sentry: 1253_grouprelease_drop_new_release_id_pending +sentry: 1254_grouprelease_drop_new_release_id service_accounts: 0007_add_service_account_token_replica diff --git a/src/sentry/migrations/1254_grouprelease_drop_new_release_id.py b/src/sentry/migrations/1254_grouprelease_drop_new_release_id.py new file mode 100644 index 000000000000..86b8cd9de4eb --- /dev/null +++ b/src/sentry/migrations/1254_grouprelease_drop_new_release_id.py @@ -0,0 +1,31 @@ +from sentry.new_migrations.migrations import CheckedMigration +from sentry.new_migrations.monkey.fields import SafeRemoveField +from sentry.new_migrations.monkey.state import DeletionAction + + +class Migration(CheckedMigration): + # This flag is used to mark that a migration shouldn't be automatically run in production. + # 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 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 + # run this outside deployments so that we don't block them. Note that while adding an index + # is a schema change, it's completely safe to run the operation after the code has deployed. + # Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment + + is_post_deployment = False + + dependencies = [ + ("sentry", "1253_grouprelease_drop_new_release_id_pending"), + ] + + operations = [ + SafeRemoveField( + model_name="grouprelease", + name="new_release_id", + deletion_action=DeletionAction.DELETE, + ), + ] diff --git a/tests/sentry/migrations/test_1252_grouprelease_swap_release_id.py b/tests/sentry/migrations/test_1252_grouprelease_swap_release_id.py deleted file mode 100644 index 6dca5e8cb6c5..000000000000 --- a/tests/sentry/migrations/test_1252_grouprelease_swap_release_id.py +++ /dev/null @@ -1,267 +0,0 @@ -import importlib - -import pytest -from django.db import IntegrityError, InternalError, connection -from django.db.migrations.state import StateApps - -from sentry.testutils.cases import TestMigrations - -migration = importlib.import_module("sentry.migrations.1252_grouprelease_swap_release_id") - -WIDE_RELEASE_ID = 2_147_483_648 -US1_UNIQUE_NAME = "sentry_grouprelease_group_id_46ba6e430d088d04_uniq" -US1_RELEASE_ID_INDEX = "sentry_grouprelease_release_id" - - -def fetch_column_types() -> dict[str, str]: - with connection.cursor() as cursor: - cursor.execute( - """ - SELECT column_name, data_type - FROM information_schema.columns - WHERE table_name = 'sentry_grouprelease' - AND column_name IN ('release_id', 'new_release_id') - """ - ) - rows = cursor.fetchall() - return dict(rows) - - -def fetch_column_nullability() -> dict[str, str]: - with connection.cursor() as cursor: - cursor.execute( - """ - SELECT column_name, is_nullable - FROM information_schema.columns - WHERE table_name = 'sentry_grouprelease' - AND column_name IN ('release_id', 'new_release_id') - """ - ) - rows = cursor.fetchall() - return dict(rows) - - -def fetch_unique_constraint_columns(name: str) -> list[str] | None: - with connection.cursor() as cursor: - cursor.execute( - """ - SELECT array_agg(attribute.attname ORDER BY key.position) - FROM pg_constraint - CROSS JOIN LATERAL unnest(pg_constraint.conkey) WITH ORDINALITY AS key(attnum, position) - JOIN pg_attribute attribute - ON attribute.attrelid = pg_constraint.conrelid - AND attribute.attnum = key.attnum - WHERE pg_constraint.conrelid = 'sentry_grouprelease'::regclass - AND pg_constraint.contype = 'u' - AND pg_constraint.conname = %s - """, - [name], - ) - (columns,) = cursor.fetchone() - return columns - - -def fetch_index_columns(name: str) -> list[str] | None: - with connection.cursor() as cursor: - cursor.execute( - """ - SELECT array_agg(attribute.attname ORDER BY key.position) - FROM pg_index - JOIN pg_class index_class ON index_class.oid = pg_index.indexrelid - CROSS JOIN LATERAL unnest(pg_index.indkey) WITH ORDINALITY AS key(attnum, position) - JOIN pg_attribute attribute - ON attribute.attrelid = pg_index.indrelid - AND attribute.attnum = key.attnum - WHERE pg_index.indrelid = 'sentry_grouprelease'::regclass - AND index_class.relname = %s - """, - [name], - ) - (columns,) = cursor.fetchone() - return columns - - -def fetch_check_names() -> list[str]: - with connection.cursor() as cursor: - cursor.execute( - """ - SELECT conname - FROM pg_constraint - WHERE conrelid = 'sentry_grouprelease'::regclass - AND contype = 'c' - AND conname IN (%s, %s) - ORDER BY conname - """, - [migration.RELEASE_ID_CHECK, migration.NEW_RELEASE_ID_CHECK], - ) - rows = cursor.fetchall() - return [name for (name,) in rows] - - -def fetch_catalog_snapshot() -> dict[str, list[tuple[str, ...]]]: - with connection.cursor() as cursor: - cursor.execute( - """ - SELECT column_name, data_type, is_nullable - FROM information_schema.columns - WHERE table_name = 'sentry_grouprelease' - ORDER BY column_name - """ - ) - columns = cursor.fetchall() - - cursor.execute( - """ - SELECT conname, pg_get_constraintdef(oid) - FROM pg_constraint - WHERE conrelid = 'sentry_grouprelease'::regclass - ORDER BY conname - """ - ) - constraints = cursor.fetchall() - - cursor.execute( - """ - SELECT indexname, indexdef - FROM pg_indexes - WHERE tablename = 'sentry_grouprelease' - ORDER BY indexname - """ - ) - indexes = cursor.fetchall() - return {"columns": columns, "constraints": constraints, "indexes": indexes} - - -class SwapGroupReleaseReleaseIdTest(TestMigrations): - app = "sentry" - migrate_from = "1251_add_apisetupcode" - migrate_to = "1252_grouprelease_swap_release_id" - - narrow_type = "integer" - - def prepare_table_shape(self) -> None: - pass - - def setup_initial_state(self) -> None: - self.project = self.create_project() - self.group = self.create_group(project=self.project) - - def setup_before_migration(self, apps: StateApps) -> None: - self.prepare_table_shape() - - GroupRelease = apps.get_model("sentry", "GroupRelease") - - self.matched = GroupRelease.objects.create( - project_id=self.project.id, - group_id=self.group.id, - release_id=5, - new_release_id=5, - ) - self.mismatched = GroupRelease.objects.create( - project_id=self.project.id, - group_id=self.group.id, - release_id=7, - new_release_id=WIDE_RELEASE_ID, - ) - - def test_wide_column_became_release_id(self) -> None: - with self.subTest("column types"): - column_types = fetch_column_types() - assert column_types["release_id"] == "bigint" - assert column_types["new_release_id"] == self.narrow_type - - with self.subTest("narrow column is nullable"): - assert fetch_column_nullability() == {"release_id": "NO", "new_release_id": "YES"} - - with self.subTest("unique constraint"): - columns = fetch_unique_constraint_columns(migration.UNIQUE_NAME) - assert columns == ["group_id", "release_id", "environment"] - - with self.subTest("indexes"): - assert fetch_index_columns(migration.RELEASE_ID_INDEX) == ["release_id"] - assert fetch_index_columns(migration.NEW_RELEASE_ID_INDEX) == ["new_release_id"] - assert fetch_index_columns(migration.UNIQUE_TWIN) is None - assert fetch_index_columns(migration.INDEX_TWIN) is None - - with self.subTest("check constraint"): - assert fetch_check_names() == [migration.NEW_RELEASE_ID_CHECK] - - with self.subTest("rows"): - GroupRelease = self.apps.get_model("sentry", "GroupRelease") - - matched = GroupRelease.objects.get(id=self.matched.id) - assert (matched.release_id, matched.new_release_id) == (5, 5) - - mismatched = GroupRelease.objects.get(id=self.mismatched.id) - assert (mismatched.release_id, mismatched.new_release_id) == (WIDE_RELEASE_ID, 7) - - with self.subTest("unique constraint is enforced"): - with connection.cursor() as cursor: - with pytest.raises(IntegrityError): - cursor.execute( - """ - INSERT INTO sentry_grouprelease - (project_id, group_id, release_id, new_release_id, environment, - first_seen, last_seen) - VALUES (%s, %s, %s, 1, '', now(), now()) - """, - [self.project.id, self.group.id, WIDE_RELEASE_ID], - ) - - with self.subTest("second run is a no-op"): - snapshot_before = fetch_catalog_snapshot() - with connection.cursor() as cursor: - cursor.execute(migration.SWAP_SQL) - assert fetch_catalog_snapshot() == snapshot_before - - -class SwapUs1ShapedGroupReleaseReleaseIdTest(SwapGroupReleaseReleaseIdTest): - """The same swap against US-1's shape: older unique and index names, and a bigint release_id.""" - - narrow_type = "bigint" - - def prepare_table_shape(self) -> None: - with connection.cursor() as cursor: - cursor.execute( - f'ALTER TABLE sentry_grouprelease RENAME CONSTRAINT "{migration.UNIQUE_NAME}" ' - f'TO "{US1_UNIQUE_NAME}"' - ) - cursor.execute( - f'ALTER INDEX "{migration.RELEASE_ID_INDEX}" RENAME TO "{US1_RELEASE_ID_INDEX}"' - ) - cursor.execute("ALTER TABLE sentry_grouprelease ALTER COLUMN release_id TYPE bigint") - - def tearDown(self) -> None: - super().tearDown() - # Later test classes expect the canonical integer column. - with connection.cursor() as cursor: - cursor.execute( - "ALTER TABLE sentry_grouprelease ALTER COLUMN new_release_id TYPE integer" - ) - - -class SwapGroupReleaseInvalidTwinTest(TestMigrations): - app = "sentry" - # Both before the swap, so the table is left unswapped and the test runs SWAP_SQL itself. - migrate_from = "1251_add_apisetupcode" - migrate_to = "1251_add_apisetupcode" - - def test_invalid_twin_index_aborts_the_swap(self) -> None: - snapshot_before = fetch_catalog_snapshot() - - with connection.cursor() as cursor: - # What an interrupted CREATE INDEX CONCURRENTLY leaves behind. - cursor.execute( - "UPDATE pg_index SET indisvalid = false " - "WHERE indexrelid = 'sentry_grouprelease_new_release_id'::regclass" - ) - try: - with pytest.raises(InternalError, match="is missing or invalid"): - cursor.execute(migration.SWAP_SQL) - finally: - cursor.execute( - "UPDATE pg_index SET indisvalid = true " - "WHERE indexrelid = 'sentry_grouprelease_new_release_id'::regclass" - ) - - assert fetch_catalog_snapshot() == snapshot_before diff --git a/tests/sentry/migrations/test_1253_grouprelease_drop_new_release_id_pending.py b/tests/sentry/migrations/test_1253_grouprelease_drop_new_release_id_pending.py deleted file mode 100644 index d112844dbaae..000000000000 --- a/tests/sentry/migrations/test_1253_grouprelease_drop_new_release_id_pending.py +++ /dev/null @@ -1,26 +0,0 @@ -import importlib - -import pytest -from django.db import InternalError, connection - -from sentry.testutils.cases import TestMigrations - -swap_migration = importlib.import_module("sentry.migrations.1252_grouprelease_swap_release_id") -migration = importlib.import_module( - "sentry.migrations.1253_grouprelease_drop_new_release_id_pending" -) - - -class RequireSwapTest(TestMigrations): - app = "sentry" - # Both before the swap, so the table is left unswapped and the test swaps it itself. - migrate_from = "1251_add_apisetupcode" - migrate_to = "1251_add_apisetupcode" - - def test_refuses_to_run_until_the_table_is_swapped(self) -> None: - with connection.cursor() as cursor: - with pytest.raises(InternalError, match="is not swapped"): - cursor.execute(migration.REQUIRE_SWAP_SQL) - - cursor.execute(swap_migration.SWAP_SQL) - cursor.execute(migration.REQUIRE_SWAP_SQL)