Remove all unused keys from the settings json

pull/9647/head
Oliver Günther 3 years ago
parent b0c44b7fdc
commit 614de339f0
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  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