<commit_before># Tenant tasks
namespace :tenant do
  namespace :multi_database do
    task drop: :environment do
      multi_database = ENV['MULTI_DATABASE'].to_i
      if multi_database > 0
        database = Rails.configuration.database_configuration[Rails.env]['database']
        (16**multi_database).times do |i|
          name = database + '_' + i.to_s(16).rjust(multi_database, '0')
          ActiveRecord::Base.connection.execute("DROP DATABASE IF EXISTS #{name}")
        end
      end
    end

    task distribute: :environment do
      beginning = (ENV['FROM'] || ENV['MULTI_DATABASE'] || 0).to_i
      finish = (ENV['TO'] || ENV['MULTI_DATABASE'] || 1).to_i
      if beginning == finish
        puts 'Nothing to do. No change wanted.'
        exit 0
      end
      database = Rails.configuration.database_configuration[Rails.env]['database']
      now = Time.now.to_i.to_s(36)
      Ekylibre::Tenant.list.each do |tenant|
        start = Time.now
        source = Ekylibre::Tenant.database_for(tenant, beginning)
        destination = Ekylibre::Tenant.database_for(tenant, finish)
        puts "Moving schema #{tenant} from #{source} to #{destination}...".cyan
        # Warn if source and destination don't exist
        Ekylibre::Tenant.switch_to_database(source)
        source_exists = Apartment.connection.schema_exists? tenant
        Ekylibre::Tenant.create_database_for!(tenant, finish)
        Ekylibre::Tenant.switch_to_database(destination)
        destination_exists = Apartment.connection.schema_exists? tenant

        if source_exists && destination_exists
          puts "For #{tenant}, source and destination exist. Destination is removed".red
          Ekylibre::Tenant.with_pg_env(tenant) { `psql -c 'DROP SCHEMA "#{tenant}" CASCADE' #{destination}` }
        elsif !source_exists && !destination_exists
          puts "For #{tenant}, no source and no destination exist".red
        end

        # Already migrated
        if !source_exists && destination_exists
          puts "For #{tenant}, no source and destination exist, schema already migrated".yellow
          next
        end

        dump = "tmp/distribute-#{beginning}-#{finish}-#{now}-#{tenant}.sql"

        # Dump source
        Ekylibre::Tenant.with_pg_env(tenant) { `pg_dump -s -x -O -f #{dump} -n "#{tenant}" #{source}` }

        # Restore destination
        Ekylibre::Tenant.with_pg_env(tenant) { `psql -f #{dump} #{destination}` }

        # Remove source and dump
        Ekylibre::Tenant.with_pg_env(tenant) { `psql -c 'DROP SCHEMA "#{tenant}" CASCADE' #{source}` }
        # FileUtils.rm_rf(dump)

        puts "Schema #{tenant} moved from #{source} to #{destination} in #{Time.now - start} seconds".green
      end
    end
  end

  namespace :agg do
    # Create aggregation schema
    desc 'Create the aggregation schema'
    task create: :environment do<commit_msg>Fixes main bugs on tenant distribution<commit_after># Tenant tasks
namespace :tenant do
  namespace :multi_database do
    task drop: :environment do
      multi_database = ENV['MULTI_DATABASE'].to_i
      if multi_database > 0
        database = Rails.configuration.database_configuration[Rails.env]['database']
        (16**multi_database).times do |i|
          name = database + '_' + i.to_s(16).rjust(multi_database, '0')
          ActiveRecord::Base.connection.execute("DROP DATABASE IF EXISTS #{name}")
        end
      end
    end

    task distribute: :environment do
      beginning = (ENV['FROM'] || ENV['MULTI_DATABASE'] || 0).to_i
      finish = (ENV['TO'] || ENV['MULTI_DATABASE'] || 1).to_i
      if beginning == finish
        puts 'Nothing to do. No change wanted.'
        exit 0
      end
      database = Rails.configuration.database_configuration[Rails.env]['database']
      now = Time.now.to_i.to_s(36)
      Ekylibre::Tenant.list.each do |tenant|
        start = Time.now
        source = Ekylibre::Tenant.database_for(tenant, beginning)
        destination = Ekylibre::Tenant.database_for(tenant, finish)
        puts "Moving schema #{tenant} from #{source} to #{destination}...".cyan
        # Warn if source and destination don't exist
        Ekylibre::Tenant.switch_to_database(source)
        source_exists = Apartment.connection.schema_exists? tenant
        Ekylibre::Tenant.create_database_for!(tenant, finish)
        Ekylibre::Tenant.switch_to_database(destination)
        destination_exists = Apartment.connection.schema_exists? tenant

        if source_exists && destination_exists
          puts "For #{tenant}, source and destination exist. Destination is removed".red
          Ekylibre::Tenant.with_pg_env(tenant) { `psql -c 'DROP SCHEMA "#{tenant}" CASCADE' #{destination}` }
        elsif !source_exists && !destination_exists
          puts "For #{tenant}, no source and no destination exist".red
          next
        end

        # Already migrated
        if !source_exists && destination_exists
          puts "For #{tenant}, no source and destination exist, schema already migrated".yellow
          next
        end

        dump = "tmp/distribute-#{beginning}-#{finish}-#{now}-#{tenant}.sql"

        # Dump source
        Ekylibre::Tenant.with_pg_env(tenant) { `pg_dump -x -O -f #{dump} -n "#{tenant}" #{source}` }

        # Restore destination
        Ekylibre::Tenant.with_pg_env(tenant) { `psql -f #{dump} #{destination}` }

        # Remove source and dump
        # Ekylibre::Tenant.with_pg_env(tenant) { `psql -c 'DROP SCHEMA "#{tenant}" CASCADE' #{source}` }
        # FileUtils.rm_rf(dump)

        puts "Schema #{tenant} moved from #{source} to #{destination} in #{Time.now - start} seconds".green
      end
    end
  end

  namespace :agg do
    # Create aggregation schema
    desc 'Create the aggregation schema'
    task create: :environment do
{'fraction_non_alphanumeric': 0.11815512178269132, 'fraction_numerical': 0.003800310934531007, 'mean_word_length': 2.8911290322580645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1032612', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1773, 'n_words': 436}