Skip to content

Let third-party adapters implement their own database rake tasks #5520

wants to merge 3 commits into from

2 participants

dazuma commented Mar 20, 2012

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.

Ruby on Rails member
arunagw commented May 14, 2012

Please try to rebase your PR first. Then we can see if this looks good.

dazuma commented May 21, 2012

Thanks Arun. Sorry, I got really busy, but I will get to rebasing it this week.

dazuma commented Jun 18, 2012

Okay, I took a look at recent changes to master, and it looks like someone has put in an alternative solution. I believe it fulfills my use case (that of writing database tasks for new ActiveRecord adapters). Accordingly, I'm closing this pull request. I'll re-open a different pull request if it turns out I need something else.

@dazuma dazuma closed this Jun 18, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.