Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zetxek/11386701 to your computer and use it in GitHub Desktop.
Save zetxek/11386701 to your computer and use it in GitHub Desktop.
Rails migration to create a Postgresql function that stripts every accent and outputs the string as lowercase, as seen in http://stackoverflow.com/questions/9243322/postgres-accent-insensitive-like-search-in-rails-3-1-on-heroku
# -*- encoding : utf-8 -*-
class AddLowerUnaccentFunctionToDb < ActiveRecord::Migration
def self.up
execute "CREATE OR REPLACE FUNCTION lower_unaccent(character varying)
RETURNS text
AS $$
SELECT lower(translate($1
, '¹²³áàâãäåāăąÀÁÂÃÄÅĀĂĄÆćčç©ĆČÇĐÐèéêёëēĕėęěÈÊËЁĒĔĖĘĚ€ğĞıìíîïìĩīĭÌÍÎÏЇÌĨĪĬłŁńňñŃŇÑòóôõöōŏőøÒÓÔÕÖŌŎŐØŒř®ŘšşșߊŞȘùúûüũūŭůÙÚÛÜŨŪŬŮýÿÝŸžżźŽŻŹ'
, '123aaaaaaaaaaaaaaaaaaacccccccddeeeeeeeeeeeeeeeeeeeeggiiiiiiiiiiiiiiiiiillnnnnnnooooooooooooooooooorrrsssssssuuuuuuuuuuuuuuuuyyyyzzzzzz'
));
$$ IMMUTABLE STRICT LANGUAGE SQL;"
end
def self.down
execute "drop function lower_unaccent(text) cascade"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment