OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/db/migrate/20210902201126_cleanup_user...

68 lines
2.2 KiB

class CleanupUserPreferences < ActiveRecord::Migration[6.1]
def up
execute <<~SQL.squish
UPDATE user_preferences
SET settings = settings - 'warn_on_leaving_unsaved' || '{"warn_on_leaving_unsaved": true}'
WHERE settings ->> 'warn_on_leaving_unsaved' = '1'
SQL
execute <<~SQL.squish
UPDATE user_preferences
SET settings = settings - 'warn_on_leaving_unsaved' || '{"warn_on_leaving_unsaved": false}'
WHERE settings ->> 'warn_on_leaving_unsaved' = '0'
SQL
execute <<~SQL.squish
UPDATE user_preferences
SET settings = settings - 'hide_mail' || '{"hide_mail": false}'
WHERE settings ->> 'hide_mail' = '0'
SQL
execute <<~SQL.squish
UPDATE user_preferences
SET settings = settings - 'hide_mail' || '{"hide_mail": true}'
WHERE settings ->> 'hide_mail' = '1'
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 = subquery.stripped_settings
FROM subquery
WHERE user_preferences.id = subquery.id
SQL
end
def down
execute <<~SQL.squish
UPDATE user_preferences
SET settings = settings - 'warn_on_leaving_unsaved' || '{"warn_on_leaving_unsaved": "1"}'
WHERE settings ->> 'warn_on_leaving_unsaved' = 'true'
SQL
execute <<~SQL.squish
UPDATE user_preferences
SET settings = settings - 'warn_on_leaving_unsaved' || '{"warn_on_leaving_unsaved": "0"}'
WHERE settings ->> 'warn_on_leaving_unsaved' = 'false'
SQL
execute <<~SQL.squish
UPDATE user_preferences
SET settings = settings - 'hide_mail' || '{"hide_mail": 0}'
WHERE settings ->> 'hide_mail' = 'true'
SQL
execute <<~SQL.squish
UPDATE user_preferences
SET settings = settings - 'hide_mail' || '{"hide_mail": 1}'
WHERE settings ->> 'hide_mail' = 'false'
SQL
end
end