Skip to content

Commit 14b5382

Browse files
committed
finish migration
1 parent 4108ece commit 14b5382

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
BEGIN;
22

3-
ALTER TABLE users DROP COLUMN linked_id;
4-
ALTER TABLE users DROP COLUMN login_type;
5-
63
COMMIT;
Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
BEGIN;
22

3-
CREATE TABLE IF NOT EXISTS users (
3+
ALTER TYPE login_type ADD VALUE 'oidc';
4+
5+
CREATE TABLE IF NOT EXISTS user_links (
46
user_id uuid NOT NULL,
57
login_type login_type NOT NULL,
6-
linked_id text NOT NULL DEFAULT ''::text NOT NULL,
7-
oauth_access_token text DEFAULT ''::text NOT NULL,
8-
oauth_refresh_token text DEFAULT ''::text NOT NULL,
9-
oauth_id_token text DEFAULT ''::text NOT NULL,
10-
oauth_expiry timestamp with time zone DEFAULT '0001-01-01 00:00:00+00'::timestamp with time zone NOT NULL,
11-
UNIQUE(user_id, login_type),
12-
)
13-
ALTER TABLE users ADD COLUMN linked_id text NOT NULL DEFAULT '';
8+
linked_id text DEFAULT ''::text NOT NULL,
9+
oauth_access_token text DEFAULT ''::text NOT NULL,
10+
oauth_refresh_token text DEFAULT ''::text NOT NULL,
11+
oauth_id_token text DEFAULT ''::text NOT NULL,
12+
oauth_expiry timestamp with time zone DEFAULT '0001-01-01 00:00:00+00'::timestamp with time zone NOT NULL,
13+
UNIQUE(user_id, login_type)
14+
);
15+
16+
INSERT INTO user_links
17+
(
18+
user_id,
19+
login_type,
20+
linked_id,
21+
oauth_access_token,
22+
oauth_refresh_token,
23+
oauth_id_token,
24+
oauth_expiry
25+
)
26+
SELECT
27+
keys.user_id,
28+
keys.login_type,
29+
'',
30+
keys.oauth_access_token,
31+
keys.oauth_refresh_token,
32+
keys.oauth_id_token,
33+
keys.oauth_expiry
34+
FROM
35+
(
36+
SELECT
37+
row_number() OVER (partition by user_id, login_type ORDER BY updated_at DESC) AS x,
38+
api_keys.* FROM api_keys
39+
) as keys
40+
WHERE x=1 AND keys.login_type != 'password';
1441

15-
UPDATE
16-
users
17-
SET
18-
login_type = (
19-
SELECT
20-
login_type
21-
FROM
22-
api_keys
23-
WHERE
24-
api_keys.user_id = users.id
25-
ORDER BY updated_at DESC
26-
LIMIT 1
27-
);
42+
ALTER TABLE api_keys RENAME COLUMN login_type TO _login_type
2843

2944
COMMIT;

0 commit comments

Comments
 (0)