Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save malachaifrazier/82e635fb52c5a1216d0cde689359a422 to your computer and use it in GitHub Desktop.
Save malachaifrazier/82e635fb52c5a1216d0cde689359a422 to your computer and use it in GitHub Desktop.
AddIndexForSearchableFieldsInDevelopers
class AddIndexForSearchableFieldsInDevelopers < ActiveRecord::Migration[7.0]
def up
execute <<-SQL
ALTER TABLE developers
ADD COLUMN textsearchable_index_col tsvector
GENERATED ALWAYS AS (to_tsvector('simple', coalesce(hero, '') || ' ' || coalesce(bio, ''))) STORED;
SQL
add_index :developers, :textsearchable_index_col, using: :gin, name: :textsearchable_index
end
def down
remove_index :developers, name: :textsearchable_index
remove_column :developers, :textsearchable_index_col
end
end
@malachaifrazier
Copy link
Author

Running bundle exec rails db:migrate --trace fails with this:

`
** Invoke db:migrate (first_time)
** Invoke db:load_config (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:load_config
** Execute db:migrate
== 20220416144523 AddIndexForSearchableFieldsInDevelopers: migrating ==========
-- execute(" ALTER TABLE developers\n ADD COLUMN textsearchable_index_col tsvector\n GENERATED ALWAYS AS (to_tsvector('simple', coalesce(hero, '') || ' ' || coalesce(bio, ''))) STORED;\n")
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::SyntaxError: ERROR: syntax error at or near "("
LINE 3: GENERATED ALWAYS AS (to_tsvector('simple', coalesc...
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment