-
-
Save drogus/6087979 to your computer and use it in GitHub Desktop.
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' |
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
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.
I would like to point to Jason modification of this gist, which works for ActiveRecord 4.2.0
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
I have made minimal example/setup using Rails migration and active record outside Rails
https://github.com/euclid1990/rails-migration
(Support Rails >= 5.2)
🔢
I get
ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter
I just tried this gist with
activerecord (4.2.0)
and got the following error:NoMethodError: undefined method
application' for Rails:Module`The fix was something like this:
EDIT: This also includes a fix for the
rake db:seed
task.