-- Migration to allow multiple bids from the same supplier for the same inquiry
-- This removes the unique constraint on (inquiryId, supplierId)

-- First, find and drop the unique index if it exists
-- The index name might vary, so we'll try common names
DROP INDEX IF EXISTS `bids_inquiryId_supplierId_unique` ON `bids`;
DROP INDEX IF EXISTS `bids_inquiryId_supplierId` ON `bids`;

-- Also try to drop by constraint name (MySQL might use this)
-- Note: This might fail if the constraint doesn't exist, which is fine
SET @constraint_name = (
  SELECT CONSTRAINT_NAME 
  FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
  WHERE TABLE_SCHEMA = DATABASE() 
    AND TABLE_NAME = 'bids' 
    AND CONSTRAINT_TYPE = 'UNIQUE'
    AND CONSTRAINT_NAME LIKE '%inquiryId%supplierId%'
  LIMIT 1
);

SET @sql = IF(@constraint_name IS NOT NULL, 
  CONCAT('ALTER TABLE `bids` DROP INDEX `', @constraint_name, '`'),
  'SELECT "No unique constraint found" AS message'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Create a non-unique index for better query performance
CREATE INDEX IF NOT EXISTS `idx_bids_inquiry_supplier` ON `bids` (`inquiryId`, `supplierId`);

