-- Migration: Add sale_id to stock_histories
-- Date: 2026-03-26
-- Purpose: Link stock history entries to the sale that caused the movement,
--          enabling full audit trail for sales and sale returns in stock history.

ALTER TABLE stock_histories
  ADD COLUMN sale_id BIGINT NULL AFTER purchase_id,
  ADD CONSTRAINT fk_stock_histories_sale_id
    FOREIGN KEY (sale_id) REFERENCES product_orders(id) ON DELETE SET NULL;
