From 592e4fb5c01090168056a56368fa0e64d81ea7a5 Mon Sep 17 00:00:00 2001 From: Michael Kochell <6913320+mickmister@users.noreply.github.com> Date: Wed, 20 Apr 2022 11:05:53 -0400 Subject: [PATCH] [MM-43500] Fix MattermostAppID null values with NOT NULL constraint (#20028) --- ...2_upgrade_oauth_mattermost_app_id.down.sql | 14 +++++++++ ...082_upgrade_oauth_mattermost_app_id.up.sql | 29 +++++++++++++++++++ ...2_upgrade_oauth_mattermost_app_id.down.sql | 13 +++++++++ ...082_upgrade_oauth_mattermost_app_id.up.sql | 14 +++++++++ 4 files changed, 70 insertions(+) create mode 100644 db/migrations/mysql/000082_upgrade_oauth_mattermost_app_id.down.sql create mode 100644 db/migrations/mysql/000082_upgrade_oauth_mattermost_app_id.up.sql create mode 100644 db/migrations/postgres/000082_upgrade_oauth_mattermost_app_id.down.sql create mode 100644 db/migrations/postgres/000082_upgrade_oauth_mattermost_app_id.up.sql diff --git a/db/migrations/mysql/000082_upgrade_oauth_mattermost_app_id.down.sql b/db/migrations/mysql/000082_upgrade_oauth_mattermost_app_id.down.sql new file mode 100644 index 0000000000..981177557c --- /dev/null +++ b/db/migrations/mysql/000082_upgrade_oauth_mattermost_app_id.down.sql @@ -0,0 +1,14 @@ +SET @preparedStatement = (SELECT IF( + ( + SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS + WHERE table_name = 'OAuthApps' + AND table_schema = DATABASE() + AND column_name = 'MattermostAppID' + ) > 0, + 'ALTER TABLE OAuthApps MODIFY MattermostAppID varchar(32);', + 'SELECT 1' +)); + +PREPARE alterIfExists FROM @preparedStatement; +EXECUTE alterIfExists; +DEALLOCATE PREPARE alterIfExists; diff --git a/db/migrations/mysql/000082_upgrade_oauth_mattermost_app_id.up.sql b/db/migrations/mysql/000082_upgrade_oauth_mattermost_app_id.up.sql new file mode 100644 index 0000000000..aa65f0de26 --- /dev/null +++ b/db/migrations/mysql/000082_upgrade_oauth_mattermost_app_id.up.sql @@ -0,0 +1,29 @@ +SET @preparedStatement = (SELECT IF( + ( + SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS + WHERE table_name = 'OAuthApps' + AND table_schema = DATABASE() + AND column_name = 'MattermostAppID' + ) > 0, + 'UPDATE OAuthApps SET MattermostAppID = "" WHERE MattermostAppID IS NULL;', + 'SELECT 1' +)); + +PREPARE alterIfExists FROM @preparedStatement; +EXECUTE alterIfExists; +DEALLOCATE PREPARE alterIfExists; + +SET @preparedStatement = (SELECT IF( + ( + SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS + WHERE table_name = 'OAuthApps' + AND table_schema = DATABASE() + AND column_name = 'MattermostAppID' + ) > 0, + 'ALTER TABLE OAuthApps MODIFY MattermostAppID varchar(32) NOT NULL DEFAULT "";', + 'SELECT 1' +)); + +PREPARE alterIfExists FROM @preparedStatement; +EXECUTE alterIfExists; +DEALLOCATE PREPARE alterIfExists; diff --git a/db/migrations/postgres/000082_upgrade_oauth_mattermost_app_id.down.sql b/db/migrations/postgres/000082_upgrade_oauth_mattermost_app_id.down.sql new file mode 100644 index 0000000000..163eb60aff --- /dev/null +++ b/db/migrations/postgres/000082_upgrade_oauth_mattermost_app_id.down.sql @@ -0,0 +1,13 @@ +DO $$ +DECLARE + column_exist boolean := false; +BEGIN +SELECT count(*) != 0 INTO column_exist + FROM information_schema.columns + WHERE table_name = 'oauthapps' + AND column_name = 'mattermostappid'; +IF column_exist THEN + ALTER TABLE OAuthApps ALTER COLUMN MattermostAppID DROP NOT NULL; + ALTER TABLE OAuthApps ALTER COLUMN MattermostAppID DROP DEFAULT; +END IF; +END $$; diff --git a/db/migrations/postgres/000082_upgrade_oauth_mattermost_app_id.up.sql b/db/migrations/postgres/000082_upgrade_oauth_mattermost_app_id.up.sql new file mode 100644 index 0000000000..7192a87385 --- /dev/null +++ b/db/migrations/postgres/000082_upgrade_oauth_mattermost_app_id.up.sql @@ -0,0 +1,14 @@ +DO $$ +DECLARE + column_exist boolean := false; +BEGIN +SELECT count(*) != 0 INTO column_exist + FROM information_schema.columns + WHERE table_name = 'oauthapps' + AND column_name = 'mattermostappid'; +IF column_exist THEN + UPDATE OAuthApps SET MattermostAppID = '' WHERE MattermostAppID IS NULL; + ALTER TABLE OAuthApps ALTER COLUMN MattermostAppID SET DEFAULT ''; + ALTER TABLE OAuthApps ALTER COLUMN MattermostAppID SET NOT NULL; +END IF; +END $$;