about summary refs log tree commit diff
path: root/db/migrate
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
parent4c8591fbeae9054a354f955e37a95aeed369222a (diff)
switch (back) to postgres fts engine for fast search & timeline filters
Diffstat (limited to 'db/migrate')
-rw-r--r--db/migrate/20191211235208_create_normalized_statuses.rb23
-rw-r--r--db/migrate/20200109191740_migrate_back_to_fts.rb42
-rw-r--r--db/migrate/20200110072034_add_index_to_tsv.rb15
3 files changed, 57 insertions, 23 deletions
diff --git a/db/migrate/20191211235208_create_normalized_statuses.rb b/db/migrate/20191211235208_create_normalized_statuses.rb
deleted file mode 100644
index 19c7cd6c3..000000000
--- a/db/migrate/20191211235208_create_normalized_statuses.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-class CreateNormalizedStatuses < ActiveRecord::Migration[5.2]
-  def up
-    create_table :normalized_statuses do |t|
-      t.references :status, null: false, foreign_key: {on_delete: :cascade}, index: {unique: true}
-      t.text :text
-    end
-
-    safety_assured do
-      remove_index :statuses, name: 'index_statuses_on_normalized_text_trgm'
-      execute 'INSERT INTO normalized_statuses (status_id, text) SELECT id, normalized_text FROM statuses'
-      remove_column :statuses, :normalized_text
-    end
-  end
-
-  def down
-    safety_assured do
-      execute 'UPDATE statuses SET normalized_text = s.text FROM (SELECT status_id, text FROM normalized_statuses) AS s WHERE statuses.id = s.id'
-      remove_index :normalized_statuses, name: 'index_statuses_on_normalized_text_trgm'
-      drop_table :normalized_statuses
-      add_column :statuses, :normalized_text, :text, null: false, default: ''
-    end
-  end
-end
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
diff --git a/db/migrate/20200110072034_add_index_to_tsv.rb b/db/migrate/20200110072034_add_index_to_tsv.rb
new file mode 100644
index 000000000..d6b66c2d6
--- /dev/null
+++ b/db/migrate/20200110072034_add_index_to_tsv.rb
@@ -0,0 +1,15 @@
+class AddIndexToTsv < ActiveRecord::Migration[5.2]
+  disable_ddl_transaction!
+
+  def up
+    safety_assured do
+      execute 'CREATE INDEX CONCURRENTLY statuses_text_vector_idx ON statuses USING GIN(tsv)'
+    end
+  end
+
+  def down
+    safety_assured do
+      execute 'DROP INDEX statuses_text_vector_idx ON statuses'
+    end
+  end
+end