Make StockEvent a base class for Delivery and StockTaking
This helps to share code between the two entities and allows easier extensions in the future.
This commit is contained in:
parent
a5582e9542
commit
785313ac23
9 changed files with 69 additions and 33 deletions
41
db/migrate/20181204070000_create_stock_events.rb
Normal file
41
db/migrate/20181204070000_create_stock_events.rb
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
class CreateStockEvents < ActiveRecord::Migration
|
||||
class StockEvent < ActiveRecord::Base; end
|
||||
class StockTaking < ActiveRecord::Base; end
|
||||
|
||||
def change
|
||||
rename_table :deliveries, :stock_events
|
||||
rename_column :stock_changes, :delivery_id, :stock_event_id
|
||||
add_column :stock_events, :type, :string, default: 'Delivery', null: false
|
||||
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
change_column_default :stock_events, :type, nil
|
||||
|
||||
diff = [StockEvent.maximum(:id) + 1 - StockTaking.minimum(:id), 0].max
|
||||
|
||||
execute "UPDATE stock_changes SET stock_event_id = stock_taking_id + #{diff}
|
||||
WHERE stock_taking_id IS NOT NULL"
|
||||
execute "INSERT INTO stock_events (type, id, date, note, created_at)
|
||||
SELECT 'StockTaking', id + #{diff}, date, note, created_at FROM stock_takings"
|
||||
|
||||
remove_column :stock_changes, :stock_taking_id
|
||||
drop_table :stock_takings
|
||||
end
|
||||
|
||||
dir.down do
|
||||
create_table :stock_takings do |t|
|
||||
t.date :date
|
||||
t.text :note
|
||||
t.datetime :created_at
|
||||
end
|
||||
add_column :stock_changes, :stock_taking_id, :integer
|
||||
|
||||
execute "INSERT INTO stock_takings (id, date, note, created_at)
|
||||
SELECT id, date, note, created_at FROM stock_events WHERE type = 'StockTaking'"
|
||||
execute "DELETE FROM stock_events WHERE type = 'StockTaking'"
|
||||
execute "UPDATE stock_changes SET stock_taking_id = stock_event_id, stock_event_id = NULL
|
||||
WHERE stock_event_id IN (SELECT id FROM stock_takings)"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue