diff --git a/lib/data_update_scripts/20200917135847_nullify_orphan_rows_from_buffer_updates_by_composer_user_id.rb b/lib/data_update_scripts/20200917135847_nullify_orphan_rows_from_buffer_updates_by_composer_user_id.rb new file mode 100644 index 000000000..ac32a0097 --- /dev/null +++ b/lib/data_update_scripts/20200917135847_nullify_orphan_rows_from_buffer_updates_by_composer_user_id.rb @@ -0,0 +1,15 @@ +module DataUpdateScripts + class NullifyOrphanRowsFromBufferUpdatesByComposerUserId + def run + # Nullify all BufferUpdates composer_user_id belonging to Users that don't exist anymore + ActiveRecord::Base.connection.execute( + <<~SQL.squish, + UPDATE buffer_updates + SET composer_user_id = NULL + WHERE composer_user_id IS NOT NULL + AND composer_user_id NOT IN (SELECT id FROM users); + SQL + ) + end + end +end