FIX: Restore trust level when leaving group (#17954)

If a user was granted a trust level, joined a group that granted a trust
level and left the group, the trust level was reset. This commit tries
to restore the last known trust level before joining the group by
looking into staff logs.

This commit also migrates old :change_trust_level user history records
to use previous_value and new_value fields.
This commit is contained in:
Bianca Nenciu
2022-08-29 13:00:48 +03:00
committed by GitHub
parent c4bb15441d
commit 0d8ecab362
7 changed files with 90 additions and 44 deletions

View File

@@ -0,0 +1,20 @@
# frozen_string_literal: true
class MoveTlUserHistoryToPreviousAndNewValue < ActiveRecord::Migration[7.0]
def change
execute <<~SQL
UPDATE user_histories
SET previous_value = old_tl,
new_value = new_tl,
details = NULL
FROM (
SELECT id user_history_id,
(REGEXP_MATCHES(details, 'old trust level: (\d+)', 'i'))[1] old_tl,
(REGEXP_MATCHES(details, 'new trust level: (\d+)', 'i'))[1] new_tl
FROM user_histories
WHERE action = 2
) trust_levels
WHERE user_histories.id = trust_levels.user_history_id
SQL
end
end