From 46524ecb3329f4563552e46d7164385639933d9e Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 17 Apr 2015 12:03:54 +0200 Subject: [PATCH] Fix username period migration to preserve uniqueness of names and paths. --- app/models/namespace.rb | 1 + ...150324133047_remove_periods_at_ends_of_usernames.rb | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/models/namespace.rb b/app/models/namespace.rb index a0d79d7e5c0..e1de114375e 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -59,6 +59,7 @@ class Namespace < ActiveRecord::Base end def clean_path(path) + path = path.dup path.gsub!(/@.*\z/, "") path.gsub!(/\.git\z/, "") path.gsub!(/\A-+/, "") diff --git a/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb b/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb index 7ce53c2a0d6..dc38b0eceb7 100644 --- a/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb +++ b/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb @@ -3,21 +3,21 @@ class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration class Namespace < ActiveRecord::Base class << self - def by_path(path) - where('lower(path) = :value', value: path.downcase).first + def find_by_path_or_name(path) + find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase) end def clean_path(path) path = path.dup path.gsub!(/@.*\z/, "") path.gsub!(/\.git\z/, "") - path.gsub!(/\A-/, "") - path.gsub!(/.\z/, "") + path.gsub!(/\A-+/, "") + path.gsub!(/\.+\z/, "") path.gsub!(/[^a-zA-Z0-9_\-\.]/, "") counter = 0 base = path - while Namespace.by_path(path).present? + while Namespace.find_by_path_or_name(path) counter += 1 path = "#{base}#{counter}" end