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