Skip to content

Commit a3f6308

Browse files
authored
fix: rewrite login type migrations (coder#16978)
When trying to add [system users](coder#16916), we discovered an issue in two migrations that added values to the login_type enum. After some [consideration](coder#16916 (comment)), we decided to retroactively correct them.
1 parent de41bd6 commit a3f6308

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1-
ALTER TYPE login_type ADD VALUE IF NOT EXISTS 'none';
1+
-- This migration has been modified after its initial commit.
2+
-- The new implementation makes the same changes as the original, but
3+
-- takes into account the message in create_migration.sh. This is done
4+
-- to allow the insertion of a user with the "none" login type in later migrations.
25

3-
COMMENT ON TYPE login_type IS 'Specifies the method of authentication. "none" is a special case in which no authentication method is allowed.';
6+
CREATE TYPE new_logintype AS ENUM (
7+
'password',
8+
'github',
9+
'oidc',
10+
'token',
11+
'none'
12+
);
13+
COMMENT ON TYPE new_logintype IS 'Specifies the method of authentication. "none" is a special case in which no authentication method is allowed.';
14+
15+
ALTER TABLE users
16+
ALTER COLUMN login_type DROP DEFAULT,
17+
ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype),
18+
ALTER COLUMN login_type SET DEFAULT 'password'::new_logintype;
19+
20+
DROP INDEX IF EXISTS idx_api_key_name;
21+
ALTER TABLE api_keys
22+
ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype);
23+
CREATE UNIQUE INDEX idx_api_key_name
24+
ON api_keys (user_id, token_name)
25+
WHERE (login_type = 'token'::new_logintype);
26+
27+
ALTER TABLE user_links
28+
ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype);
29+
30+
DROP TYPE login_type;
31+
ALTER TYPE new_logintype RENAME TO login_type;

coderd/database/migrations/000195_oauth2_provider_codes.up.sql

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,33 @@ AFTER DELETE ON oauth2_provider_app_tokens
4343
FOR EACH ROW
4444
EXECUTE PROCEDURE delete_deleted_oauth2_provider_app_token_api_key();
4545

46-
ALTER TYPE login_type ADD VALUE IF NOT EXISTS 'oauth2_provider_app';
46+
CREATE TYPE new_logintype AS ENUM (
47+
'password',
48+
'github',
49+
'oidc',
50+
'token',
51+
'none',
52+
'oauth2_provider_app'
53+
);
54+
COMMENT ON TYPE new_logintype IS 'Specifies the method of authentication. "none" is a special case in which no authentication method is allowed.';
55+
56+
ALTER TABLE users
57+
ALTER COLUMN login_type DROP DEFAULT,
58+
ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype),
59+
ALTER COLUMN login_type SET DEFAULT 'password'::new_logintype;
60+
61+
DROP INDEX IF EXISTS idx_api_key_name;
62+
ALTER TABLE api_keys
63+
ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype);
64+
CREATE UNIQUE INDEX idx_api_key_name
65+
ON api_keys (user_id, token_name)
66+
WHERE (login_type = 'token'::new_logintype);
67+
68+
ALTER TABLE user_links
69+
ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype);
70+
71+
DROP TYPE login_type;
72+
ALTER TYPE new_logintype RENAME TO login_type;
4773

4874
-- Switch to an ID we will prefix to the raw secret that we give to the user
4975
-- (instead of matching on the entire secret as the ID, since they will be

0 commit comments

Comments
 (0)