Is Rails clever or smart?

When generating a Rails project you can specify the database adapter. If you select mysql, then Rails 3.0.0 translates mysql to mysql2, which is (or should be) the current adapter for MySQL in Ruby.

I smell a hack here, like a solution belonging elsewhere.

Problem: A new MySQL adapter is available, so how do we make Rails compatible with it?

Clever solution: We translate mysql to mysql2, so that any time a user requests the old mysql adapter, the new mysql2 adapter will be used instead.

Smart solution: We add mysql2 alongside mysql, so that Rails users will decide which adapter they want.

I think that Rails implemented the clever solution because there are Rails developers that write applications in IDEs, which are released infrequently. Those developers would have to wait a lot to get the new mysql2 adapter accessible from their IDEs. So, a transparent translation is very effective in this case, because an IDE dialog for generating a project will execute the “rails new” command under the hood, requesting the mysql adapter. The problem is that there’s no way to use the old mysql adapter, if a Rails developer really wants to, except for hacking Rails itself.

Anyway, I think that Rails could have been smarter. When the user issues the “rails new” command, these are the possible combinations:

  1. mysql2 is installed but mysql is not
    If the user specifies “–database=mysql”, Rails should inform: “using available mysql2 adapter instead of specified mysql”, thus translating mysql to mysql2. This takes care of the fact that mysql2 is meant to replace mysql.
    If the user specifies “–database=mysql2”, Rails should use the specified adapter.
  2. mysql is installed but mysql2 is not
    If the user specifies “–database=mysql”, Rails should use the specified adapter.
    If the user specifies “–database=mysql2”, this should be considered an error and Rails should stop and inform the user. This takes care of the fact that mysql2 has been released after mysql.
  3. neither mysql2 nor mysql are installed
    If the user specifies “–database=mysql” or “–database=mysql2”, this is an error and Rails should stop and inform the user.
  4. both mysql2 and mysql are installed
    If the user specifies “–database=mysql” or “–database=mysql2”, Rails should use the specified adapter.

Browsing Rails code, I see that they were implementing mysql2 alongside mysql, but eventually translated the latter to the former.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.