SEO has become a hot topic in the Internet. I am working in my personal project which I’d like to have a friendly URL. I need to create a slug as my friend URL. But, the slug is case sensitive in my case. for example, word ‘fisher’ is different to name ‘Fisher’ in a dictionary. unfortunately, MySQL is case sensitive by default. So I need to change the setting for the slug field in my table.
I change my slug field in MySQL by adding the following code into my migration script:
execute %{ALTER TABLE TABLE_NAME MODIFY slug varchar(255) COLLATE utf8_bin NOT NULL}
That works fine in my production database which is MySQL. However, I am using sqlite3 in test which is case sensitive by default. So I need to modify the field only in MySQL database. The finial code I put in my migration script is following:
if ActiveRecord::Base.configurations[RAILS_ENV]['adapter'] == 'mysql'
execute %{ALTER TABLE words MODIFY slug varchar(255) COLLATE utf8_bin NOT NULL}
end
COLLATE options in MySQL:
- utf8_bin: compare strings by the binary value of each character
- utf8_general_ci: compare strings using general language rules, case insensitive
- utf8_general_cs: compare strings using general language case sensitive