diff options
| -rw-r--r-- | app/models/page.rb | 26 | ||||
| -rw-r--r-- | config/initializers/ensure_search_vector_trigger.rb | 7 |
2 files changed, 33 insertions, 0 deletions
diff --git a/app/models/page.rb b/app/models/page.rb index 740d42e..db5b688 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -254,6 +254,32 @@ class Page < ApplicationRecord | |||
| 254 | 254 | ||
| 255 | end | 255 | end |
| 256 | 256 | ||
| 257 | # Installs (or re-installs) the trigger that keeps page_translations' | ||
| 258 | # search_vector in sync. Idempotent, safe to call on every boot. | ||
| 259 | # search_vector is populated by a raw Postgres trigger, not anything | ||
| 260 | # Rails' schema dumper can represent -- a database rebuilt from | ||
| 261 | # schema.rb rather than replayed migrations silently loses it. | ||
| 262 | def self.ensure_search_vector_trigger! | ||
| 263 | connection.execute(<<~SQL) | ||
| 264 | CREATE OR REPLACE FUNCTION page_translations_search_vector_update() | ||
| 265 | RETURNS trigger AS $$ | ||
| 266 | BEGIN | ||
| 267 | NEW.search_vector := to_tsvector( | ||
| 268 | 'simple', | ||
| 269 | coalesce(NEW.title, '') || ' ' || | ||
| 270 | coalesce(NEW.abstract, '') || ' ' || | ||
| 271 | coalesce(NEW.body, '') | ||
| 272 | ); | ||
| 273 | RETURN NEW; | ||
| 274 | END; | ||
| 275 | $$ LANGUAGE plpgsql; | ||
| 276 | |||
| 277 | CREATE OR REPLACE TRIGGER page_translations_search_vector_trigger | ||
| 278 | BEFORE INSERT OR UPDATE ON page_translations | ||
| 279 | FOR EACH ROW EXECUTE PROCEDURE page_translations_search_vector_update(); | ||
| 280 | SQL | ||
| 281 | end | ||
| 282 | |||
| 257 | private | 283 | private |
| 258 | 284 | ||
| 259 | def set_page_title | 285 | def set_page_title |
diff --git a/config/initializers/ensure_search_vector_trigger.rb b/config/initializers/ensure_search_vector_trigger.rb new file mode 100644 index 0000000..a750e9c --- /dev/null +++ b/config/initializers/ensure_search_vector_trigger.rb | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | Rails.application.config.after_initialize do | ||
| 2 | begin | ||
| 3 | Page.ensure_search_vector_trigger! if Page.connection.table_exists?(:page_translations) | ||
| 4 | rescue ActiveRecord::NoDatabaseError, ActiveRecord::ConnectionNotEstablished, PG::ConnectionBad | ||
| 5 | # Database doesn't exist yet -- e.g. mid `rails db:create`. Nothing to install yet. | ||
| 6 | end | ||
| 7 | end | ||
