Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is the example contents of the Rakefile, which you would use to run active record tasks without using Rails. It assumes using the same directories as rails uses: `db/migrate`, `config/database.yml`.
require 'bundler/setup'
require 'active_record'
include ActiveRecord::Tasks
db_dir = File.expand_path('../db', __FILE__)
config_dir = File.expand_path('../config', __FILE__)
DatabaseTasks.env = ENV['ENV'] || 'development'
DatabaseTasks.db_dir = db_dir
DatabaseTasks.database_configuration = YAML.load(File.read(File.join(config_dir, 'database.yml')))
DatabaseTasks.migrations_paths = File.join(db_dir, 'migrate')
task :environment do
ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
ActiveRecord::Base.establish_connection DatabaseTasks.env
end
load 'active_record/railties/databases.rake'
@damncabbage

This comment has been minimized.

damncabbage commented Aug 29, 2013

Suggested addition just below require 'active_record' above:

module Rails
  def self.root
    File.dirname(__FILE__)
  end
end

(Fixes the uninitialized constant ActiveRecord::Tasks::SQLiteDatabaseTasks::Rails error when using the above with SQLite.)

@zekefast

This comment has been minimized.

zekefast commented Jan 30, 2014

For activerecord v4.0.2 I've got an error with rake db:drop, but setting ActiveRecord::Tasks::DatabaseTasks.root attribute helped to resolve that issue.

The error:

$ rake db:drop
can't convert nil into String
<backtrace goes below>
@Timo614

This comment has been minimized.

Timo614 commented Feb 1, 2014

Thanks for the gist! Was a huge help -- there's no way I would have been able to figure out how to include those tasks without it.

Just a tiny note to anyone using this, remember to change the environment variable mentioned above to match the environment variable you use to set your env.

DatabaseTasks.env = ENV['ENV'] || 'development'

I wasn't thinking and just copied that outright and wasted some time debugging a stupid issue as a result.

Had a goliath, test unit, active support test cases setup and was trying to get parallel_tests to work with it. It always passes RAILS_ENV though so some of the commands were acting up as a result because I had this set to development.

@jwliechty

This comment has been minimized.

jwliechty commented Sep 10, 2014

Thank you!! This was so buried. Only when I googled 'rake file using DatabaseTasks' did this come up as the fourth hit. Here are some suggested key words. I'll add them here so others can find this.

  • active record rake file without rails
  • active record rake tasks without rails
  • how to load databases.rake

The other hits were about referencing the same active record configuration for using DatabaseTasks. Seriously, I wanted the authentic rake tasks instead of implementing them myself. So again thank you.

@danelowe

This comment has been minimized.

danelowe commented Dec 24, 2014

Simpler solution to uninitialized constant ActiveRecord::Tasks::SQLiteDatabaseTasks::Rails:

DatabaseTasks.root = File.dirname(__FILE__)
@ajorgensen

This comment has been minimized.

ajorgensen commented Feb 22, 2015

I just tried this gist with activerecord (4.2.0) and got the following error: NoMethodError: undefined methodapplication' for Rails:Module`

The fix was something like this:

module Rails
  def self.root
    File.dirname(__FILE__)
  end

  def self.env
    ENV['APP_ENV']
  end

  def self.application
    Paths.new
  end
end

class Paths
  def paths
    { "db/migrate" => [File.expand_path("../db/migrate", __FILE__)] }
  end

  def load_seed
    load File.expand_path("../db/seeds.rb", __FILE__)
  end
end

EDIT: This also includes a fix for the rake db:seed task.

@vanburg

This comment has been minimized.

vanburg commented May 27, 2015

If someone need only part of AR tasks (for example while testing gem you don't need half of them, because you test against test db and do not need seed or bunch of others) and for more details on how @drogus gist works, check my variation (based on this, thanks, btw :) https://gist.github.com/vanburg/56c5691c799c9e62e81a

@jehughes

This comment has been minimized.

jehughes commented Jul 29, 2016

Super helpful gist!

I'm using Ruby 2.3.1 and found I needed to change the env variable to a symbol when making the connection:

ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym

Otherwise I was getting a missing adapter error.

@foton

This comment has been minimized.

foton commented Mar 21, 2017

I would like to point to Jason modification of this gist, which works for ActiveRecord 4.2.0

@Paxa

This comment has been minimized.

Paxa commented Jul 3, 2017

Solved as:

# Rakefile
if Rake.application.top_level_tasks != ['test']
  require_relative 'config/application'
  Rails.application.load_tasks
  Rake::Task['test'].clear
end

task :test do
  ENV['RAILS_ENV'] = 'test'
  Dir.glob('./test/**/*_test.rb').each { |file| require file}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment