-- Migration: Add unique constraints for Innomaint API sync
-- This enables ON CONFLICT clauses in upsert operations for:
-- - ic3_asset_performance (performance metrics per asset)
-- - ic3_workorder_summary (work order counts per asset and period)

-- ic3_asset_performance needs unique on asset_id for ON CONFLICT
ALTER TABLE ic3_asset_performance
    ADD CONSTRAINT uq_performance_asset_id UNIQUE (asset_id);

-- ic3_workorder_summary needs unique on (asset_id, period_type)
ALTER TABLE ic3_workorder_summary
    ADD CONSTRAINT uq_workorder_asset_period UNIQUE (asset_id, period_type);

-- Verify constraints were added
-- SELECT constraint_name, table_name FROM information_schema.table_constraints
-- WHERE constraint_name LIKE 'uq_%' AND table_name IN ('ic3_asset_performance', 'ic3_workorder_summary');
