FIX: Correct error in user_custom_field migration, rebuild deleted data

This commit is contained in:
David Taylor 2020-01-21 12:24:09 +00:00
parent 134c66d089
commit 3ed42a6a56
2 changed files with 33 additions and 0 deletions

View File

@ -8,6 +8,7 @@ class AddUserNotesCountIndex < ActiveRecord::Migration[5.2]
USING user_custom_fields b
WHERE a.name = 'user_notes_count'
AND a.name = b.name
AND a.user_id = b.user_id
AND a.id > b.id
SQL

View File

@ -0,0 +1,32 @@
# frozen_string_literal: true
class CorrectUserNotesCount < ActiveRecord::Migration[5.2]
# This corrects an error in the previous migration (now fixed)
def up
execute <<~SQL
INSERT INTO user_custom_fields (
user_id,
name,
value,
created_at,
updated_at
) SELECT
REPLACE(key, 'notes:', '')::int,
'user_notes_count',
json_array_length(value::json),
now(),
now()
FROM plugin_store_rows
WHERE plugin_name = 'user_notes'
AND key LIKE 'notes:%'
ON CONFLICT (name, user_id) WHERE name::text = 'user_notes_count'::text
DO NOTHING
SQL
end
def down
# Nothing to do
end
end