title: Is it possible to add a function-based database-agnostic index via a Rails/ActiveRecord migration?
body: <p>I have an ActiveRecord model like this:</p>
  
  <pre><code>create_table "books" do |t|
    t.string "title"
  end
  
  class Book &lt; ActiveRecord::Base
    default_scope :order =&gt; 'lower(title) DESC'
  end
  </code></pre>
  
  <p>As you can see I want to sort by the lowercase form of the <code>title</code> attribute, but this imposes a performance hit at the database level. This hit can be remedied in different ways with different databases. For example, in PostgreSQL or Oracle you create a function-based index:</p>
  
  <pre><code>CREATE INDEX lowercase_book_title_index ON book (lower(title));
  </code></pre>
  
  <p>SQLite3 doesn't have function-based indexes so you have to specify a collation:</p>
  
  <pre><code>CREATE INDEX lowercase_book_title_index ON book (title COLLATE NOCASE);
  </code></pre>
  
  <p>I haven't looked into how you do it with MySQL but I'm sure there's a way (collations? virtual columns?).</p>
  
  <p>In any event I would like to do this in proper Rails fashion with a database-agnostic migration. I can of course create a simple index like this:</p>
  
  <pre><code>add_index :books, :title
  </code></pre>
  
  <p>But the index this generates is case-sensitive. I realize I could write a database-dependent migration, but it's not very elegant. It's also not practical--I often find myself using SQLite3 on my development workstations and PostgreSQL in production. The available options for add_index deal with index name, uniqueness, and length. Am I overlooking a way to accomplish what I'm trying to do here?</p>