<commit_before># -*- encoding : utf-8 -*-
task :environment do
end

namespace :cequel do
  namespace :keyspace do
    desc 'Initialize Cassandra keyspace'
    task :create => :environment do
      create!
    end

    desc 'Drop Cassandra keyspace'
    task :drop => :environment do
      drop!
    end
  end

  desc "Synchronize all models defined in `app/models' with Cassandra " \
       "database schema"
  task :migrate => :environment do
    migrate
  end

  desc "Create keyspace and tables for all defined models"
  task :init => %w(keyspace:create migrate)


  desc 'Drop keyspace if exists, then create and migrate'
  task :reset => :environment do
    if Cequel::Record.connection.schema.exists?
      drop!
    end
    create!
    migrate
  end

  def create!
    Cequel::Record.connection.schema.create!
    puts "Created keyspace #{Cequel::Record.connection.name}"
  end


  def drop!
    Cequel::Record.connection.schema.drop!
    puts "Dropped keyspace #{Cequel::Record.connection.name}"
  end

  def migrate
    watch_stack = ActiveSupport::Dependencies::WatchStack.new

    migration_table_names = Set[]
    project_root = defined?(Rails) ? Rails.root : Dir.pwd
    models_dir_path = "#{File.expand_path('app/models', project_root)}/"
    model_files = Dir.glob(File.join(models_dir_path, '**', '*.rb'))
    model_files.sort.each do |file|
      watch_namespaces = ["Object"]
      model_file_name = file.sub(/^#{Regexp.escape(models_dir_path)}/, "")
      dirname = File.dirname(model_file_name)
      watch_namespaces << dirname.classify unless dirname == "."
      watch_stack.watch_namespaces(watch_namespaces)
      require_dependency(file)

      new_constants = watch_stack.new_constants
      if new_constants.empty?
        new_constants << model_file_name.sub(/\.rb$/, "").classify
      end

      new_constants.each do |class_name|
        # rubocop:disable HandleExceptions
        begin
          clazz = class_name.constantize
        rescue LoadError, NameError, RuntimeError
        else
          if clazz.is_a?(Class)
            if clazz.ancestors.include?(Cequel::Record) &&
                !migration_table_names.include?(clazz.table_name.to_sym)
              clazz.synchronize_schema
              migration_table_names << clazz.table_name.to_sym
              puts "Synchronized schema for #{class_name}"
            end
          end
        end
        # rubocop:enable HandleExceptions
      end
    end
  end
end<commit_msg>the current migration tactic depends on re-loading all of the app/model files and detecting any new constants within. refactor to something simpler which is guaranteed to work and not have any namespace issues<commit_after># -*- encoding : utf-8 -*-

unless defined?(Rails)
 task :environment do
  end
end

namespace :cequel do
  namespace :keyspace do
    desc 'Initialize Cassandra keyspace'
    task :create => :environment do
      create!
    end

    desc 'Drop Cassandra keyspace'
    task :drop => :environment do
      drop!
    end
  end

  desc "Synchronize all models defined in `app/models' with Cassandra " \
       "database schema"
  task :migrate => :environment do
    migrate
  end

  desc "Create keyspace and tables for all defined models"
  task :init => %w(keyspace:create migrate)


  desc 'Drop keyspace if exists, then create and migrate'
  task :reset => :environment do
    if Cequel::Record.connection.schema.exists?
      drop!
    end
    create!
    migrate
  end

  def create!
    Cequel::Record.connection.schema.create!
    puts "Created keyspace #{Cequel::Record.connection.name}"
  end


  def drop!
    Cequel::Record.connection.schema.drop!
    puts "Dropped keyspace #{Cequel::Record.connection.name}"
  end

  def migrate
    models = Dir['app/models/**/*.rb'].reject{|f| f.start_with? "app/models/concern"}.map {|f| File.basename(f, '.*').camelize.constantize }
    cequel_models = models.select {|m| m.ancestors.include? Cequel::Record }.uniq 
    cequel_models.each do |model|
      model.synchronize_schema
      puts "Synchronized schema for #{model}"
    end
  end
end
{'fraction_non_alphanumeric': 0.09982872522632738, 'fraction_numerical': 0.0004893564962074871, 'mean_word_length': 2.9612403100775193, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '5494248', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1248, 'n_words': 335}