Created
October 2, 2019 17:40
-
-
Save felipefunes/70b76d667db71f88f3f6573a5bb0ef02 to your computer and use it in GitHub Desktop.
TSVECTOR migration for Rails projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddTsvectorColumnToYourModel < ActiveRecord::Migration[5.2] | |
def up | |
# 1. Add column to your model | |
add_column :your_model, :tsv_column_name, :tsvector | |
# 2. Create index | |
add_index(:webpros, :tsv_webpros, using: 'gin') | |
say_with_time("Adding trigger function on webpros for updating tsv_webpros column") do | |
# 3. Add trigger | |
execute <<-SQL | |
DROP TRIGGER IF EXISTS your_model_tsv_update ON your_model; | |
CREATE TRIGGER your_model_tsv_update BEFORE INSERT OR UPDATE | |
ON your_model FOR EACH ROW EXECUTE PROCEDURE | |
tsvector_update_trigger(tsv_column_name, 'pg_catalog.simple', attributes, you, want, to, index) | |
SQL | |
end | |
end | |
def down | |
remove_column :your_model, :tsv_column_name | |
execute <<-SQL | |
DROP TRIGGER IF EXISTS your_model_tsv_update on your_model; | |
SQL | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment