-- 02: FK ocr_customer_match_rule.maincompany_id → maincompany.id
SET NAMES utf8mb4;
SET @db := DATABASE();

SET @fk := (
  SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
  WHERE CONSTRAINT_SCHEMA = @db AND TABLE_NAME = 'ocr_customer_match_rule'
    AND CONSTRAINT_NAME = 'FK_3D05263918C8CC62' AND CONSTRAINT_TYPE = 'FOREIGN KEY'
);
SET @sql := IF(@fk = 0,
  'ALTER TABLE `ocr_customer_match_rule` ADD CONSTRAINT `FK_3D05263918C8CC62` FOREIGN KEY (`maincompany_id`) REFERENCES `maincompany` (`id`)',
  'SELECT ''skip: FK_3D05263918C8CC62 already exists'' AS note');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
