From 473c2bebefed628ede893a98cfcf5cac54f82a11 Mon Sep 17 00:00:00 2001 From: Mario Oprea Date: Wed, 2 Feb 2022 11:54:44 +0200 Subject: [PATCH] Fix invalid WHERE statement when conditions are empty Given an Active Record model with PK of type UUID: ``` +class CreateCardTransactions < ActiveRecord::Migration[6.0] + def change + create_table :card_transactions, id: :uuid do |t| + t.timestamps + end + end +end ``` And with a Forest collection with `search_fields` configured only for `id` field: ``` module Forest class CardTransaction include ForestLiana::Collection collection :Card__Transaction search_fields %w[id] end end ``` `SearchQueryBuilder` will generate an invalid SQL query with an empty `WHERE` statement if search term doesn't meet `REGEX_UUID` format: ```sql SELECT "transactions"."id" AS t0_r0, "transactions"."processed_at" AS FROM "transactions" WHERE ORDER BY "transactions"."created_at" DESC LIMIT $1 OFFSET $2 [["LIMIT", 10], ["OFFSET", 0]] ``` --- app/services/forest_liana/search_query_builder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/forest_liana/search_query_builder.rb b/app/services/forest_liana/search_query_builder.rb index f6f11cd0..386f9ee3 100644 --- a/app/services/forest_liana/search_query_builder.rb +++ b/app/services/forest_liana/search_query_builder.rb @@ -144,7 +144,7 @@ def search_param conditions.join(' OR '), search_value_for_string: "%#{@search.downcase}%", search_value_for_uuid: @search.to_s - ) + ) unless conditions.empty? end @records