Skip to content

Commit

Permalink
Merge pull request #2836 from adrianschroeter/fix_migration
Browse files Browse the repository at this point in the history
[api] don't fail on existing double repository entries
  • Loading branch information
adrianschroeter committed Mar 17, 2017
2 parents e2abc10 + 881f566 commit e0c554a
Showing 1 changed file with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
class ChangeRepositoriesRemoteProjectNameToNotNull < ActiveRecord::Migration[5.0]
def up
execute 'UPDATE repositories SET remote_project_name = "" WHERE remote_project_name is null'
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

change_column_null :repositories, :remote_project_name, false
change_column_default :repositories, :remote_project_name, ''
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'

change_column_null :repositories, :remote_project_name, false
change_column_default :repositories, :remote_project_name, ''
end
end

def down
Expand Down

0 comments on commit e0c554a

Please sign in to comment.