Last active
June 30, 2017 13:21
-
-
Save ldlsegovia/32a0e1da45085645e8182e6a08fce9c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV['RAILS_ENV'] = ARGV[0] || 'production' | |
DIR = File.dirname(__FILE__) | |
require DIR + '/config/environment' | |
class LoadOldValues | |
include PricingLogger | |
def initialize(realm) | |
raise 'invalid realm' unless ["gasco", "enex"].include?(realm) | |
@realm = realm | |
end | |
def attrs | |
{ | |
"id" => :integer, | |
"price_id" => :integer, | |
"value" => :integer, | |
"price_changed_at" => :datetime | |
} | |
end | |
def query | |
PriceChange.select(attrs.keys) | |
.where(realm: @realm) | |
.order(:price_id, :price_changed_at) | |
.to_sql | |
end | |
def update_query(prev_record, current_record) | |
prev_value = prev_record[2] | |
prev_price_changed_at = prev_record[3].to_datetime.strftime('%Y-%m-%d %H:%M:%S') | |
<<~SQL | |
UPDATE price_changes | |
SET old_value = #{prev_value}, old_price_changed_at = "#{prev_price_changed_at}" | |
WHERE id = #{current_record[0]} | |
SQL | |
end | |
def perform | |
log_start_proccess | |
prev_record = nil | |
ActiveRecord::Base.connection.execute(query).each_with_index do |record, idx| | |
log_msg(idx) if (idx % 10000).zero? | |
if prev_record && prev_record[1] == record[1] | |
ActiveRecord::Base.connection.execute(update_query(prev_record, record)) | |
end | |
prev_record = record | |
end | |
ensure | |
log_end_process | |
end | |
end | |
LoadOldValues.new("enex").perform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment