chore(organizations): swap member teams id to int8 - #123076
Open
ldelvoye wants to merge 1 commit into
Open
Conversation
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.
Contributor
|
This PR has a migration; here is the generated SQL for for 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves INFRENG-449
Swap
organizationmember_team.idandorganizationmember_team.new_id, attach a constraint to the pre-built index, and widen the sequence to BIGINT.