From 4790f652fdabea51517e05cf1312654d69890293 Mon Sep 17 00:00:00 2001 From: Koza Date: Thu, 11 Jan 2024 14:55:54 +0100 Subject: [PATCH] Deduplicate transferred / moved repos --- app/models/repo.rb | 23 ++++++++++++++++++++--- test/jobs/update_repo_info_job_test.rb | 18 ++++++------------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/app/models/repo.rb b/app/models/repo.rb index 8ac9294f..ea620004 100644 --- a/app/models/repo.rb +++ b/app/models/repo.rb @@ -8,8 +8,8 @@ class Repo < ActiveRecord::Base validate :github_url_exists, on: :create, unless: :skip_validation validates :name, :user_name, presence: true - has_many :issues - has_many :repo_subscriptions + has_many :issues, dependent: :destroy + has_many :repo_subscriptions, dependent: :restrict_with_error has_many :users, through: :repo_subscriptions has_many :subscribers, through: :repo_subscriptions, source: :user has_many :doc_classes, dependent: :destroy @@ -194,7 +194,24 @@ def update_from_github repo_full_name = fetcher_json.fetch("full_name", full_name) if repo_full_name != full_name && self.class.exists_with_name?(repo_full_name) - # TODO: Add deduplication step + ActiveRecord::Base.transaction do + new_repo = self.class.find_by(full_name: repo_full_name) + + repo_subscriptions.each do |existing_subscription| + new_repo.repo_subscriptions.find_or_create_by!(user_id: existing_subscription.user_id) do |new_subscription| + new_subscription.last_sent_at = existing_subscription.last_sent_at + new_subscription.email_limit = existing_subscription.email_limit + new_subscription.write = existing_subscription.write + new_subscription.read = existing_subscription.read + new_subscription.write_limit = existing_subscription.write_limit + new_subscription.read_limit = existing_subscription.read_limit + end + end + + repo_subscriptions.destroy_all + destroy! + end + return end diff --git a/test/jobs/update_repo_info_job_test.rb b/test/jobs/update_repo_info_job_test.rb index 42eca6cb..4c2e7e7e 100644 --- a/test/jobs/update_repo_info_job_test.rb +++ b/test/jobs/update_repo_info_job_test.rb @@ -56,19 +56,13 @@ class UpdateRepoInfoJobTest < ActiveJob::TestCase "full_name" => "sinatra/sinatra" } ) - repo = repos(:node) - assert_no_changes -> { - [ - repo.full_name, - repo.name, - repo.user_name, - repo.language, - repo.description, - repo.archived - ] - } do + repo = repos(:rails_rails) + sinatra = repos(:sinatra_sinatra) + + assert_difference "Repo.count", -1 do UpdateRepoInfoJob.perform_now(repo) - repo.reload end + + assert_equal 2, sinatra.subscribers.count end end