5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-16 22:36:21 +00:00
panel/db/migrate/20210507221120_add_dictionary_to_indexed_posts.rb
f 6396841b2c evitar confusiones entre diccionario y locale
el diccionario es lo que usa internamente pg para indexar, el locale es
el idioma que asignamos en sutty.
2021-05-07 19:25:09 -03:00

29 lines
824 B
Ruby

# frozen_string_literal: true
# Para no estar calculando todo el tiempo el diccionario del idioma,
# agregamos una columna más.
class AddDictionaryToIndexedPosts < ActiveRecord::Migration[6.1]
LOCALES = {
'english' => 'en',
'spanish' => 'es'
}
def up
add_column :indexed_posts, :dictionary, :string
create_trigger(compatibility: 1).on(:indexed_posts).before(:insert, :update) do
"new.indexed_content := to_tsvector(('pg_catalog.' || new.dictionary)::regconfig, coalesce(new.title, '') || '\n' || coalesce(new.content,''));"
end
IndexedPost.find_each do |post|
locale = post.locale
post.update dictionary: locale, locale: LOCALES[locale]
end
end
def down
remove_column :indexed_posts, :locale
rename_column :indexed_posts, :dictionary, :locale
end
end