Fix CreateStockEvents migration for DBs without Delivery and/or StockTaking

This commit is contained in:
Patrick Gansterer 2020-09-07 13:55:48 +02:00
parent c6250de945
commit 00bb10d14a

View file

@ -11,7 +11,9 @@ class CreateStockEvents < ActiveRecord::Migration[4.2]
dir.up do dir.up do
change_column_default :stock_events, :type, nil change_column_default :stock_events, :type, nil
diff = [StockEvent.maximum(:id) + 1 - StockTaking.minimum(:id), 0].max stock_event_max = StockEvent.maximum(:id) || 0
stock_taking_min = StockTaking.minimum(:id) || 0
diff = [stock_event_max + 1 - stock_taking_min, 0].max
execute "UPDATE stock_changes SET stock_event_id = stock_taking_id + #{diff} execute "UPDATE stock_changes SET stock_event_id = stock_taking_id + #{diff}
WHERE stock_taking_id IS NOT NULL" WHERE stock_taking_id IS NOT NULL"