diff --git a/CHANGELOG.md b/CHANGELOG.md index 20943cbda2..6ad12a8717 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ and this project adheres to ### Changed +- Don't add cast fragments if the search_term is nil + [#968](https://github.com/OpenFn/Lightning/issues/968) + ### Fixed ## [v0.7.0-pre2] - 2023-07-26 diff --git a/lib/lightning/invocation.ex b/lib/lightning/invocation.ex index d19ee18113..a79307eb47 100644 --- a/lib/lightning/invocation.ex +++ b/lib/lightning/invocation.ex @@ -381,35 +381,37 @@ defmodule Lightning.Invocation do end) end - def filter_run_body_and_logs_where(_search_term, search_fields) - when search_fields == [] do - dynamic(true) - end - - def filter_run_body_and_logs_where(search_term, search_fields) - when search_fields != [] do - Enum.reduce(search_fields, dynamic(false), fn - :log, query -> - dynamic( - [log_lines: l], - ^query or - fragment( - "cast(? as VARCHAR) ilike ?", - l.body, - ^"%#{search_term}%" - ) - ) + def filter_run_body_and_logs_where(search_term, search_fields) do + if is_nil(search_term) do + dynamic(true) + else + Enum.reduce(search_fields || [], dynamic(false), fn + :log, query -> + dynamic( + [log_lines: l], + ^query or + fragment( + "cast(? as VARCHAR) ilike ?", + l.body, + ^"%#{search_term}%" + ) + ) - :body, query -> - dynamic( - [input: i], - ^query or - fragment("cast(? as VARCHAR) ilike ?", i.body, ^"%#{search_term}%") - ) + :body, query -> + dynamic( + [input: i], + ^query or + fragment( + "cast(? as VARCHAR) ilike ?", + i.body, + ^"%#{search_term}%" + ) + ) - _, query -> - query - end) + _, query -> + query + end) + end end def list_work_orders_for_project_query(