<commit_before>namespace :cartodb do
  namespace :db do

    ########################
    # LOAD CARTODB FUNCTIONS
    ########################
    desc "Install/upgrade CARTODB SQL functions"
    task :load_functions => :environment do
      User.all.each do |user|
        next if !user.respond_to?('database_name') || user.database_name.blank?        
        user.load_cartodb_functions
      end
    end

    ############################
    # RUN CARTODB FUNCTION TESTS
    # cartodb:db:test_functions
    ############################
    desc "Run CARTODB SQL functions tests"
    task :test_functions => :environment do
      User.all.each do |user|
        next if !user.respond_to?('database_name') || user.database_name.blank?
        
        user.test_cartodb_functions
      end
    end
        
    ##############
    # SET DB PERMS
    ##############
    desc "Set DB Permissions"
    task :set_permissions => :environment do
      User.all.each do |user|
        next if !user.respond_to?('database_name') || user.database_name.blank?

        # reset perms
        user.set_database_permissions

        # rebuild public access perms from redis
        user.tables.all.each do |table|
          
          # reset public
          if table.public?
            user.in_database(:as => :superuser).run("GRANT SELECT ON #{table.name} TO #{CartoDB::PUBLIC_DB_USER};")
          end
          
          # reset triggers
          table.add_python
          table.set_trigger_update_updated_at
          table.set_trigger_cache_timestamp
          table.set_trigger_check_quota
        end  
      end
    end


    ################
    # SET DISK QUOTA
    ################
    desc "reset all users quota to 100mb or use current setting"
    task :reset_quotas => :environment do
      User.all.each do |user|
        next if !user.respond_to?('database_name') || user.database_name.blank?
        
        #user.update(:quota_in_bytes => 104857600) if user.quota_in_bytes.blank?
                
        # rebuild quota trigger
        user.tables.all.each do |table|
        
          # reset quota trigger
          table.add_python
          table.set_trigger_check_quota rescue puts("FAIL #{table.name}")
        end  
      end
    end

    desc "set users quota to amount in mb"
    task :set_user_quota, [:username, :quota_in_mb] => :environment do |t, args|
      usage = "usage: rake cartodb:db:set_user_quota[username,quota_in_mb]"
      raise usage if args[:username].blank? || args[:quota_in_mb].blank?
      
      user  = User.filter(:username => args[:username]).first
      quota = args[:quota_in_mb].to_i * 1024 * 1024<commit_msg>Adds rake task to reset the check_quota trigger on all tables<commit_after>namespace :cartodb do
  namespace :db do

    ########################
    # LOAD CARTODB FUNCTIONS
    ########################
    desc "Install/upgrade CARTODB SQL functions"
    task :load_functions => :environment do
      User.all.each do |user|
        next if !user.respond_to?('database_name') || user.database_name.blank?        
        user.load_cartodb_functions
      end
    end

    ############################
    # RUN CARTODB FUNCTION TESTS
    # cartodb:db:test_functions
    ############################
    desc "Run CARTODB SQL functions tests"
    task :test_functions => :environment do
      User.all.each do |user|
        next if !user.respond_to?('database_name') || user.database_name.blank?
        
        user.test_cartodb_functions
      end
    end
        
    ##############
    # SET DB PERMS
    ##############
    desc "Set DB Permissions"
    task :set_permissions => :environment do
      User.all.each do |user|
        next if !user.respond_to?('database_name') || user.database_name.blank?

        # reset perms
        user.set_database_permissions

        # rebuild public access perms from redis
        user.tables.all.each do |table|
          
          # reset public
          if table.public?
            user.in_database(:as => :superuser).run("GRANT SELECT ON #{table.name} TO #{CartoDB::PUBLIC_DB_USER};")
          end
          
          # reset triggers
          table.add_python
          table.set_trigger_update_updated_at
          table.set_trigger_cache_timestamp
          table.set_trigger_check_quota
        end  
      end
    end

    ##########################
    # SET TRIGGER CHECK QUOTA
    ##########################
    desc "reset check quota trigger on all user tables"
    task :reset_trigger_check_quota => :environment do
      User.all.each do |user|
        next if !user.respond_to?('database_name') || user.database_name.blank?

        # rebuild quota trigger
        user.tables.all.each do |table|
          begin
            table.add_python
            table.set_trigger_check_quota
            puts "OK #{user.username} => #{table.name}"
          rescue => e
            puts "FAIL #{user.username} => #{table.name} #{e.message}"
          end
        end
      end
    end

    desc "set users quota to amount in mb"
    task :set_user_quota, [:username, :quota_in_mb] => :environment do |t, args|
      usage = "usage: rake cartodb:db:set_user_quota[username,quota_in_mb]"
      raise usage if args[:username].blank? || args[:quota_in_mb].blank?
      
      user  = User.filter(:username => args[:username]).first
      quota = args[:quota_in_mb].to_i * 1024 * 1024
{'fraction_non_alphanumeric': 0.1484837278106509, 'fraction_numerical': 0.005177514792899409, 'mean_word_length': 2.3721945137157108, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '1528685', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1563, 'n_words': 375}