about summary refs log tree commit diff
path: root/db/migrate/20200109191740_migrate_back_to_fts.rb
diff options
context:
space:
mode:
authormultiple creatures <dev@multiple-creature.party>2020-01-10 03:45:29 -0600
committermultiple creatures <dev@multiple-creature.party>2020-01-10 03:45:29 -0600
commitf03960382bd05b8570e0e3b1066545831c59138a (patch)
treee7f4b362018e17e2a475a9c2e6988b0ebd79ac5d /db/migrate/20200109191740_migrate_back_to_fts.rb
parent4c8591fbeae9054a354f955e37a95aeed369222a (diff)
switch (back) to postgres fts engine for fast search & timeline filters
Diffstat (limited to 'db/migrate/20200109191740_migrate_back_to_fts.rb')
-rw-r--r--db/migrate/20200109191740_migrate_back_to_fts.rb42
1 files changed, 42 insertions, 0 deletions
diff --git a/db/migrate/20200109191740_migrate_back_to_fts.rb b/db/migrate/20200109191740_migrate_back_to_fts.rb
new file mode 100644
index 000000000..dd98f7649
--- /dev/null
+++ b/db/migrate/20200109191740_migrate_back_to_fts.rb
@@ -0,0 +1,42 @@
+class MigrateBackToFts < ActiveRecord::Migration[5.2]
+  def up
+    if table_exists? :normalized_statuses
+      remove_index :normalized_statuses, name: 'index_statuses_on_normalized_text_trgm'
+      drop_table :normalized_statuses
+    end
+
+    safety_assured do
+      execute <<-SQL.squish
+        DROP FUNCTION IF EXISTS public.f_normalize;
+        DROP FUNCTION IF EXISTS public.f_unaccent;
+
+        CREATE OR REPLACE FUNCTION public.f_strip_mentions(text)
+          RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE STRICT AS
+          $func$
+            SELECT regexp_replace(
+              regexp_replace($1, '</?span>', '', 'g'),
+              '>@[^[:space:]]+<', '><', 'g'
+            )
+          $func$;
+
+        CREATE OR REPLACE AGGREGATE tsquery_union(tsquery) (
+          SFUNC = tsquery_or,
+          STYPE = tsquery,
+          PARALLEL = SAFE
+        );
+
+        CREATE TEXT SEARCH CONFIGURATION fedi ( COPY = simple );
+
+        ALTER TEXT SEARCH CONFIGURATION fedi
+          ALTER MAPPING FOR hword, hword_part, word
+            WITH unaccent, simple;
+
+        ALTER TABLE statuses
+          ADD COLUMN tsv tsvector
+          GENERATED ALWAYS AS (
+            to_tsvector('fedi', f_strip_mentions(spoiler_text || ' ' || text))
+          ) STORED;
+      SQL
+    end
+  end
+end