From 5adbf6c61268decbb3fff72d495c7e6543035133 Mon Sep 17 00:00:00 2001 From: Ben Cooke Date: Tue, 2 May 2023 12:32:30 -0400 Subject: [PATCH] [MM-52288] Oauth2 postgres migration issue (#23036) * fixing migrations that caused some oauth2 issues --- server/channels/db/migrations/migrations.list | 4 +++ ...remove_orphaned_oauth_preferences.down.sql | 1 + ...8_remove_orphaned_oauth_preferences.up.sql | 1 + .../postgres/000105_remove_tokens.up.sql | 3 +- ...remove_orphaned_oauth_preferences.down.sql | 1 + ...8_remove_orphaned_oauth_preferences.up.sql | 33 +++++++++++++++++++ 6 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 server/channels/db/migrations/mysql/000108_remove_orphaned_oauth_preferences.down.sql create mode 100644 server/channels/db/migrations/mysql/000108_remove_orphaned_oauth_preferences.up.sql create mode 100644 server/channels/db/migrations/postgres/000108_remove_orphaned_oauth_preferences.down.sql create mode 100644 server/channels/db/migrations/postgres/000108_remove_orphaned_oauth_preferences.up.sql diff --git a/server/channels/db/migrations/migrations.list b/server/channels/db/migrations/migrations.list index 47f5bf333bb..eef5b1fe2ac 100644 --- a/server/channels/db/migrations/migrations.list +++ b/server/channels/db/migrations/migrations.list @@ -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 diff --git a/server/channels/db/migrations/mysql/000108_remove_orphaned_oauth_preferences.down.sql b/server/channels/db/migrations/mysql/000108_remove_orphaned_oauth_preferences.down.sql new file mode 100644 index 00000000000..9a11a0a1e83 --- /dev/null +++ b/server/channels/db/migrations/mysql/000108_remove_orphaned_oauth_preferences.down.sql @@ -0,0 +1 @@ +-- Migration only applied to postgres diff --git a/server/channels/db/migrations/mysql/000108_remove_orphaned_oauth_preferences.up.sql b/server/channels/db/migrations/mysql/000108_remove_orphaned_oauth_preferences.up.sql new file mode 100644 index 00000000000..9a11a0a1e83 --- /dev/null +++ b/server/channels/db/migrations/mysql/000108_remove_orphaned_oauth_preferences.up.sql @@ -0,0 +1 @@ +-- Migration only applied to postgres diff --git a/server/channels/db/migrations/postgres/000105_remove_tokens.up.sql b/server/channels/db/migrations/postgres/000105_remove_tokens.up.sql index ac9dc4dc7dc..0a9760ea051 100644 --- a/server/channels/db/migrations/postgres/000105_remove_tokens.up.sql +++ b/server/channels/db/migrations/postgres/000105_remove_tokens.up.sql @@ -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 ) diff --git a/server/channels/db/migrations/postgres/000108_remove_orphaned_oauth_preferences.down.sql b/server/channels/db/migrations/postgres/000108_remove_orphaned_oauth_preferences.down.sql new file mode 100644 index 00000000000..4743bd64621 --- /dev/null +++ b/server/channels/db/migrations/postgres/000108_remove_orphaned_oauth_preferences.down.sql @@ -0,0 +1 @@ +-- Skipping it because the forward migrations are destructive diff --git a/server/channels/db/migrations/postgres/000108_remove_orphaned_oauth_preferences.up.sql b/server/channels/db/migrations/postgres/000108_remove_orphaned_oauth_preferences.up.sql new file mode 100644 index 00000000000..0e0c9a496cb --- /dev/null +++ b/server/channels/db/migrations/postgres/000108_remove_orphaned_oauth_preferences.up.sql @@ -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 $$;