From 3ed42a6a564a4019c2bb3f9883a72cb1b9259a0a Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 21 Jan 2020 12:24:09 +0000 Subject: [PATCH] FIX: Correct error in user_custom_field migration, rebuild deleted data --- ...200120140900_add_user_notes_count_index.rb | 1 + ...20200121120800_correct_user_notes_count.rb | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 db/migrate/20200121120800_correct_user_notes_count.rb diff --git a/db/migrate/20200120140900_add_user_notes_count_index.rb b/db/migrate/20200120140900_add_user_notes_count_index.rb index 423ee96..7092c5d 100644 --- a/db/migrate/20200120140900_add_user_notes_count_index.rb +++ b/db/migrate/20200120140900_add_user_notes_count_index.rb @@ -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 diff --git a/db/migrate/20200121120800_correct_user_notes_count.rb b/db/migrate/20200121120800_correct_user_notes_count.rb new file mode 100644 index 0000000..db0ad7a --- /dev/null +++ b/db/migrate/20200121120800_correct_user_notes_count.rb @@ -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