class AddTsvectorColumns < ActiveRecord::Migration
  def up
      add_column :image_files, :tsv_tsearch, :tsvector
      add_column :image_files, :tags_cache, :text
      add_index :image_files, :tsv_tsearch, using: "gin"

      execute <<-SQL
        CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
        ON image_files FOR EACH ROW EXECUTE PROCEDURE
        tsvector_update_trigger(
          tsv_tsearch, 'pg_catalog.french', name, description, photograph, origin_name, tags_cache
        );
      SQL

      now = Time.current.to_s(:db)
      update("UPDATE image_files SET updated_at = '#{now}'")
    end

    def down
      execute <<-SQL
        DROP TRIGGER tsvectorupdate
        ON image_files
      SQL

      remove_index :image_files, :tsv_tsearch
      remove_column :image_files, :tsv_tsearch
      remove_column :image_files, :tags_cache, :text
    end
end
