SET @has_unique_sku := (
    SELECT COUNT(*)
    FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'productos'
      AND INDEX_NAME = 'uk_productos_empresa_sku'
);

SET @sql := IF(
    @has_unique_sku = 0,
    'ALTER TABLE productos ADD UNIQUE KEY uk_productos_empresa_sku (empresa_id, sku)',
    'SELECT 1'
);

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