<commit_before>class MergeEmployeesInUsers < ActiveRecord::Migration
  COLUMNS = {
    :department_id=>:integer, 
    :establishment_id=>:integer, 
    :arrived_on=>:date, 
    :departed_on=>:date,
    :office=>:string, 
    :comment=>:text, 
    :profession_id=>:integer, 
    :commercial=>:boolean
  }
  def self.change_ids(table, column, conv)
    # Build of CASE WHEN which register all employee_id=>user_id conversion
    casewhen =  "CASE "+conv.collect{|k, v| "WHEN #{column}=#{k} THEN #{v}"}.join(" ")+ " END"
    execute "UPDATE #{table} SET #{column}=#{casewhen}"
  end
  
  def self.up
    add_column :users, :deleted_at, :timestamp
    execute "UPDATE users SET deleted_at = updated_at WHERE deleted = #{quoted_true}"
    remove_column :users, :deleted

    for k, v in COLUMNS.stringify_keys.sort
      add_column :users, k, v
    end
    add_column :users, :employed,   :boolean, :null=>false, :default=>false
    add_column :users, :employment, :string

    # puts "#{User.all.size} users"
    execute "INSERT INTO users (company_id, created_at, updated_at, first_name, last_name, language_id, name, role_id, office, admin) "+
      "SELECT company_id, created_at, updated_at, first_name, last_name, 0, LOWER(first_name||'.'||last_name), 0, id, #{quoted_false} FROM employees WHERE user_id IS NULL"
    # puts "#{User.all.size} users"
    for user in select_all("SELECT * FROM users WHERE language_id=0")
#      user.password = ([0]*(rand*10+3).to_i).collect{|x| rand.to_s[2..-1].to_i.to_s(36)}.join
#      user.password_confirmation = <PASSWORD>
#      user.language = user.company.entity.language
#      user.role = user.company.roles.find(:first, :order=>"LENGTH(rights)")
#      user.name = user.name.lower_ascii.gsub(/\W/, "")
#      user.save!
      execute "UPDATE employees SET user_id = #{user['id']} WHERE id = #{user['office']}"
      # puts user.inspect
    end

    employees = {}
    for employee in select_all("SELECT * FROM employees")
      updates = []
      hash = {}
      COLUMNS.each do |k,v| 
        updates << k.to_s+"="+if v == :boolean
                                ['1', 't', 'T', 'true'].include?(employee[k]) ? quoted_true : quoted_false
                              elsif v == :integer
                                employee[k].to_s
                              else 
                                quote(employee[k])
                              end
        # hash[k] = employee[k.to_s]}
      end
      updates << "employed=#{quoted_true}"
      updates << "employment="+quote(employee['role'])
      #hash[:employed] = true
      #hash[:employment] = employee['role']
      # puts hash.inspect
      execute "UPDATE users SET #{updates.join(', ')} WHERE id=#{employee['user_id']}"
      employees[employee['id'].to_s] = employee['user_id']
    end

    if employees.keys.size > 0
      change_ids(:entities,         :employee_id,    employees)
      change_ids(:events,           :employee_id,    employees)
      change_ids(:inventories,      :employee_id,    employees)
      change_ids(:sale_orders,      :responsible_id, employees)
      change_ids(:shape_operations,       :employee_id,    employees)
      change_ids(:transports,       :responsible_id, employees)
    end
<commit_msg>Fixes the same bug again<commit_after>class MergeEmployeesInUsers < ActiveRecord::Migration
  COLUMNS = {
    :department_id=>:integer, 
    :establishment_id=>:integer, 
    :arrived_on=>:date, 
    :departed_on=>:date,
    :office=>:string, 
    :comment=>:text, 
    :profession_id=>:integer, 
    :commercial=>:boolean
  }
  def self.change_ids(table, column, conv)
    # Build of CASE WHEN which register all employee_id=>user_id conversion
    casewhen =  "CASE "+conv.collect{|k, v| "WHEN #{column}=#{k} THEN #{v}"}.join(" ")+ " END"
    execute "UPDATE #{table} SET #{column}=#{casewhen}"
  end
  
  def self.up
    add_column :users, :deleted_at, :timestamp
    execute "UPDATE users SET deleted_at = updated_at WHERE deleted = #{quoted_true}"
    remove_column :users, :deleted

    for k, v in COLUMNS.stringify_keys.sort
      add_column :users, k, v
    end
    add_column :users, :employed,   :boolean, :null=>false, :default=>false
    add_column :users, :employment, :string

    # puts "#{User.all.size} users"
    execute "INSERT INTO users (company_id, created_at, updated_at, first_name, last_name, language_id, name, role_id, office, admin) "+
      "SELECT company_id, created_at, updated_at, first_name, last_name, 0, LOWER(first_name||'.'||last_name), 0, id, #{quoted_false} FROM employees WHERE user_id IS NULL"
    # puts "#{User.all.size} users"
    for user in select_all("SELECT * FROM users WHERE language_id=0")
#      user.password = ([0]*(rand*10+3).to_i).collect{|x| rand.to_s[2..-1].to_i.to_s(36)}.join
#      user.password_confirmation = <PASSWORD>
#      user.language = user.company.entity.language
#      user.role = user.company.roles.find(:first, :order=>"LENGTH(rights)")
#      user.name = user.name.lower_ascii.gsub(/\W/, "")
#      user.save!
      execute "UPDATE employees SET user_id = #{user['id']} WHERE id = #{user['office']}"
      # puts user.inspect
    end

    employees = {}
    for employee in select_all("SELECT * FROM employees")
      updates = []
      hash = {}
      COLUMNS.each do |k,v| 
        updates << k.to_s+"="+if v == :boolean
                                ['1', 't', 'T', 'true'].include?(employee[k]) ? quoted_true : quoted_false
                              elsif v == :integer
                                (employee[k]||0).to_s
                              else 
                                quote(employee[k])
                              end
        # hash[k] = employee[k.to_s]}
      end
      updates << "employed=#{quoted_true}"
      updates << "employment="+quote(employee['role'].to_s)
      #hash[:employed] = true
      #hash[:employment] = employee['role']
      # puts hash.inspect
      execute "UPDATE users SET #{updates.join(', ')} WHERE id=#{employee['user_id']}"
      employees[employee['id'].to_s] = employee['user_id']
    end

    if employees.keys.size > 0
      change_ids(:entities,         :employee_id,    employees)
      change_ids(:events,           :employee_id,    employees)
      change_ids(:inventories,      :employee_id,    employees)
      change_ids(:sale_orders,      :responsible_id, employees)
      change_ids(:shape_operations,       :employee_id,    employees)
      change_ids(:transports,       :responsible_id, employees)
    end

{'fraction_non_alphanumeric': 0.1435149654643131, 'fraction_numerical': 0.004144282425172678, 'mean_word_length': 2.7234285714285713, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, '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': '1558481', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2067, 'n_words': 448}