Skip to content

Instantly share code, notes, and snippets.

@qd3v
Last active July 10, 2018 18:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save qd3v/56c5691c799c9e62e81a to your computer and use it in GitHub Desktop.
Save qd3v/56c5691c799c9e62e81a to your computer and use it in GitHub Desktop.
test:
adapter: postgresql
encoding: unicode
host: localhost
database: gem_test
username:
password:
pool: 5
require 'bundler/gem_tasks'
require 'active_record'
# Temporary switch to blank rake app, extract required tasks and import them to
# current rake task. Skip seed loader here, as we do not need it for tests.
# More info on AR tasks import: @see https://gist.github.com/drogus/6087979
def import_active_record_tasks(default_rake_app)
Rake.application = Rake::Application.new
Rake.application.rake_require('active_record/railties/databases')
# Customize AR database tasks
include ActiveRecord::Tasks
db_dir = File.expand_path('../spec/db', __FILE__)
db_config_path = db_dir + '/database.yml'
migrations_path = db_dir + '/migrate'
# WARNING! This MUST be a String not a Symbol
DatabaseTasks.env = 'test'
DatabaseTasks.db_dir = db_dir
DatabaseTasks.database_configuration = YAML.load_file(db_config_path)
DatabaseTasks.migrations_paths = [migrations_path]
# Several AR tasks rely (but do not include) on this task internally (like :create)
# Here we establish connection. The first line is actually exists in :load_config
# which is called AFTER :environment. And because we have custom :environment
# task and want to connect, we copy that first line here.
# Not the best solution, but don't have time dig deeper
Rake::Task.define_task(:environment) do
ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
# Use Symbol or you'll get deprecation warning
ActiveRecord::Base.establish_connection(DatabaseTasks.env.to_sym)
end
tasks_to_import = %w[db:create db:drop db:purge db:rollback db:migrate
db:migrate:up db:migrate:down db:migrate:status db:version db:schema:load]
imported_tasks = Rake.application.tasks.select do |task|
tasks_to_import.include?(task.name)
end
# Restore default rake app
Rake.application = default_rake_app
# NOTE: didn't found a way to just use something like this tasks.define(task)
imported_tasks.each do |task|
# import description
Rake.application.last_description = task.comment
# import task
Rake::Task.define_task(task.name) { task.invoke }
end
end
import_active_record_tasks(Rake.application)
@murtyk
Copy link

murtyk commented Jan 20, 2016

Hi Ivan, thank you for putting this together. Saves me lot of time in research. In our case we need to namespace active record tasks since we already have them defined for sequel. We are merging another app developed in rails/active record into our app which is in cuba/sequel.

Any suggestions on name spacing active record tasks so that we can do something like:

rake db:migrate:
execute sequel migrations
invoke active record migration task

@ioquatix
Copy link

ioquatix commented Dec 7, 2016

@murtyk are you still looking for a solution? this might help: http://www.codeotaku.com/journal/2016-12/cool-things-about-rakefiles/index

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