-
Notifications
You must be signed in to change notification settings - Fork 894
feat: add support for optional external auth providers #12021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 20 commits
f9feb97
179adae
bdf6142
751400b
5068d88
eaddb5b
eba3d12
ebb518b
29be417
48267a4
e870667
d58c088
757ea0f
79ad9da
6746f88
cb7ab9a
3bacc70
28a878d
b514292
892a79b
c5e63eb
6f933b8
ea81a1f
2376357
b9471a5
b2b9e95
b159156
04579de
7ff56b1
16ba268
bdb875a
328202a
a475ec0
353e6a3
1021bc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
-- We cannot alter the column type while a view depends on it, so we drop it and recreate it. | ||
DROP VIEW template_version_with_user; | ||
|
||
|
||
-- Does the opposite of `migrate_external_auth_providers_to_jsonb` | ||
-- eg. `'[{"id": "github"}, {"id": "gitlab"}]'::jsonb` would become `'{github,gitlab}'::text[]` | ||
CREATE OR REPLACE FUNCTION revert_migrate_external_auth_providers_to_jsonb(jsonb) | ||
RETURNS text[] | ||
LANGUAGE plpgsql | ||
AS $$ | ||
DECLARE | ||
result text[]; | ||
BEGIN | ||
SELECT | ||
array_agg(id::text) INTO result | ||
FROM ( | ||
SELECT | ||
jsonb_array_elements($1) ->> 'id' AS id) AS external_auth_provider_ids; | ||
RETURN result; | ||
END; | ||
$$; | ||
|
||
|
||
-- Remove the non-null constraint and default | ||
ALTER TABLE template_versions | ||
ALTER COLUMN external_auth_providers DROP DEFAULT; | ||
ALTER TABLE template_versions | ||
ALTER COLUMN external_auth_providers DROP NOT NULL; | ||
|
||
|
||
-- Update the column type and migrate the values | ||
ALTER TABLE template_versions | ||
ALTER COLUMN external_auth_providers TYPE text[] | ||
USING revert_migrate_external_auth_providers_to_jsonb(external_auth_providers); | ||
|
||
|
||
-- Recreate `template_version_with_user` as described in dump.sql | ||
CREATE VIEW template_version_with_user AS | ||
SELECT | ||
template_versions.id, | ||
template_versions.template_id, | ||
template_versions.organization_id, | ||
template_versions.created_at, | ||
template_versions.updated_at, | ||
template_versions.name, | ||
template_versions.readme, | ||
template_versions.job_id, | ||
template_versions.created_by, | ||
template_versions.external_auth_providers, | ||
template_versions.message, | ||
template_versions.archived, | ||
COALESCE(visible_users.avatar_url, ''::text) AS created_by_avatar_url, | ||
COALESCE(visible_users.username, ''::text) AS created_by_username | ||
FROM (public.template_versions | ||
LEFT JOIN visible_users ON (template_versions.created_by = visible_users.id)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
-- We cannot alter the column type while a view depends on it, so we drop it and recreate it. | ||
DROP VIEW template_version_with_user; | ||
|
||
|
||
-- Turns the list of provider names into JSONB with the type `Array<{ id: string; optional?: boolean }>` | ||
-- eg. `'{github,gitlab}'::text[]` would become `'[{"id": "github"}, {"id": "gitlab"}]'::jsonb` | ||
CREATE OR REPLACE FUNCTION migrate_external_auth_providers_to_jsonb(text[]) | ||
RETURNS jsonb | ||
LANGUAGE plpgsql | ||
AS $$ | ||
DECLARE | ||
result jsonb; | ||
BEGIN | ||
SELECT | ||
jsonb_agg(jsonb_build_object('id', value::text)) INTO result | ||
FROM | ||
unnest($1) AS value; | ||
RETURN result; | ||
END; | ||
$$; | ||
aslilac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
-- Update the column type and migrate the values | ||
ALTER TABLE template_versions | ||
ALTER COLUMN external_auth_providers TYPE jsonb | ||
USING migrate_external_auth_providers_to_jsonb(external_auth_providers); | ||
|
||
|
||
-- Make the column non-nullable to make the types nicer on the Go side | ||
UPDATE template_versions | ||
SET external_auth_providers = '[]'::jsonb | ||
WHERE external_auth_providers IS NULL; | ||
ALTER TABLE template_versions | ||
ALTER COLUMN external_auth_providers SET DEFAULT '[]'::jsonb; | ||
ALTER TABLE template_versions | ||
ALTER COLUMN external_auth_providers SET NOT NULL; | ||
|
||
|
||
-- Recreate `template_version_with_user` as described in dump.sql | ||
CREATE VIEW template_version_with_user AS | ||
SELECT | ||
template_versions.id, | ||
template_versions.template_id, | ||
template_versions.organization_id, | ||
template_versions.created_at, | ||
template_versions.updated_at, | ||
template_versions.name, | ||
template_versions.readme, | ||
template_versions.job_id, | ||
template_versions.created_by, | ||
template_versions.external_auth_providers, | ||
template_versions.message, | ||
template_versions.archived, | ||
COALESCE(visible_users.avatar_url, ''::text) AS created_by_avatar_url, | ||
COALESCE(visible_users.username, ''::text) AS created_by_username | ||
FROM (public.template_versions | ||
LEFT JOIN visible_users ON (template_versions.created_by = visible_users.id)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heads-up: just before you merge, double-check the latest migration number on
main