<commit_before>namespace :db do
  namespace :anonymizer do
    desc "Remove created schema"
    task :remove_schema => :environment do
      ActiveRecord::Base.connection.execute(
        "DROP SCHEMA IF EXISTS #{ActiveRecordAnonymizer::Anonymizer.default_schema_name} CASCADE"
      )
    end

    desc "Creates mandatory schema"
    task :create_schema => :environment do
      ActiveRecord::Base.connection.execute(
        "CREATE SCHEMA IF NOT EXISTS #{ActiveRecordAnonymizer::Anonymizer.default_schema_name}"
      )
    end

    desc "Generates all the views for the models"
    task :generate_views => :environment do
      Rails.application.eager_load! if defined?(Rails)

      generated_views = {}
      ActiveRecord::Base.connection.schema_cache.clear!
      ActiveRecord::Base.descendants.each do |c|
        if c.respond_to?(:generate_anonymized_view?) && c.generate_anonymized_view? && !generated_views[c.table_name] && ActiveRecord::Base.connection.table_exists?(c.table_name)
          generated_views[c.table_name] = true
          puts "Generating view '#{c.view_name}' for table '#{c.table_name}'"

          c.reset_column_information
          ActiveRecord::Base.connection.execute(
            "CREATE OR REPLACE VIEW #{c.view_name} AS " +
            "(SELECT #{c.view_columns.join(', ')} " +
            "FROM #{c.table_name})"
          )
        end
      end
    end
  end
end

Rake::Task["db:migrate"].enhance ["db:anonymizer:create_schema"] do
  Rake::Task["db:anonymizer:generate_views"].invoke
end<commit_msg>before migrating, remove schema to avoid change type errors<commit_after>namespace :db do
  namespace :anonymizer do
    desc "Remove created schema"
    task :remove_schema => :environment do
      ActiveRecord::Base.connection.execute(
        "DROP SCHEMA IF EXISTS #{ActiveRecordAnonymizer::Anonymizer.default_schema_name} CASCADE"
      )
    end

    desc "Creates mandatory schema"
    task :create_schema => :environment do
      ActiveRecord::Base.connection.execute(
        "CREATE SCHEMA IF NOT EXISTS #{ActiveRecordAnonymizer::Anonymizer.default_schema_name}"
      )
    end

    desc "Generates all the views for the models"
    task :generate_views => :environment do
      Rails.application.eager_load! if defined?(Rails)

      generated_views = {}
      ActiveRecord::Base.connection.schema_cache.clear!
      ActiveRecord::Base.descendants.each do |c|
        if c.respond_to?(:generate_anonymized_view?) && c.generate_anonymized_view? && !generated_views[c.table_name] && ActiveRecord::Base.connection.table_exists?(c.table_name)
          generated_views[c.table_name] = true
          puts "Generating view '#{c.view_name}' for table '#{c.table_name}'"

          c.reset_column_information
          ActiveRecord::Base.connection.execute(
            "CREATE OR REPLACE VIEW #{c.view_name} AS " +
            "(SELECT #{c.view_columns.join(', ')} " +
            "FROM #{c.table_name})"
          )
        end
      end
    end
  end
end

Rake::Task["db:migrate"].enhance ["db:anonymizer:remove_schema", "db:anonymizer:create_schema"] do
  Rake::Task["db:anonymizer:generate_views"].invoke
end
{'fraction_non_alphanumeric': 0.1120607787274454, 'fraction_numerical': 0.0, 'mean_word_length': 3.328767123287671, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3786240', 'n_tokens_mistral': 966, 'n_tokens_neox': 956, 'n_words': 196}