rails/rails — pull request #5520 by dazuma (2012-03-20 02:31:17+00:00)
Title: Let third-party adapters implement their own database rake tasks

The file activerecord/lib/active_record/railties/databases.rake implements the various database rake tasks (such as db:create, db:structure:dump, etc.). However, it currently recognizes particular ActiveRecord connection adapters by name and hard-codes an implementation for each adapter. This means, if you write (or update) a third-party adapter, you must edit this central file in order to get the database rake tasks to work for your adapter.

This patch includes a mechanism to let ActiveRecord adapters provide their own implementation for the standard rake tasks. An adapter may optionally provide a Tasks object that provides implementations of any number of operations. This object must be distinct from the connection adapter itself because some tasks (such as database creation) cannot be run within the context of any one connection. The object, and any and all operations it provides, are all completely optional, so existing adapters will be fully backwards compatible.

This patch also demonstrates how the mechanism will work, by porting the db:charset task. Previously, databases.rake implemented this task using a case statement on the adapter name. Now, the implementations for the various supported adapters (mysql[2], Postgresql, and sqlite3) have been moved into the adapters themselves via the task mechanism, thus removing the adapter-specific code from databases.rake.

I haven't yet ported the rest of databases.rake because I wanted to get a sign-off on this mechanism before doing that work. If this patch is accepted, I'll go ahead and port the rest of databases.rake. I believe we can drastically reduce the size of databases.rake, and eliminate all the adapter-specific code therein. More importantly, this will allow third-party adapters to provide their own implementations of these tasks without touching the central repository.