Skip to content

Commit e9a3416

Browse files
committed
Handle existing PKCE schema
These changes were already applied in production; we should ensure the script detects that and skips over them rather than failing.
1 parent e01a8af commit e9a3416

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

admin/sql/updates/20200914-oauth-pkce.sql

+28-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,33 @@
22

33
BEGIN;
44

5-
CREATE TYPE oauth_code_challenge_method AS ENUM ('plain', 'S256');
6-
ALTER TABLE editor_oauth_token ADD COLUMN code_challenge TEXT;
7-
ALTER TABLE editor_oauth_token ADD COLUMN code_challenge_method oauth_code_challenge_method;
8-
ALTER TABLE editor_oauth_token ADD CONSTRAINT valid_code_challenge CHECK (
9-
(code_challenge IS NULL) = (code_challenge_method IS NULL) AND
10-
(code_challenge IS NULL OR code_challenge ~ E'^[A-Za-z0-9.~_-]{43,128}$')
11-
);
5+
DO $$
6+
BEGIN
7+
PERFORM 1 FROM pg_type
8+
WHERE typname = 'oauth_code_challenge_method';
9+
10+
IF NOT FOUND THEN
11+
CREATE TYPE oauth_code_challenge_method AS ENUM ('plain', 'S256');
12+
END IF;
13+
END
14+
$$;
15+
16+
ALTER TABLE editor_oauth_token ADD COLUMN IF NOT EXISTS code_challenge TEXT;
17+
ALTER TABLE editor_oauth_token ADD COLUMN IF NOT EXISTS code_challenge_method oauth_code_challenge_method;
18+
19+
DO $$
20+
BEGIN
21+
PERFORM 1 FROM information_schema.constraint_column_usage
22+
WHERE table_name = 'editor_oauth_token'
23+
AND constraint_name = 'valid_code_challenge';
24+
25+
IF NOT FOUND THEN
26+
ALTER TABLE editor_oauth_token ADD CONSTRAINT valid_code_challenge CHECK (
27+
(code_challenge IS NULL) = (code_challenge_method IS NULL) AND
28+
(code_challenge IS NULL OR code_challenge ~ E'^[A-Za-z0-9.~_-]{43,128}$')
29+
);
30+
END IF;
31+
END
32+
$$;
1233

1334
COMMIT;

admin/sql/updates/schema-change/26.slave.sql

+28-7
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,34 @@ $$ LANGUAGE 'plpgsql';
8181
SELECT '20200914-oauth-pkce.sql';
8282

8383

84-
CREATE TYPE oauth_code_challenge_method AS ENUM ('plain', 'S256');
85-
ALTER TABLE editor_oauth_token ADD COLUMN code_challenge TEXT;
86-
ALTER TABLE editor_oauth_token ADD COLUMN code_challenge_method oauth_code_challenge_method;
87-
ALTER TABLE editor_oauth_token ADD CONSTRAINT valid_code_challenge CHECK (
88-
(code_challenge IS NULL) = (code_challenge_method IS NULL) AND
89-
(code_challenge IS NULL OR code_challenge ~ E'^[A-Za-z0-9.~_-]{43,128}$')
90-
);
84+
DO $$
85+
BEGIN
86+
PERFORM 1 FROM pg_type
87+
WHERE typname = 'oauth_code_challenge_method';
88+
89+
IF NOT FOUND THEN
90+
CREATE TYPE oauth_code_challenge_method AS ENUM ('plain', 'S256');
91+
END IF;
92+
END
93+
$$;
94+
95+
ALTER TABLE editor_oauth_token ADD COLUMN IF NOT EXISTS code_challenge TEXT;
96+
ALTER TABLE editor_oauth_token ADD COLUMN IF NOT EXISTS code_challenge_method oauth_code_challenge_method;
97+
98+
DO $$
99+
BEGIN
100+
PERFORM 1 FROM information_schema.constraint_column_usage
101+
WHERE table_name = 'editor_oauth_token'
102+
AND constraint_name = 'valid_code_challenge';
103+
104+
IF NOT FOUND THEN
105+
ALTER TABLE editor_oauth_token ADD CONSTRAINT valid_code_challenge CHECK (
106+
(code_challenge IS NULL) = (code_challenge_method IS NULL) AND
107+
(code_challenge IS NULL OR code_challenge ~ E'^[A-Za-z0-9.~_-]{43,128}$')
108+
);
109+
END IF;
110+
END
111+
$$;
91112

92113
--------------------------------------------------------------------------------
93114
SELECT '20201028-mbs-1424.sql';

0 commit comments

Comments
 (0)