From 9cdf123063e0ce1b0856fc0b6dd7325441f59af5 Mon Sep 17 00:00:00 2001 From: oldmoe Date: Thu, 30 May 2024 01:53:12 +0200 Subject: [PATCH] ignore fts5 tables when dumping schema in AR fixes #115 --- lib/litestack/litesearch/model.rb | 2 ++ .../litesearch/schema_adapters/backed_adapter.rb | 10 +++++----- test/test_ar_search.rb | 6 ++++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/litestack/litesearch/model.rb b/lib/litestack/litesearch/model.rb index 69f00d8..2d433f2 100644 --- a/lib/litestack/litesearch/model.rb +++ b/lib/litestack/litesearch/model.rb @@ -12,6 +12,8 @@ def self.included(klass) klass.extend Litesearch::Model::ActiveRecordClassMethods ActiveRecord::Base.extend Litesearch::Model::BaseClassMethods Litesearch::Schema.prepend Litesearch::Model::ActiveRecordSchemaMethods + # ignore FTS5 virtual and shadow tables when dumping the schema + ActiveRecord::SchemaDumper.ignore_tables << /^#{klass.table_name}_search_idx.*$/ end end diff --git a/lib/litestack/litesearch/schema_adapters/backed_adapter.rb b/lib/litestack/litesearch/schema_adapters/backed_adapter.rb index 8562d0f..ed41a5d 100644 --- a/lib/litestack/litesearch/schema_adapters/backed_adapter.rb +++ b/lib/litestack/litesearch/schema_adapters/backed_adapter.rb @@ -30,7 +30,7 @@ def create_primary_triggers_sql(active = false) when_stmt = "NEW.#{filter} = TRUE" cols << filter end - update_filter = "" + update_filter = String.new if cols.length > 0 " OF #{cols.join(', ')} " end @@ -82,7 +82,7 @@ def create_secondary_trigger_poly_sql(target_table, target_col, col, conditions) def drop_secondary_triggers_sql - sql = "" + sql = String.new @schema[:fields].each do |name, field| if field[:trigger_sql] if field[:col] @@ -96,7 +96,7 @@ def drop_secondary_triggers_sql end def create_secondary_triggers_sql - sql = "" + sql = String.new @schema[:fields].each do |name, field| if field[:trigger_sql] if field[:col] @@ -145,7 +145,7 @@ def enrich_schema end def filter_sql - sql = "" + sql = String.new sql << " WHERE #{@schema[:filter_column]} = TRUE " if @schema[:filter_column] sql end @@ -166,7 +166,7 @@ def joins_sql joins = [@schema[:table]] active_fields.each do |name, field| if field[:trigger_sql] - join_table = "" + join_table = String.new join_table << "#{field[:target_table]} AS #{field[:target_table_alias]} ON " if field[:col] join_table << "#{field[:target_table_alias]}.id = #{@schema[:table]}.#{field[:col]}" if field[:col] diff --git a/test/test_ar_search.rb b/test/test_ar_search.rb index 9ef0168..b14c1bc 100644 --- a/test/test_ar_search.rb +++ b/test/test_ar_search.rb @@ -255,4 +255,10 @@ def test_uncreated_table assert_equal 2, rs.length end + def test_ignore_tables + assert_equal false, ActiveRecord::SchemaDumper.ignore_tables.empty? + # we have created 6 models, one ignore regex for each + assert_equal 6, ActiveRecord::SchemaDumper.ignore_tables.count + end + end