Skip to content

Instantly share code, notes, and snippets.

@nimf
Forked from drogus/Rakefile.rb
Last active February 12, 2016 16:56
Show Gist options
  • Save nimf/28998ee9090bcaa7b520 to your computer and use it in GitHub Desktop.
Save nimf/28998ee9090bcaa7b520 to your computer and use it in GitHub Desktop.
I found this in the answer by Jason to this question http://stackoverflow.com/questions/19206764/how-can-i-load-activerecord-database-tasks-on-a-ruby-project-outside-rails. This is a Rakefile for using ActiveRecord without Rails. It loads ActiveRecord's databases.rake file making ActiveRecord rake tasks available for your non-rails app.
require 'bundler/setup'
require 'active_record'
include ActiveRecord::Tasks
class Seeder
def initialize(seed_file)
@seed_file = seed_file
end
def load_seed
raise "Seed file '#{@seed_file}' does not exist" unless File.file?(@seed_file)
load @seed_file
end
end
root = File.expand_path '..', __FILE__
DatabaseTasks.env = ENV['ENV'] || 'development'
DatabaseTasks.database_configuration = YAML.load(File.read(File.join(root, 'config/database.yml')))
DatabaseTasks.db_dir = File.join root, 'db'
DatabaseTasks.fixtures_path = File.join root, 'test/fixtures'
DatabaseTasks.migrations_paths = [File.join(DatabaseTasks.db_dir, 'migrate')]
DatabaseTasks.seed_loader = Seeder.new File.join(DatabaseTasks.db_dir, 'seeds.rb')
DatabaseTasks.root = root
task :environment do
ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym
end
load 'active_record/railties/databases.rake'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment