Skip to content

Instantly share code, notes, and snippets.

@exAspArk
Last active August 29, 2015 14:04
Show Gist options
  • Save exAspArk/293cb8cf014b23763744 to your computer and use it in GitHub Desktop.
Save exAspArk/293cb8cf014b23763744 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 pg:db:create
  bundle exec rake pg:db:migrate 

Notes

  • From here having a more complex structure with more than one alternative database should be easy.

More resources

More info: http://stackoverflow.com/questions/1635885/rails-mysql-postgres-at-the-same-time-in-the-same-app

# 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 PostgresRecord::Base < ActiveRecord::Base
DATABASE_CONFIG_FILE = Rails.root.join('config/database.pg.yml')
# 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(File.read(DATABASE_CONFIG_FILE))
establish_connection(databases[Rails.env])
end
common: &common
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
min_messages: warning
development:
<<: *common
database: administrator_development
test:
<<: *common
database: administrator_test
production:
<<: *common
database: administrator_production
# 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
namespace :pg 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
ENV["SCHEMA"] = "db_pg/schema.rb"
Rails.application.config.paths["db"] = ["db_pg/pg"]
Rails.application.config.paths["db/migrate"] = ["db_pg/migrate"]
Rails.application.config.paths["db/seeds"] = ["db_pg/seeds.rb"]
Rails.application.config.paths["config/database"] = ["config/database.pg.yml"]
end
namespace :db do
namespace :test do
task prepare: :set_custom_db_config_paths do
Rake::Task["db:test:prepare"].invoke
end
end
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