Skip to content

Commit

Permalink
[api] fix migration to only delete one of each duplicate pair of repo…
Browse files Browse the repository at this point in the history
…sitories
  • Loading branch information
evanrolfe committed Mar 17, 2017
1 parent 21a6922 commit 881f566
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
class ChangeRepositoriesRemoteProjectNameToNotNull < ActiveRecord::Migration[5.0]
def up
transaction do
Repository.transaction do
sql = <<-SQL
SELECT a.*
FROM repositories AS a
LEFT JOIN repositories AS b
ON a.db_project_id = b.db_project_id
AND a.name = b.name
WHERE a.id != b.id
GROUP BY a.db_project_id, a.name
SQL

repos_with_duplicates = Repository.find_by_sql(sql)

sql = <<-SQL
SELECT b.*
FROM repositories a
LEFT JOIN repositories AS b
ON a.db_project_id = b.db_project_id
AND a.name = b.name
WHERE a.id != b.id
AND a.id IN (#{repos_with_duplicates.map(&:id).join(', ')})
SQL

duplicate_repos = Repository.find_by_sql(sql)

# must be a ruby loop for the path_elements and reference cleanup
duplicate_repos.each(&:destroy)

execute 'UPDATE repositories SET remote_project_name = "" WHERE remote_project_name is null'
# drop existsing double entries, must be a ruby loop for the path_elements and reference cleanup
Repository.find_by_sql("select A.* from repositories as A LEFT JOIN repositories as B ON A.db_project_id = B.db_project_id and A.name = B.name where A.id != B.id").each.destroy

change_column_null :repositories, :remote_project_name, false
change_column_default :repositories, :remote_project_name, ''
Expand Down

0 comments on commit 881f566

Please sign in to comment.