passenger - Dynamically defining the database name in a Ruby Rails application -
is there way of setting database name dynamically (coming database.yml), ideally using part of application pathname?
background of question is, have single source code shall run on 1 , same server multiple times each instance of application should have different database.
example, have auction site works currency usd. want run same auction site again (one same server) second currency. valid reasons don't want make application multi-currency capable, i'ld keep source code is.
setting application on same server using sub-url follow approach: http://www.modrails.com/documentation/users%20guide%20apache.html#deploying_rack_to_sub_uri
the question left is, how each instance of application own db name, e.g. 1 instance uses database name production_usd , otherone uses production_cad
edit: solution works charm, feedback received:
my folder structure on server is
/var/www/auction/usd-us dollar /var/www/auction/cad-canadian dollar /var/www/auction/source /var/www/logs
with source folder containing original source code , usd , cad being links source (no actual need copying code anywhere other placing source.
next set db dynamically. currency determined automatically looking @ folder name. put application.rb need in stage because want different log files different currencies. storing log files outside of source folder make sure don't loose them when source folder gets refreshed qa system
here code changes:
application.rb:
fname = file.basename(file.expand_path('../../', __file__)) curr = fname.split("-") if curr[1].nil? curr_short = "xxx" curr_long = "xxx" else curr_short = curr[0] curr_long = curr[1] end dbname = "myapp_#{curr_short}_#{rails.env[0..2]}" activerecord::base.establish_connection( :adapter => "sqlite3", :host => "localhost", :username => "myuser", :password => "mypass", :database => dbname
)
module virex class application < rails::application config.logger = activesupport::bufferedlogger.new("../logs/#{rails.env}.#{curr_short}.log") ....
of course, have @ activerecord::base.establish_connection
:
activerecord::base.establish_connection( :adapter => "mysql", :host => "localhost", :username => "myuser", :password => "mypass", :database => "somedatabase" )
you can put piece of code in initializer, database name want, depending on pathname.
Comments
Post a Comment