20150324133047_remove_periods_at_ends_of_usernames.rb 2.62 KB
Newer Older
1
class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration
2 3
  include Gitlab::ShellAdapter

4 5
  class Namespace < ActiveRecord::Base
    class << self
6 7
      def find_by_path_or_name(path)
        find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase)
8 9 10
      end

      def clean_path(path)
11
        path = path.dup
12 13
        path.gsub!(/@.*\z/,             "")
        path.gsub!(/\.git\z/,           "")
14 15
        path.gsub!(/\A-+/,              "")
        path.gsub!(/\.+\z/,             "")
16 17 18 19
        path.gsub!(/[^a-zA-Z0-9_\-\.]/, "")

        counter = 0
        base = path
20
        while Namespace.find_by_path_or_name(path)
21 22 23 24 25 26 27 28 29 30
          counter += 1
          path = "#{base}#{counter}"
        end

        path
      end
    end
  end

  def up
31 32
    changed_paths = {}

33
    select_all("SELECT id, username FROM users WHERE username LIKE '%.'").each do |user|
34 35 36 37 38
      username_was = user["username"]
      username = Namespace.clean_path(username_was)
      changed_paths[username_was] = username

      username = quote_string(username)
39
      execute "UPDATE users SET username = '#{username}' WHERE id = #{user["id"]}"
Douwe Maan's avatar
Douwe Maan committed
40
      execute "UPDATE namespaces SET path = '#{username}', name = '#{username}' WHERE type IS NULL AND owner_id = #{user["id"]}"
41 42 43
    end

    select_all("SELECT id, path FROM namespaces WHERE type = 'Group' AND path LIKE '%.'").each do |group|
44 45 46 47 48
      path_was = group["path"]
      path = Namespace.clean_path(path_was)
      changed_paths[path_was] = path

      path = quote_string(path)
49 50
      execute "UPDATE namespaces SET path = '#{path}' WHERE id = #{group["id"]}"
    end
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    changed_paths.each do |path_was, path|
      if gitlab_shell.mv_namespace(path_was, path)
        # If repositories moved successfully we need to remove old satellites
        # and send update instructions to users.
        # However we cannot allow rollback since we moved namespace dir
        # So we basically we mute exceptions in next actions
        begin
          gitlab_shell.rm_satellites(path_was)
          # We cannot send update instructions since models and mailers
          # can't safely be used from migrations as they may be written for 
          # later versions of the database.
          # send_update_instructions
        rescue
          # Returning false does not rollback after_* transaction but gives
          # us information about failing some of tasks
          false
        end
      else
        # if we cannot move namespace directory we should rollback
        # db changes in order to prevent out of sync between db and fs
        raise Exception.new('namespace directory cannot be moved')
      end
    end
75 76
  end
end