From 614de339f014970a76b56ce3f137fab4c6bc2520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Thu, 9 Sep 2021 10:27:35 +0200 Subject: [PATCH] Remove all unused keys from the settings json --- db/migrate/20210902201126_cleanup_user_preferences.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/db/migrate/20210902201126_cleanup_user_preferences.rb b/db/migrate/20210902201126_cleanup_user_preferences.rb index 2a00fb5ae4..c5f32e9aba 100644 --- a/db/migrate/20210902201126_cleanup_user_preferences.rb +++ b/db/migrate/20210902201126_cleanup_user_preferences.rb @@ -12,9 +12,18 @@ class CleanupUserPreferences < ActiveRecord::Migration[6.1] WHERE settings ->> 'warn_on_leaving_unsaved' = '0' SQL + # Remove all other keys from the user preferences + object_map = UserPreferences::Schema.properties.map { |key| "'#{key}', settings->'#{key}'" }.join(", ") execute <<~SQL.squish + WITH subquery AS ( + SELECT id, + jsonb_strip_nulls(jsonb_build_object(#{object_map})) as stripped_settings + FROM user_preferences + ) UPDATE user_preferences - SET settings = settings - 'no_self_notified' + SET settings = subquery.stripped_settings + FROM subquery + WHERE user_preferences.id = subquery.id SQL end