Sha256: 1a95b3cc87762f0e3a6bae6ab957d899f1d369c846138f1ced5d4a72052959d1

Contents?: true

Size: 1.9 KB

Versions: 7

Compression:

Stored size: 1.9 KB

Contents

module Renalware
  Drugs::Drug.transaction do
    reset_sequence_sql = nil

    log "Adding Drug Types" do
      Drugs::Drug.transaction do

        %w(Antibiotic ESA Immunosuppressant Peritonitis Controlled).each_with_index do |drug_type, index|
          Drugs::Type.find_or_create_by!(code: drug_type.downcase) do |type|
            type.id = index + 1
            type.name = drug_type
          end
        end

        reset_sequence_sql = <<-SQL.squish
          SELECT setval('%{table_name}_id_seq', (SELECT MAX(id) FROM %{table_name}));
        SQL
        ActiveRecord::Base.connection.execute(
          reset_sequence_sql % { table_name: Renalware::Drugs::Type.table_name }
        )
      end
    end

    log "Adding Drugs" do
      file_path = File.join(File.dirname(__FILE__), "drugs.csv")

      if Drugs::Drug.count == 0
        # Use #import! which is 4 times faster
        drugs = CSV.read(file_path, headers: false)
        columns = drugs[0]
        Drugs::Drug.import! columns, drugs[1..-1], validate: true
      else
        # There are already drugs so use an idempotent approach
        CSV.foreach(file_path, headers: true) do |row|
          Drugs::Drug.find_or_create_by!(name: row["name"]) do |drug|
            drug.id = row["id"]
          end
        end
      end
    end

    log "Assigning Drug Types to Drugs" do
      ActiveRecord::Base.connection.execute(
        reset_sequence_sql % { table_name: Renalware::Drugs::Drug.table_name }
      )

      file_path = File.join(File.dirname(__FILE__), "drug_drug_types.csv")

      CSV.foreach(file_path, headers: true) do |row|
        drug = Drugs::Drug.find(row["drug_id"])
        drug_type = Drugs::Type.find(row["drug_type_id"])
        drug.drug_types << drug_type unless drug.drug_types.include?(drug_type)
      end
    end

    log "Adding Blood Transfusion drug" do
      Drugs::Drug.find_or_create_by!(name: "Blood Transfusion")
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
renalware-core-2.0.0.pre.beta10 db/seeds/default/drugs/seeds.rb
renalware-core-2.0.0.pre.beta9 db/seeds/default/drugs/seeds.rb
renalware-core-2.0.0.pre.beta8 db/seeds/default/drugs/seeds.rb
renalware-core-2.0.0.pre.beta7 db/seeds/default/drugs/seeds.rb
renalware-core-2.0.0.pre.beta6 db/seeds/default/drugs/seeds.rb
renalware-core-2.0.0.pre.beta5 db/seeds/default/drugs/seeds.rb
renalware-core-2.0.0.pre.beta4 db/seeds/default/drugs/seeds.rb