<commit_before>class MoveCanPurchaseIntoPricePolicy < ActiveRecord::Migration
  def self.up
    add_column :price_policies, :can_purchase, :boolean, :after => :price_group_id, :default => false, :null => false
    PricePolicy.reset_column_information
    PriceGroupProduct.all.each do |pgp|
      price_policies = PricePolicy.find_all_by_price_group_id_and_product_id(pgp.price_group_id, pgp.product_id)
      if price_policies.any?
        price_policies.each do |pp|
          Rails.logger.debug "updating can_purchase #{pp.id}"
          # don't do validation
          pp.update_attribute(:can_purchase,  1)
        end
      else
        puts "Didn't find any policies for #{pgp.product} | #{pgp.price_group}"
        # skip bundles
        next if pgp.product.type == "Bundle"
        model_class = "#{pgp.product.type}PricePolicy".constantize
        pp = model_class.new(:product => pgp.product,
                             :price_group => pgp.price_group,
                             :can_purchase => true,
                             :start_date => Time.zone.now,
                             #TODO use SettingsHelper from fiscal_year branch
                             :expire_date => fiscal_year_end(Time.zone.now))
        pp.save!(:validate => false)
      end
    end
  end

  def self.down
    remove_column :price_policies, :can_purchase
    PricePolicy.where(:unit_cost => nil, :usage_rate => nil).each do |pp|
      pp.destroy
    end
  end

  def self.fiscal_year_end(date=nil)
    date ||= Time.zone.now
    (fiscal_year_beginning(date) + 1.year- 1.day).end_of_day
  end

  def self.fiscal_year_beginning(date=nil)
    date ||= Time.zone.now
    fiscal_year_starts = fiscal_year(date.year) 
    date.to_time >= fiscal_year_starts ? fiscal_year_starts : fiscal_year_starts - 1.year
  end
  def self.fiscal_year(year)
    Time.zone.parse("#{year}-09-01").beginning_of_day
  end
end<commit_msg>make new policies expired fixes to migration<commit_after>class MoveCanPurchaseIntoPricePolicy < ActiveRecord::Migration
  def self.up
    add_column :price_policies, :can_purchase, :boolean, :after => :price_group_id, :default => false, :null => false
    
    PricePolicy.reset_column_information
    InstrumentPricePolicy.reset_column_information
    ServicePricePolicy.reset_column_information
    ItemPricePolicy.reset_column_information

    PriceGroupProduct.all.each do |pgp|
      price_policies = PricePolicy.find_all_by_price_group_id_and_product_id(pgp.price_group_id, pgp.product_id)
      if price_policies.any?
        price_policies.each do |pp|
          Rails.logger.debug "updating can_purchase #{pp.id}"
          # don't do validation
          pp.update_attribute(:can_purchase,  1)
        end
      else
        next if pgp.product.type == "Bundle" # skip bundles since they don't have their own price policies
        next if pgp.product.is_archived? # Ignore inactive products

        puts "Didn't find any policies for #{pgp.product.facility} ##{pgp.product.id} | #{pgp.product} | #{pgp.price_group}"
        model_class = "#{pgp.product.type}PricePolicy".constantize
        # create an expired policy
        pp = model_class.new(:product => pgp.product,
                             :price_group => pgp.price_group,
                             :can_purchase => true,
                             :start_date => 1.year.ago,
                             :expire_date => 1.year.ago)
        begin
          pp.save!(:validate => false)
        rescue Exception => e
          puts e.message
          puts e.backtrace
        end
      end
    end
  end

  def self.down
    remove_column :price_policies, :can_purchase
    PricePolicy.reset_column_information
    InstrumentPricePolicy.reset_column_information
    ServicePricePolicy.reset_column_information
    ItemPricePolicy.reset_column_information

    PricePolicy.where(:unit_cost => nil, :usage_rate => nil, :can_purchase => true).each do |pp|
      pp.destroy
    end
  end

end
{'fraction_non_alphanumeric': 0.09214160180768265, 'fraction_numerical': 0.0027617373838814963, 'mean_word_length': 2.595667870036101, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '3019401', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1228, 'n_words': 243}