<commit_before>class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration
  include Gitlab::ShellAdapter

  class Namespace < ActiveRecord::Base
    class << self
      def by_path(path)
        where('lower(path) = :value', value: path.downcase).first
      end

      def clean_path(path)
        path = path.dup
        path.gsub!(/@.*\z/,             "")
        path.gsub!(/\.git\z/,           "")
        path.gsub!(/\A-/,               "")
        path.gsub!(/.\z/,               "")
        path.gsub!(/[^a-zA-Z0-9_\-\.]/, "")

        counter = 0
        base = path
        while Namespace.by_path(path).present?
          counter += 1
          path = "#{base}#{counter}"
        end

        path
      end
    end
  end

  def up
    changed_paths = {}

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

      username = quote_string(username)
      execute "UPDATE users SET username = '#{username}' WHERE id = #{user["id"]}"
      execute "UPDATE namespaces SET path = '#{username}', name = '#{username}' WHERE type = NULL AND owner_id = #{user["id"]}"
    end

    select_all("SELECT id, path FROM namespaces WHERE type = 'Group' AND path LIKE '%.'").each do |group|
      path_was = group["path"]
      path = Namespace.clean_path(path_was)
      changed_paths[path_was] = path
<commit_msg>Fix migration SQL.<commit_after>class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration
  include Gitlab::ShellAdapter

  class Namespace < ActiveRecord::Base
    class << self
      def by_path(path)
        where('lower(path) = :value', value: path.downcase).first
      end

      def clean_path(path)
        path = path.dup
        path.gsub!(/@.*\z/,             "")
        path.gsub!(/\.git\z/,           "")
        path.gsub!(/\A-/,               "")
        path.gsub!(/.\z/,               "")
        path.gsub!(/[^a-zA-Z0-9_\-\.]/, "")

        counter = 0
        base = path
        while Namespace.by_path(path).present?
          counter += 1
          path = "#{base}#{counter}"
        end

        path
      end
    end
  end

  def up
    changed_paths = {}

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

      username = quote_string(username)
      execute "UPDATE users SET username = '#{username}' WHERE id = #{user["id"]}"
      execute "UPDATE namespaces SET path = '#{username}', name = '#{username}' WHERE type IS NULL AND owner_id = #{user["id"]}"
    end

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

{'fraction_non_alphanumeric': 0.14237743451981194, 'fraction_numerical': 0.002686366689053056, 'mean_word_length': 2.203225806451613, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1372127', 'n_tokens_mistral': 903, 'n_tokens_neox': 890, 'n_words': 217}