From e4381a441a7066c5ff2dece01960be1c55ea87cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Thu, 4 Nov 2021 16:19:21 +0100 Subject: [PATCH 1/6] [38671] Reset all notifications, remove previous migration attempts https://community.openproject.org/wp/38671 --- ...0210618132206_add_notification_settings.rb | 93 ------------------- ...1104151329_default_notification_setting.rb | 19 ++++ 2 files changed, 19 insertions(+), 93 deletions(-) create mode 100644 db/migrate/20211104151329_default_notification_setting.rb diff --git a/db/migrate/20210618132206_add_notification_settings.rb b/db/migrate/20210618132206_add_notification_settings.rb index 92086c36c9..97ad8dc26b 100644 --- a/db/migrate/20210618132206_add_notification_settings.rb +++ b/db/migrate/20210618132206_add_notification_settings.rb @@ -22,10 +22,6 @@ class AddNotificationSettings < ActiveRecord::Migration[6.1] name: 'index_notification_settings_unique_project' end - insert_project_specific_channel - insert_default_mail_channel - insert_default_in_app_channel - remove_column :members, :mail_notification end @@ -33,95 +29,6 @@ class AddNotificationSettings < ActiveRecord::Migration[6.1] add_column :members, :mail_notification, :boolean, default: false, null: false add_column :users, :mail_notification, :string, default: '', null: false - update_mail_notifications - drop_table :notification_settings end - - def insert_default_mail_channel - execute <<~SQL - INSERT INTO - notification_settings - (user_id, - channel, - watched, - involved, - mentioned, - "all") - SELECT - id, - 1, - mail_notification = 'only_my_events', - mail_notification = 'only_my_events' OR mail_notification = 'only_assigned', - NOT mail_notification = 'all' AND NOT mail_notification = 'NONE', - mail_notification = 'all' - FROM - users - WHERE - mail_notification IS NOT NULL - SQL - end - - def insert_project_specific_channel - execute <<~SQL - INSERT INTO - notification_settings - (project_id, - user_id, - channel, - "all") - SELECT - project_id, - user_id, - 1, - true - FROM - members - WHERE - mail_notification - SQL - end - - def insert_default_in_app_channel - execute <<~SQL - INSERT INTO - notification_settings - (user_id, - channel, - involved, - mentioned, - watched) - SELECT - id, - 0, - true, - true, - true - FROM - users - WHERE - type = 'User' - SQL - end - - def update_mail_notifications - # We cannot reconstruct the settings completely - execute <<~SQL - UPDATE - users - SET - mail_notification = CASE - WHEN notification_settings.all - THEN 'all' - WHEN notification_settings.watched - THEN 'only_my_events' - WHEN notification_settings.involved - THEN 'only_assigned' - ELSE 'none' - FROM - notification_settings - WHERE - notification_settings.user_id = users.id - SQL - end end diff --git a/db/migrate/20211104151329_default_notification_setting.rb b/db/migrate/20211104151329_default_notification_setting.rb new file mode 100644 index 0000000000..51e717bb5e --- /dev/null +++ b/db/migrate/20211104151329_default_notification_setting.rb @@ -0,0 +1,19 @@ +class DefaultNotificationSetting < ActiveRecord::Migration[6.1] + def up + NotificationSetting.delete_all + + execute <<~SQL.squish + INSERT INTO + notification_settings + (user_id, watched, involved, mentioned) + SELECT + id, true, true, true + FROM + users + SQL + end + + def down + # No data to revert + end +end From 2f6765520596f2281b6608ddcfee3cc76ccf8cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Mon, 8 Nov 2021 08:08:37 +0100 Subject: [PATCH 2/6] Set a default mail_notification on down migration --- db/migrate/20210618132206_add_notification_settings.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/db/migrate/20210618132206_add_notification_settings.rb b/db/migrate/20210618132206_add_notification_settings.rb index 97ad8dc26b..29b1ead028 100644 --- a/db/migrate/20210618132206_add_notification_settings.rb +++ b/db/migrate/20210618132206_add_notification_settings.rb @@ -30,5 +30,8 @@ class AddNotificationSettings < ActiveRecord::Migration[6.1] add_column :users, :mail_notification, :string, default: '', null: false drop_table :notification_settings + + User.reset_column_information + User.update_all(mail_notification: 'only_assigned') end end From ca38c4cef76c4a995fdd84307a0835f40e74426f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Mon, 8 Nov 2021 08:08:46 +0100 Subject: [PATCH 3/6] Fix query in default notification setup --- db/migrate/20211104151329_default_notification_setting.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/db/migrate/20211104151329_default_notification_setting.rb b/db/migrate/20211104151329_default_notification_setting.rb index 51e717bb5e..6aef31ae4a 100644 --- a/db/migrate/20211104151329_default_notification_setting.rb +++ b/db/migrate/20211104151329_default_notification_setting.rb @@ -10,6 +10,7 @@ class DefaultNotificationSetting < ActiveRecord::Migration[6.1] id, true, true, true FROM users + WHERE type = 'User' SQL end From 89a4ef53a63bc65fde57ff384c2448b337e96884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Mon, 8 Nov 2021 08:09:12 +0100 Subject: [PATCH 4/6] Remove mail_notification column --- db/migrate/20210618132206_add_notification_settings.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/db/migrate/20210618132206_add_notification_settings.rb b/db/migrate/20210618132206_add_notification_settings.rb index 29b1ead028..23c411d469 100644 --- a/db/migrate/20210618132206_add_notification_settings.rb +++ b/db/migrate/20210618132206_add_notification_settings.rb @@ -23,6 +23,7 @@ class AddNotificationSettings < ActiveRecord::Migration[6.1] end remove_column :members, :mail_notification + remove_column :users, :mail_notification end def down From 1e736cbdf01dff67ea371e68d67778139341fbb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Mon, 8 Nov 2021 08:10:16 +0100 Subject: [PATCH 5/6] Remove more notification migrations --- .../20210701073944_add_digest_setting.rb | 24 +------------------ .../20210914065555_cleanup_notifications.rb | 19 --------------- 2 files changed, 1 insertion(+), 42 deletions(-) diff --git a/db/migrate/20210701073944_add_digest_setting.rb b/db/migrate/20210701073944_add_digest_setting.rb index 910066157e..cfcb97c073 100644 --- a/db/migrate/20210701073944_add_digest_setting.rb +++ b/db/migrate/20210701073944_add_digest_setting.rb @@ -1,34 +1,12 @@ class AddDigestSetting < ActiveRecord::Migration[6.1] def up - insert_default_digest_channel + # No-op end def down remove_digest_channels end - def insert_default_digest_channel - execute <<~SQL - INSERT INTO - notification_settings - (user_id, - channel, - involved, - mentioned, - watched) - SELECT - id, - 2, - true, - true, - true - FROM - users - WHERE - type = 'User' - SQL - end - # Removes all digest channels. Includes non default channels as those might # also have been added not by the migration but in the cause of the functionality # the migration was added for. diff --git a/db/migrate/20210914065555_cleanup_notifications.rb b/db/migrate/20210914065555_cleanup_notifications.rb index e1a15b4441..9de1b20ec8 100644 --- a/db/migrate/20210914065555_cleanup_notifications.rb +++ b/db/migrate/20210914065555_cleanup_notifications.rb @@ -55,25 +55,6 @@ class CleanupNotifications < ActiveRecord::Migration[6.1] where: "project_id IS NOT NULL", name: 'index_notification_settings_unique_project' end - - # Set all channels to ian - execute <<~SQL.squish - UPDATE notification_settings SET channel = 0; - SQL - - # Restore notification settings - execute <<~SQL.squish - INSERT INTO notification_settings - (project_id, user_id, channel, watched, involved, mentioned, - work_package_commented, work_package_created, work_package_processed, work_package_prioritized, work_package_scheduled) - SELECT project_id, user_id, channel + 1, watched, involved, mentioned, - work_package_commented, work_package_created, work_package_processed, work_package_prioritized, work_package_scheduled - FROM notification_settings - UNION - SELECT project_id, user_id, channel + 2, watched, involved, mentioned, - work_package_commented, work_package_created, work_package_processed, work_package_prioritized, work_package_scheduled - FROM notification_settings; - SQL end # rubocop:enable Metrics/AbcSize end From 1c018f85a1bbf6456629ccb2c92a721850f31f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Mon, 8 Nov 2021 08:27:07 +0100 Subject: [PATCH 6/6] Remove mail_notification from legacy fixtures --- spec_legacy/fixtures/users.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/spec_legacy/fixtures/users.yml b/spec_legacy/fixtures/users.yml index 96ecb679bf..1ea2406779 100644 --- a/spec_legacy/fixtures/users.yml +++ b/spec_legacy/fixtures/users.yml @@ -84,7 +84,6 @@ users_003: firstname: Dave id: 3 auth_source_id: - mail_notification: all login: dlopper type: User first_login: false @@ -101,7 +100,6 @@ users_005: lastname: Lopper2 firstname: Dave2 auth_source_id: - mail_notification: all login: dlopper2 type: User first_login: false @@ -132,7 +130,6 @@ users_007: lastname: One firstname: Some auth_source_id: - mail_notification: only_my_events login: someone type: User first_login: false @@ -148,7 +145,6 @@ users_008: lastname: Misc firstname: User auth_source_id: - mail_notification: only_my_events login: miscuser8 type: User first_login: false @@ -164,7 +160,6 @@ users_009: lastname: Misc firstname: User auth_source_id: - mail_notification: only_my_events login: miscuser9 type: User first_login: false