Skip to content

Instantly share code, notes, and snippets.

@airspeed
Last active December 20, 2015 04:38
Show Gist options
  • Save airspeed/6071796 to your computer and use it in GitHub Desktop.
Save airspeed/6071796 to your computer and use it in GitHub Desktop.
Migration
def migrate
timestamp = Time.now.to_i.to_s
echt_ink = Invoice.where(:invoice_number => ['B12-001765','B12-001849','B12-001850','B12-001851','B12-002119','B12-002120','B12-002121','B12-002684','B12-003439','B12-003449','B12-003450','B12-003482','B12-003520','B12-003574','B12-003708','B12-004053','B12-004055','B13-001241','B13-001329','B13-001330','B13-001343','B13-001345','B13-001385','B12-004155'])
puts echt_ink.count #24
all_ink = Invoice.where(:state => 'ink')
puts all_ink.count #70
falsch_ink = all_ink - echt_ink
puts falsch_ink.count #46
# Zusätzliche Tests
puts falsch_ink.map(&:state).uniq #["ink"]
puts falsch_ink.select{|r| r.invoice_number == echt_ink.first.invoice_number} unless falsch_ink.length.zero? || echt_ink.length.zero? #[]
falsch_ink.each do |r|
puts r.invoice_number
ActiveRecord::Base.transaction do
r.tap{|r1| r1.state = 'm2'; r1.updated_at = Date.today}.save
InvoiceStateTransition.create!(:invoice_id => r.id, :event => "second_rem", :from => 'm1', :to => 'm2', :context => timestamp)
InvoiceStateTransition.where(:invoice_id => r.id, :event => 'inkasso').map(&:destroy) # destroy! erst auf Rails4
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment