New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ActiveRecord Rake tasks out of Rails #11609

Closed
xcambar opened this Issue Jul 26, 2013 · 7 comments

Comments

Projects
None yet
7 participants
@xcambar

xcambar commented Jul 26, 2013

Hi,
I'm building a Grape+ActiveRecord API, and I've been running into a strange issue while trying to use ActiveRecord Rake tasks into this project.

Here's a snippet of the Rakefile:

require 'rake'
require 'active_record'

include ActiveRecord::Tasks
DatabaseTasks.database_configuration = YAML::load(File.open('./config/database.yml'))
DatabaseTasks.db_dir = 'db'
DatabaseTasks.create_current(ENV['RACK_ENV'])

And I get the following error when running rake -T:

database configuration does not specify adapter
/Users/xav/code/ruby/incase/server2/Rakefile:7:in `<top (required)>'

I (think I) have read carefully the doc in https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/connection_specification.rb though.

After some quick investigation, it appears that the line 52 of connection_specification.rb (https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/connection_specification.rb#L52) checks for a symbol in the Hash:

raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)
  1. The keys in the parsed YAML file are all strings. So the documentation does not make a valid example (unless I'm wrong).
  2. Even when I convert the hash so the keys are symbols, I still have the same error.

What do you think? Is that a PEBKAC or a bug?

@dmathieu

This comment has been minimized.

Contributor

dmathieu commented Jul 26, 2013

create_current calls each_current_configuration, which relies on ActiveRecord::Base.configurations, not ActiveRecord::Tasks::DatabaseTasks.database_configuration.

You're apparently just not defining the configuration at the right place.

@drogus: you implemented this in d1d7c86, but apparently missed some configuration uses. Was that on purpose?

@miry

This comment has been minimized.

miry commented Jul 26, 2013

@xcambar ActiveRecord::Base.configurations = DatabaseTasks.database_configuration

@drogus

This comment has been minimized.

Member

drogus commented Jul 26, 2013

Damien is right about providing the configuration. I intentionally left out
the configuration part, because it's up to the application to provide the
config and it's up to the application when to do it. Maybe it could be
improved to handle some kind of "default" use case, but I'm hesitant to
make it any more complicated for now.

Here is the gist with an example Rakefile, which you could use to run AR
rake tasks: https://gist.github.com/drogus/6087979, so basically the part
which you're missing is :environment task, which sets the configuration
and initializes the connection.

I started adding info about it in the AR guide, I will push it later today.

On Fri, Jul 26, 2013 at 11:54 AM, Michael Nikitochkin <
notifications@github.com> wrote:

@xcambar https://github.com/xcambar pls provide the content of
./config/database.yml


Reply to this email directly or view it on GitHubhttps://github.com//issues/11609#issuecomment-21611619
.

Piotr Sarnacki
http://piotrsarnacki.com

@dmathieu

This comment has been minimized.

Contributor

dmathieu commented Jul 26, 2013

Since this is not a bug, I believe the issue can be closed.

@robin850

This comment has been minimized.

Member

robin850 commented Jul 26, 2013

Agree with @dmathieu since @drogus is working on the documentation.

@xcambar

This comment has been minimized.

xcambar commented Jul 26, 2013

Works perfectly!

Thanks for helping

@kpassapk

This comment has been minimized.

kpassapk commented Oct 6, 2013

Thanks @drogus, I was having similar issues :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment