NomadicRiley
NomadicRiley

Reputation: 111

Rake - how to use to modify database.yml config prior to invoking task?

I have a database with a public schema where we keep login/account information and multiple client schemas for client data. I've successfully used yaml_db to dump/load test data into a client schema, but this requires constantly modifying the schema_search_path between the public schema and client specific one each time I want to reload data.

I'm trying to build a rake task to automate this, but am unsure how to modify schema_search_path in prior to invoking the db:data:load task.

I can view the current values in database.yml by doing this:

env = "#{RAILS_ENV}"
config = YAML::load(File.open('config/database.yml'))
puts config[env]["schema_search_path"]

But I'm unclear how to modify that value to be used when i call

Rake::Task['db:data:load'].invoke

I've tried just assigning a new value to the

config[env]["schema_search_path"] = "test_data_schema"

but that doesn't appear to work

Upvotes: 2

Views: 1417

Answers (1)

NomadicRiley
NomadicRiley

Reputation: 111

Figured this out - just needed to establish the ActiveRecord::Base connection with modified config prior to invoking the Rake task:

ActiveRecord::Base.establish_connection(config[env])
Rake::Task['db:data:load'].invoke()

Upvotes: 3

Related Questions