<commit_before>module RunTracker
  module CommandLoader
    module SetGameAlias
      extend Discordrb::Commands::CommandContainer

      command(:setgamealias, description: '',
                         usage: "!setgamealias <old alias> <new alias>\nAlias must be unique.",
                         min_args: 2,
                         max_args: 2) do |_event, _oldAlias, _newAlias|

        # check if the newly provided alias is valid
        if !/[^a-zA-Z0-9\-()&:%]./.match(_newAlias).nil?
          return :usage
        end

        PostgresDB::Conn.transaction do |conn|

          # Check to see if alias even exists
          conn.prepare("find_alias", "SELECT * FROM public.\"aliases\" WHERE alias= $1 and type='game'")
          aliasResults = conn.exec_prepared('find_alias', _oldAlias)
          if aliasResults.ntuples < 1
            return "Game Alias not found use !listgames to see the current aliases"
          end
          conn.exec('DEALLOCATE find_alias')

          # Set the alias for the game, and then change the prefix for any category aliases
          conn.prepare("update_game_alias", "update public.aliases set alias=$1 where alias=$2 and type = 'game'")
          conn.exec_prepared('update_game_alias', [_newAlias, _oldAlias])
          conn.exec('DEALLOCATE update_game_alias')

          categoryAliases = conn.exec("SELECT * FROM public.aliases WHERE type='category'")
          conn.prepare("update_category_prefix", "update public.aliases set alias = $1 where alias = $2 and type = 'category'")
          categoryAliases.each do |catAlias|
            prefix = catAlias['alias'].split('-').first
            withPrefixRemoved = catAlias['alias'].split('-')
            withPrefixRemoved[0] = "#{_newAlias}-"
            newCategoryAlias = withPrefixRemoved.join('')
            if prefix.casecmp(_oldAlias).zero?
              conn.exec_prepared('update_category_prefix', ["#{newCategoryAlias}", catAlias['alias']])
            end
          end
          conn.exec('DEALLOCATE update_category_prefix')

        end
        # TODO add some output for errors

        return
      end # end command body
    end # end SetGameAlias module
  end
end<commit_msg>updating the game alias will now properly update the resource aliases as well<commit_after>module RunTracker
  module CommandLoader
    module SetGameAlias
      extend Discordrb::Commands::CommandContainer

      command(:setgamealias, description: '',
                         usage: "!setgamealias <old alias> <new alias>\nAlias must be unique.",
                         min_args: 2,
                         max_args: 2) do |_event, _oldAlias, _newAlias|

        # check if the newly provided alias is valid
        if !/[^a-zA-Z0-9\-()&:%]./.match(_newAlias).nil?
          return :usage
        end

        PostgresDB::Conn.transaction do |conn|

          # Check to see if alias even exists
          conn.prepare("find_alias", "SELECT * FROM public.\"aliases\" WHERE alias= $1 and type='game'")
          aliasResults = conn.exec_prepared('find_alias', [_oldAlias])
          if aliasResults.ntuples < 1
            return "Game Alias not found use !listgames to see the current aliases"
          end
          conn.exec('DEALLOCATE find_alias')

          # Set the alias for the game, and then change the prefix for any category aliases
          conn.prepare("update_game_alias", "update public.aliases set alias=$1 where alias=$2 and type = 'game'")
          conn.exec_prepared('update_game_alias', [_newAlias, _oldAlias])
          conn.exec('DEALLOCATE update_game_alias')

          categoryAliases = conn.exec("SELECT * FROM public.aliases WHERE type='category'")
          conn.prepare("update_category_prefix", "update public.aliases set alias = $1 where alias = $2 and type = 'category'")
          categoryAliases.each do |catAlias|
            prefix = catAlias['alias'].split('-').first
            withPrefixRemoved = catAlias['alias'].split('-')
            withPrefixRemoved[0] = "#{_newAlias}-"
            newCategoryAlias = withPrefixRemoved.join('')
            if prefix.casecmp(_oldAlias).zero?
              conn.exec_prepared('update_category_prefix', ["#{newCategoryAlias}", catAlias['alias']])
            end
          end
          conn.exec('DEALLOCATE update_category_prefix')

          # Set the alias for any existing resources
          conn.prepare('update_resource_aliases', "update public.resources set game_alias=$1 where game_alias=$2")
          conn.exec_prepared('update_resource_aliases', [_newAlias, _oldAlias])
          conn.exec('DEALLOCATE update_resource_aliases')

        end

        _event << "Alias updated from #{_oldAlias} to #{_newAlias}"
        # TODO add some output for errors

        return
      end # end command body
    end # end SetGameAlias module
  end
end
{'fraction_non_alphanumeric': 0.10330578512396695, 'fraction_numerical': 0.0049586776859504135, 'mean_word_length': 2.460328806290207, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '5819837', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1274, 'n_words': 369}