diff --git a/app/models/user_profile_view.rb b/app/models/user_profile_view.rb index 716b26c07c3..237318a101c 100644 --- a/app/models/user_profile_view.rb +++ b/app/models/user_profile_view.rb @@ -1,5 +1,5 @@ class UserProfileView < ActiveRecord::Base - validates_presence_of :user_profile_id, :ip_address, :viewed_at + validates_presence_of :user_profile_id, :viewed_at belongs_to :user_profile @@ -9,6 +9,7 @@ class UserProfileView < ActiveRecord::Base if user_id return if user_id < 1 redis_key << ":user-#{user_id}" + ip = nil else redis_key << ":ip-#{ip}" end @@ -59,13 +60,12 @@ end # id :integer not null, primary key # user_profile_id :integer not null # viewed_at :datetime not null -# ip_address :inet not null +# ip_address :inet # user_id :integer # # Indexes # # index_user_profile_views_on_user_id (user_id) # index_user_profile_views_on_user_profile_id (user_profile_id) -# unique_profile_view_ip (viewed_at,ip_address,user_profile_id) UNIQUE -# unique_profile_view_user (viewed_at,user_id,user_profile_id) UNIQUE +# unique_profile_view_user_or_ip (viewed_at,user_id,ip_address,user_profile_id) UNIQUE # diff --git a/db/migrate/20180521191418_allow_null_ip_user_profile_view.rb b/db/migrate/20180521191418_allow_null_ip_user_profile_view.rb new file mode 100644 index 00000000000..0f5b3795c17 --- /dev/null +++ b/db/migrate/20180521191418_allow_null_ip_user_profile_view.rb @@ -0,0 +1,25 @@ +class AllowNullIpUserProfileView < ActiveRecord::Migration[5.1] + def up + begin + Migration::SafeMigrate.disable! + change_column :user_profile_views, :ip_address, :inet, null: true + ensure + Migration::SafeMigrate.enable! + end + + remove_index :user_profile_views, + column: [:viewed_at, :ip_address, :user_profile_id], + name: :unique_profile_view_ip, + unique: true + remove_index :user_profile_views, + column: [:viewed_at, :user_id, :user_profile_id], + name: :unique_profile_view_user, + unique: true + add_index :user_profile_views, [:viewed_at, :user_id, :ip_address, :user_profile_id], + name: :unique_profile_view_user_or_ip, + unique: true + end + + def down + end +end diff --git a/spec/models/user_profile_view_spec.rb b/spec/models/user_profile_view_spec.rb index de82b167c75..108a5a7a5f2 100644 --- a/spec/models/user_profile_view_spec.rb +++ b/spec/models/user_profile_view_spec.rb @@ -34,6 +34,8 @@ RSpec.describe UserProfileView do ['1.1.1.1', '2.2.2.2'].each do |ip| add(user_profile_id, ip, other_user.id, time) expect(described_class.count).to eq(1) + # should not actually log IPs + expect(UserProfileView.where(user_id: other_user.id).count(:ip_address)).to eq(0) end end