Skip to content

Instantly share code, notes, and snippets.

@dpsk
Last active December 17, 2015 12:38
Show Gist options
  • Save dpsk/5610804 to your computer and use it in GitHub Desktop.
Save dpsk/5610804 to your computer and use it in GitHub Desktop.

Commands examples

If the namespace is not used then the commands will perform on top of the default database. bundle exec rake db:create bundle exec rake db:migrate

By using the namespace we are going to use all the configuration for our alternate DB. bundle exec rake store:db:create bundle exec rake store:db:migrate

Notes

  • From here having a more complex structure with more than one alternative database should be easy.
#TODO: Rework this as mixin!
# models/data_store_base.rb
# Public: Provide a base class for accessing the store database.
# All models inheriting from this class will use by default the data store DB.
class DataStoreBase < ActiveRecord::Base
# Tell Active Record (AR) to not look up for a table called like this class,
# since this class is only used to add custom config for AR
self.abstract_class = true
databases = YAML::load(IO.read('config/database_store.yml'))
establish_connection(databases[Rails.env])
end
# config/database.yml
development: &development
adapter: sqlite3
database: db/administrator_development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/administrator_test.sqlite3
pool: 5
timeout: 5000
# config/database_store.yml
development: &development
adapter: sqlite3
database: db_store/data_store_development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db_store/data_store_test.sqlite3
pool: 5
timeout: 5000
# lib/tasks/databases.rake
# Public: This Rake file tries to add what rails provides on the
# databases.rake but for building on top of custom databases.
# Basically we get the nice db:migrate but for using it on a different DB than
# the default, by calling it with the namespace defined here.
#
# In order to be able to use the default rails rake commands but on a different
# DB, we are first updating the Rails.application.config.paths and then
# calling the original rake task. Rails.application.config.paths is getting
# loaded and available as soon as we call rake since the rakefile in a rails
# project declares that. Look at Rakefile in the project root for more details.
# Public: Access to the same commands you do for DB operations,
# like db:drop, db:migrate but using the store namespace, this will
# execute on top of the store DB.
namespace :store do
desc "Configure the variables that rails need in order to look up for the db
configuration in a different folder"
task :set_custom_db_config_paths do
# This is the minimum required to tell rails to use a different location
# for all the files related to the database.
ENV['SCHEMA'] = 'db_store/schema.rb'
Rails.application.config.paths['db'] = ['db_store']
Rails.application.config.paths['db/migrate'] = ['db_store/migrate']
Rails.application.config.paths['db/seeds'] = ['db_store/seeds.rb']
Rails.application.config.paths['config/database'] = ['config/database_store.yml']
end
namespace :db do
task :drop => :set_custom_db_config_paths do
Rake::Task["db:drop"].invoke
end
task :create => :set_custom_db_config_paths do
Rake::Task["db:create"].invoke
end
task :migrate => :set_custom_db_config_paths do
Rake::Task["db:migrate"].invoke
end
task :rollback => :set_custom_db_config_paths do
Rake::Task["db:rollback"].invoke
end
task :seed => :set_custom_db_config_paths do
Rake::Task["db:seed"].invoke
end
task :version => :set_custom_db_config_paths do
Rake::Task["db:version"].invoke
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment