issue_1977_remove_invalid_user_foreign_keys_spec.rb 1.7 KB

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