<commit_before>require "houcho/database"

module Houcho

class OuterRole
  module Save
    def save_outer_role(outerrole, datasource)
      db = Houcho::Database.new.handle
      db.transaction do

      delete_ids = db.execute("SELECT id FROM outerrole").flatten

      outerrole.each do |outerrole, hosts|
        begin
          db.execute("INSERT INTO outerrole(name, data_source) VALUES(?,?)", outerrole, datasource)
        rescue SQLite3::ConstraintException, "column name is not unique"
        ensure
          outerrole_id = db.execute("SELECT id FROM outerrole WHERE name = ?", outerrole).flatten.first
        end

        delete_ids.delete(outerrole_id)
        db.execute("DELETE FROM outerrole_host WHERE outerrole_id = ?", outerrole_id)

        hosts.each do |host|
          begin
            db.execute("INSERT INTO host(name) VALUES(?)", host)
          rescue SQLite3::ConstraintException, "column name is not unique"
          ensure
            begin
              db.execute(
                "INSERT INTO outerrole_host(outerrole_id, host_id) VALUES(?,?)",
                outerrole_id,
                db.execute("SELECT id FROM host WHERE name = ?", host).flatten.first
              )
            rescue SQLite3::ConstraintException, "column name is not unique"
            end
          end
        end
      end

      delete_ids.each do |id|
        db.execute("DELETE FROM outerrole_host WHERE outerrole_id = ?", id)
      end

      end #end of transaction
    end
  end
end

end
<commit_msg>fix bug of loading outer role<commit_after>require "houcho/database"

module Houcho

class OuterRole
  module Save
    def save_outer_role(outerrole, datasource)
      db = Houcho::Database.new.handle
      db.transaction do

      delete_ids = db.execute("SELECT id FROM outerrole WHERE data_source = ?", datasource).flatten

      outerrole.each do |outerrole, hosts|
        begin
          db.execute("INSERT INTO outerrole(name, data_source) VALUES(?,?)", outerrole, datasource)
        rescue SQLite3::ConstraintException, "column name is not unique"
        ensure
          outerrole_id = db.execute("SELECT id FROM outerrole WHERE name = ?", outerrole).flatten.first
        end

        delete_ids.delete(outerrole_id)
        db.execute("DELETE FROM outerrole_host WHERE outerrole_id = ?", outerrole_id)

        hosts.each do |host|
          begin
            db.execute("INSERT INTO host(name) VALUES(?)", host)
          rescue SQLite3::ConstraintException, "column name is not unique"
          ensure
            begin
              db.execute(
                "INSERT INTO outerrole_host(outerrole_id, host_id) VALUES(?,?)",
                outerrole_id,
                db.execute("SELECT id FROM host WHERE name = ?", host).flatten.first
              )
            rescue SQLite3::ConstraintException, "column name is not unique"
            end
          end
        end
      end

      delete_ids.each do |id|
        db.execute("DELETE FROM outerrole_host WHERE outerrole_id = ?", id)
      end

      end #end of transaction
    end
  end
end

end

{'fraction_non_alphanumeric': 0.0842479018721756, 'fraction_numerical': 0.001936733376371853, 'mean_word_length': 2.2247658688865766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2947062', 'n_tokens_mistral': 874, 'n_tokens_neox': 848, 'n_words': 242}