Skip to content

chore(organizations): swap member teams id to int8 - #123076

Open
ldelvoye wants to merge 1 commit into
masterfrom
ldelvoye/chore/omt-swap-new-id-primary-key
Open

chore(organizations): swap member teams id to int8#123076
ldelvoye wants to merge 1 commit into
masterfrom
ldelvoye/chore/omt-swap-new-id-primary-key

Conversation

@ldelvoye

Copy link
Copy Markdown
Member

Resolves INFRENG-449

Swap organizationmember_team.id and organizationmember_team.new_id, attach a constraint to the pre-built index, and widen the sequence to BIGINT.

Promotes new_id to the primary key by rotating the column names, so the
running release keeps both columns it declares. The key attaches to the
unique index built earlier, keeping the swap metadata-only.
@ldelvoye
ldelvoye requested review from a team as code owners August 28, 2026 21:57
@linear-code

linear-code Bot commented Aug 28, 2026

Copy link
Copy Markdown

INFRENG-449

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Aug 28, 2026
Comment thread src/sentry/models/organizationmemberteam.py
@github-actions

Copy link
Copy Markdown
Contributor

This PR has a migration; here is the generated SQL for src/sentry/migrations/1160_organizationmemberteam_swap_new_id_primary_key.py

for 1160_organizationmemberteam_swap_new_id_primary_key in sentry

BEGIN;
--
-- Custom state/database change combination
--

DO $$
DECLARE
    next_id bigint;
    pk_name text;
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_schema = current_schema()
          AND table_name = 'sentry_organizationmember_teams'
          AND column_name = 'id'
          AND is_identity = 'YES'
    ) THEN
        -- Read the mark first: dropping the identity takes its sequence with it.
        SELECT CASE WHEN is_called THEN last_value + 1 ELSE last_value END
        INTO next_id
        FROM "sentry_organizationmember_teams_id_seq";

        ALTER TABLE "sentry_organizationmember_teams" ALTER COLUMN "id" DROP IDENTITY;
        -- Rebuild under the same name, resuming where the identity left off.
        EXECUTE format(
            'CREATE SEQUENCE "sentry_organizationmember_teams_id_seq" AS bigint '
            'START WITH %s OWNED BY "sentry_organizationmember_teams"."new_id"',
            next_id
        );
    ELSE
        -- A plain sequence can simply be moved across, keeping its position untouched.
        ALTER TABLE "sentry_organizationmember_teams" ALTER COLUMN "id" DROP DEFAULT;
        ALTER SEQUENCE "sentry_organizationmember_teams_id_seq"
            OWNED BY "sentry_organizationmember_teams"."new_id";
        ALTER SEQUENCE "sentry_organizationmember_teams_id_seq" AS bigint;
    END IF;

    ALTER TABLE "sentry_organizationmember_teams"
        ALTER COLUMN "new_id" SET DEFAULT nextval('sentry_organizationmember_teams_id_seq');

    -- Postgres owns this name, so look it up rather than hardcode a guess.
    SELECT constraint_name INTO pk_name
    FROM information_schema.table_constraints
    WHERE table_schema = current_schema()
      AND table_name = 'sentry_organizationmember_teams'
      AND constraint_type = 'PRIMARY KEY';

    EXECUTE format(
        'ALTER TABLE "sentry_organizationmember_teams" DROP CONSTRAINT %I', pk_name
    );

    -- Naming the constraint renames the prebuilt index onto it, so the key keeps its old name.
    EXECUTE format(
        'ALTER TABLE "sentry_organizationmember_teams" ADD CONSTRAINT %I '
        'PRIMARY KEY USING INDEX "sentry_organizationmember_teams_new_id_uniq"',
        pk_name
    );
END $$;

-- Rotate all three so the running release keeps both columns it declares.
ALTER TABLE "sentry_organizationmember_teams" RENAME COLUMN "id" TO "id_tmp";
ALTER TABLE "sentry_organizationmember_teams" RENAME COLUMN "new_id" TO "id";
ALTER TABLE "sentry_organizationmember_teams" RENAME COLUMN "id_tmp" TO "new_id";

COMMIT;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant