issue_1977_remove_invalid_user_foreign_keys_spec.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Issue1977RemoveInvalidUserForeignKeys, type: :db_migration do
  4. context 'no online_notifications foreign key' do
  5. let(:existing_user_id) { User.first.id }
  6. context 'invalid User foreign key columns' do
  7. it 'cleans up OnlineNotification#user_id', db_strategy: :reset do
  8. without_foreign_key(:online_notifications, column: :user_id)
  9. OnlineNotification.without_callback :commit, :after, :trigger_subscriptions do
  10. create(:online_notification, user_id: 1337)
  11. create(:online_notification, user_id: existing_user_id)
  12. end
  13. expect do
  14. migrate
  15. end.to change(OnlineNotification, :count).by(-1)
  16. end
  17. it 'cleans up RecentView#created_by_id', db_strategy: :reset do
  18. without_foreign_key(:online_notifications, column: :user_id)
  19. without_foreign_key(:recent_views, column: :created_by_id)
  20. record = build(:recent_view, created_by_id: 1337)
  21. record.save(validate: false)
  22. create(:recent_view, created_by_id: existing_user_id)
  23. expect do
  24. migrate
  25. end.to change(RecentView, :count).by(-1)
  26. end
  27. it 'cleans up Avatar#o_id', db_strategy: :reset do
  28. without_foreign_key(:online_notifications, column: :user_id)
  29. create(:avatar, object_lookup_id: ObjectLookup.by_name('User'), o_id: 1337)
  30. create(:avatar, object_lookup_id: ObjectLookup.by_name('Ticket'), o_id: 1337)
  31. create(:avatar, object_lookup_id: ObjectLookup.by_name('User'), o_id: existing_user_id)
  32. expect do
  33. migrate
  34. end.to change(Avatar, :count).by(-1)
  35. end
  36. end
  37. it 'adds OnlineNotification#user_id foreign key' do
  38. adds_foreign_key(:online_notifications, column: :user_id)
  39. end
  40. end
  41. end