Skip to content

Commit 620f214

Browse files
committed
Split the migration in two parts, two transactions.
1 parent 81efccf commit 620f214

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

crates/storage-pg/migrations/20250709142230_backfill_id_token_claims.sql renamed to crates/storage-pg/migrations/20250709142230_id_token_claims_trigger.sql

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
--
1010
-- We will be able to remove this trigger in a future version of the app.
1111
--
12-
-- We do this before the backfilling starts, to make sure we don't miss anything
12+
-- We backfill in a second migration after this one to make sure we don't miss
13+
-- any rows, and don't lock the table for too long.
1314
CREATE OR REPLACE FUNCTION fill_id_token_claims()
1415
RETURNS TRIGGER AS $$
1516
BEGIN
@@ -48,21 +49,3 @@ CREATE TRIGGER trg_fill_id_token_claims
4849
FOR EACH ROW
4950
WHEN (NEW.id_token_claims IS NULL AND NEW.id_token IS NOT NULL AND NEW.id_token <> '')
5051
EXECUTE FUNCTION fill_id_token_claims();
51-
52-
-- This backfills the id_token_claims column in the upstream_oauth_authorization_sessions table
53-
-- by decoding the id_token column and storing the decoded claims in the id_token_claims column.
54-
UPDATE upstream_oauth_authorization_sessions
55-
SET id_token_claims = CASE
56-
WHEN id_token IS NULL OR id_token = '' THEN NULL
57-
WHEN split_part(id_token, '.', 2) = '' THEN NULL
58-
ELSE
59-
(convert_from(
60-
decode(
61-
replace(replace(split_part(id_token, '.', 2), '-', '+'), '_', '/') ||
62-
repeat('=', (4 - length(split_part(id_token, '.', 2)) % 4) % 4),
63-
'base64'
64-
),
65-
'UTF8'
66-
))::JSONB
67-
END
68-
WHERE id_token IS NOT NULL AND id_token_claims IS NULL;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Copyright 2025 New Vector Ltd.
2+
--
3+
-- SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
4+
-- Please see LICENSE in the repository root for full details.
5+
6+
-- This backfills the id_token_claims column in the upstream_oauth_authorization_sessions table
7+
-- by decoding the id_token column and storing the decoded claims in the id_token_claims column.
8+
UPDATE upstream_oauth_authorization_sessions
9+
SET id_token_claims = CASE
10+
WHEN id_token IS NULL OR id_token = '' THEN NULL
11+
WHEN split_part(id_token, '.', 2) = '' THEN NULL
12+
ELSE
13+
(convert_from(
14+
decode(
15+
replace(replace(split_part(id_token, '.', 2), '-', '+'), '_', '/') ||
16+
repeat('=', (4 - length(split_part(id_token, '.', 2)) % 4) % 4),
17+
'base64'
18+
),
19+
'UTF8'
20+
))::JSONB
21+
END
22+
WHERE id_token IS NOT NULL AND id_token_claims IS NULL;

0 commit comments

Comments
 (0)