[MM-52288] Oauth2 postgres migration issue (#23036)

* fixing migrations that caused some oauth2 issues
This commit is contained in:
Ben Cooke 2023-05-02 12:32:30 -04:00 committed by GitHub
parent 3ba75afa08
commit 5adbf6c612
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 2 deletions

View file

@ -214,6 +214,8 @@ channels/db/migrations/mysql/000106_fileinfo_channelid.down.sql
channels/db/migrations/mysql/000106_fileinfo_channelid.up.sql
channels/db/migrations/mysql/000107_threadmemberships_cleanup.down.sql
channels/db/migrations/mysql/000107_threadmemberships_cleanup.up.sql
channels/db/migrations/mysql/000108_remove_orphaned_oauth_preferences.down.sql
channels/db/migrations/mysql/000108_remove_orphaned_oauth_preferences.up.sql
channels/db/migrations/postgres/000001_create_teams.down.sql
channels/db/migrations/postgres/000001_create_teams.up.sql
channels/db/migrations/postgres/000002_create_team_members.down.sql
@ -428,3 +430,5 @@ channels/db/migrations/postgres/000106_fileinfo_channelid.down.sql
channels/db/migrations/postgres/000106_fileinfo_channelid.up.sql
channels/db/migrations/postgres/000107_threadmemberships_cleanup.down.sql
channels/db/migrations/postgres/000107_threadmemberships_cleanup.up.sql
channels/db/migrations/postgres/000108_remove_orphaned_oauth_preferences.down.sql
channels/db/migrations/postgres/000108_remove_orphaned_oauth_preferences.up.sql

View file

@ -0,0 +1 @@
-- Migration only applied to postgres

View file

@ -0,0 +1 @@
-- Migration only applied to postgres

View file

@ -4,8 +4,7 @@ WITH oauthDelete AS (
DELETE FROM oauthaccessdata o
WHERE NOT EXISTS (
SELECT p.* FROM preferences p
WHERE o.clientid = p.name AND o.userid = p.userid AND p.category = 'oauth_app'
and p.name IS NULL
WHERE o.clientid = p.name AND o.userid = p.userid AND p.category = 'oauth_app'
)
RETURNING o.token
)

View file

@ -0,0 +1 @@
-- Skipping it because the forward migrations are destructive

View file

@ -0,0 +1,33 @@
DO $$
DECLARE
preferences_exist boolean := false;
BEGIN
SELECT count(p.*) != 0 INTO preferences_exist FROM preferences p
WHERE (
(NOT EXISTS (
SELECT o.* FROM oauthaccessdata o
WHERE o.clientid = p.name AND o.userid = p.userid AND p.category = 'oauth_app'
))
AND
(NOT EXISTS (
SELECT oa.* FROM oauthauthdata oa
WHERE oa.clientid = p.name AND oa.userid = p.userid AND p.category = 'oauth_app'
))
)
AND p.category = 'oauth_app';
IF preferences_exist THEN
DELETE FROM preferences p
WHERE (
(NOT EXISTS (
SELECT o.* FROM oauthaccessdata o
WHERE o.clientid = p.name AND o.userid = p.userid AND p.category = 'oauth_app'
))
AND
(NOT EXISTS (
SELECT oa.* FROM oauthauthdata oa
WHERE oa.clientid = p.name AND oa.userid = p.userid AND p.category = 'oauth_app'
))
)
AND p.category = 'oauth_app';
END IF;
END $$;