Merge pull request #9647 from opf/fix/38835/remove-unused-keys-from-json

[38835] Remove all unused keys from the settings json
pull/9650/head
ulferts 3 years ago committed by GitHub
commit 43ac5ce758
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      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

Loading…
Cancel
Save