Skip to content

Instantly share code, notes, and snippets.

@ldlsegovia
Last active June 30, 2017 13:16
Show Gist options
  • Save ldlsegovia/2b89336a3f9475492db75aad0ec40821 to your computer and use it in GitHub Desktop.
Save ldlsegovia/2b89336a3f9475492db75aad0ec40821 to your computer and use it in GitHub Desktop.
ENV['RAILS_ENV'] = ARGV[0] || 'production'
DIR = File.dirname(__FILE__)
require DIR + '/config/environment'
class DupPriceChanges
include PricingLogger
def initialize(realm)
raise 'invalid realm' unless ["gasco", "enex"].include?(realm)
@realm = realm
end
def attrs
{
"price_id" => :integer,
"price_changes.distributor_id" => :integer,
"price_changes.created_at" => :datetime,
"price_changes.updated_at" => :datetime,
"value" => :integer,
"price_changed_at" => :datetime,
"realm" => :realm,
"price_changes.brand_id" => :integer,
"price_changes.product_id" => :integer
}
end
def query
PriceChange.select(attrs.keys)
.joins(distributor: "#{@realm}_distributor")
.where(realm: nil)
.to_sql
end
def insert_query(record)
attrs.keys.each_with_index do |k, idx|
attr_type = attrs[k]
value = case attr_type
when :string
"\"#{record[idx]}\""
when :datetime
"\"#{record[idx].to_datetime.strftime('%Y-%m-%d %H:%M:%S')}\""
when :realm
"\"#{@realm}\""
else
record[idx]
end
record[idx] = value
end
"INSERT INTO price_changes (#{attrs.keys.join(', ')}) VALUES (#{record.join(', ')})"
end
def perform
log_start_proccess
ActiveRecord::Base.connection.execute(query).each_with_index do |record, idx|
log_msg(idx) if (idx % 10000).zero?
ActiveRecord::Base.connection.execute(insert_query(record))
end
ensure
log_end_process
end
end
DupPriceChanges.new("enex").perform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment