Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord::ConnectionNotEstablished within a rake task

I'm working to create a rake task to remove a few tables and triggers.

My rake task:

task :remove_rubyrep do
  sql = <<-SQL
    DROP TABLE rr_logged_events, rr_running_flags, rr_pending_changes;
  SQL
  ActiveRecord::Base.establish_connection
  ActiveRecord::Base.connection.execute(sql)
end

I tried running this like so:

rake remove_rubyrep
RAILS_ENV=development rake remove_rubyrep

Problem is the rake tasks errors with:

rake aborted!
ActiveRecord::ConnectionNotEstablished

Any suggestions on how to allow the rake task to connect to the DB to execute the raw sql? Thanks

like image 418
AnApprentice Avatar asked Jan 04 '13 19:01

AnApprentice


1 Answers

You're not loading the rails application in your rake task, so ActiveRecord never creates a database connection.

Change your rake task to:

task :remove_rubyrep => :environment do

After doing that, you'll no longer need the "establish_connection" line

like image 155
wless1 Avatar answered Sep 19 '22 02:09

wless1