<commit_before># Used during testing to generate and load database information and connection strings
class DatabaseGenerator
  # Returns a mysql connection (using the database.yml)
  def self.mysql_connection
    @sql_connection ||= Mongify::Database::SqlConnection.new( :adapter => CONNECTION_CONFIG.mysql['adapter'], 
                                                            :host => CONNECTION_CONFIG.mysql['host'], 
                                                            :port => CONNECTION_CONFIG.mysql['port'],
                                                            :username => CONNECTION_CONFIG.mysql['username'],
                                                            :password => CONNECTION_CONFIG.mysql['password'],
                                                            :database => CONNECTION_CONFIG.mysql['database']
                                                          )
  end
  
  # Returns a sqlite connection (using the database.yml )
  def self.sqlite_connection
    @db_path = File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), CONNECTION_CONFIG.sqlite['database'])
    
    @sqlite_connecton ||= Mongify::Database::SqlConnection.new(:adapter => CONNECTION_CONFIG.sqlite['adapter'], :database => @db_path)
  end
  
  # Creates a new mysql database (and deletes the old one)
  def self.sqlite(include_data=true)
    File.delete(sqlite_connection.database) if File.exists?(sqlite_connection.database)

    conn = sqlite_connection.connection

    #SETUP TABLES
    conn.create_table(:users) do |t|
      t.string :first_name, :last_name
      t.timestamps
    end

    conn.create_table(:posts) do |t|
      t.string :title
      t.integer :owner_id
      t.text :body
      t.datetime :published_at
      t.timestamps
    end

    conn.create_table(:comments) do |t|
      t.text :body
      t.integer :post_id
      t.integer :user_id
      t.timestamps
    end
    
    conn.create_table(:preferences) do |t|
      t.integer :user_id
      t.boolean :notify_by_email
      t.timestamps
    end
    
    conn.create_table(:notes) do |t|
      t.integer :user_id
      t.integer :notable_id
      t.string :notable_type
      t.text :body
      t.timestamps
    end
    
    if include_data
      
      #Users
      [ 
        {:first_name => 'Timmy', :last_name => 'Zuza'},
        {:first_name => 'Bob', :last_name => 'Smith'},
        {:first_name => 'Joe', :last_name => 'Franklin'}
      ].each do |values|
        conn.insert("INSERT INTO users (first_name, last_name, created_at, updated_at) VALUES ('#{values[:first_name]}', '#{values[:last_name]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
      end
      
      #Posts
      [ 
        {:title => 'First Post', :owner_id => 1, :body => 'First Post Body', :published_at => (Time.now - 2).to_s(:db)},
        {:title => 'Second Post', :owner_id => 1, :body => 'Second Post Body', :published_at => (Time.now - 1).to_s(:db)},
        {:title => 'Third Post', :owner_id => 2, :body => 'Thrid Post Body', :published_at => (Time.now).to_s(:db)},
      ].each do |v|
        conn.insert("INSERT INTO posts (title, owner_id, body, published_at, created_at, updated_at) 
                    VALUES ('#{v[:title]}', #{v[:owner_id]}, '#{v[:body]}', '#{v[:published_at]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
      end
      
      #Comments
      [
        {:post_id => 1, :user_id => 1, :body => 'First Comment Body'},
        {:post_id => 2, :user_id => 1, :body => 'Second Comment Body'},
        {:post_id => 2, :user_id => 2, :body => 'Thrid Comment Body'},
      ].each do |v|
        conn.insert("INSERT INTO comments (body, post_id, user_id, created_at, updated_at) 
                    VALUES ('#{v[:body]}', #{v[:post_id]}, #{v[:user_id]}, '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
      end
      
      #Preferences
      [
        {:user_id => 1, :notify_by_email => true},
        {:user_id => 2, :notify_by_email => true},
        {:user_id => 3, :notify_by_email => false},
      ].each do |v|
          conn.insert("INSERT INTO preferences (user_id, notify_by_email, created_at, updated_at) 
                      VALUES (#{v[:user_id]}, '#{v[:notify_by_email]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
      end<commit_msg>Changed preferences to :string id (for testing)<commit_after># Used during testing to generate and load database information and connection strings
class DatabaseGenerator
  # Returns a mysql connection (using the database.yml)
  def self.mysql_connection
    @sql_connection ||= Mongify::Database::SqlConnection.new( :adapter => CONNECTION_CONFIG.mysql['adapter'], 
                                                            :host => CONNECTION_CONFIG.mysql['host'], 
                                                            :port => CONNECTION_CONFIG.mysql['port'],
                                                            :username => CONNECTION_CONFIG.mysql['username'],
                                                            :password => CONNECTION_CONFIG.mysql['password'],
                                                            :database => CONNECTION_CONFIG.mysql['database']
                                                          )
  end
  
  # Returns a sqlite connection (using the database.yml )
  def self.sqlite_connection
    @db_path = File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), CONNECTION_CONFIG.sqlite['database'])
    
    @sqlite_connecton ||= Mongify::Database::SqlConnection.new(:adapter => CONNECTION_CONFIG.sqlite['adapter'], :database => @db_path)
  end
  
  # Creates a new mysql database (and deletes the old one)
  def self.sqlite(include_data=true)
    File.delete(sqlite_connection.database) if File.exists?(sqlite_connection.database)

    conn = sqlite_connection.connection

    #SETUP TABLES
    conn.create_table(:users) do |t|
      t.string :first_name, :last_name
      t.timestamps
    end

    conn.create_table(:posts) do |t|
      t.string :title
      t.integer :owner_id
      t.text :body
      t.datetime :published_at
      t.timestamps
    end

    conn.create_table(:comments) do |t|
      t.text :body
      t.integer :post_id
      t.integer :user_id
      t.timestamps
    end
    
    conn.create_table(:preferences, :id => false) do |t|
      t.string :id
      t.integer :user_id
      t.boolean :notify_by_email
      t.timestamps
    end
    
    conn.create_table(:notes) do |t|
      t.integer :user_id
      t.integer :notable_id
      t.string :notable_type
      t.text :body
      t.timestamps
    end
    
    if include_data
      
      #Users
      [ 
        {:first_name => 'Timmy', :last_name => 'Zuza'},
        {:first_name => 'Bob', :last_name => 'Smith'},
        {:first_name => 'Joe', :last_name => 'Franklin'}
      ].each do |values|
        conn.insert("INSERT INTO users (first_name, last_name, created_at, updated_at) VALUES ('#{values[:first_name]}', '#{values[:last_name]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
      end
      
      #Posts
      [ 
        {:title => 'First Post', :owner_id => 1, :body => 'First Post Body', :published_at => (Time.now - 2).to_s(:db)},
        {:title => 'Second Post', :owner_id => 1, :body => 'Second Post Body', :published_at => (Time.now - 1).to_s(:db)},
        {:title => 'Third Post', :owner_id => 2, :body => 'Thrid Post Body', :published_at => (Time.now).to_s(:db)},
      ].each do |v|
        conn.insert("INSERT INTO posts (title, owner_id, body, published_at, created_at, updated_at) 
                    VALUES ('#{v[:title]}', #{v[:owner_id]}, '#{v[:body]}', '#{v[:published_at]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
      end
      
      #Comments
      [
        {:post_id => 1, :user_id => 1, :body => 'First Comment Body'},
        {:post_id => 2, :user_id => 1, :body => 'Second Comment Body'},
        {:post_id => 2, :user_id => 2, :body => 'Thrid Comment Body'},
      ].each do |v|
        conn.insert("INSERT INTO comments (body, post_id, user_id, created_at, updated_at) 
                    VALUES ('#{v[:body]}', #{v[:post_id]}, #{v[:user_id]}, '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
      end
      
      #Preferences
      [
        {:user_id => 1, :notify_by_email => true},
        {:user_id => 2, :notify_by_email => true},
        {:user_id => 3, :notify_by_email => false},
      ].each_with_index do |v, idx|
          conn.insert("INSERT INTO preferences (id, user_id, notify_by_email, created_at, updated_at) 
                      VALUES ('p#{idx+1}',#{v[:user_id]}, '#{v[:notify_by_email]}', '#{Time.now.to_s(:db)}', '#{Time.now.to_s(:db)}')")
      end
{'fraction_non_alphanumeric': 0.15481506927745334, 'fraction_numerical': 0.00332073743272644, 'mean_word_length': 2.248047601338788, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3727268', 'n_tokens_mistral': 2780, 'n_tokens_neox': 2685, 'n_words': 589}