shawn bright
shawn bright

Reputation: 93

how to import and execute database connection in a script

I am writing many scripts to run housekeeping tasks on our system. At the top of each script, I establish a connection to the database using ActiveRecord::Base.connection. I would like to keep this bit in it's own file, so If I move to another server, i can change it in only one place, but I have not found a way to do this.

ActiveRecord::Base::establish_connection({                                                                                                                                                                                              
    :adapter   => 'mysql',                                                                                                                                                                                                              
    :host      => 'ip.address',                                                                                                                                                                                                         
    :database  => 'test-db',                                                                                                                                                                                                            
    :username  => 'userName',                                                                                                                                                                                                           
    :password  => 'passWord'})   

load_models
keyWord = KeyWord.find(:all)

i need something like a function i can just call like

def set_connection
   connection code here
   return connection
end

where, if run, my script will be connected to the database.

i write my scripts using only the rubygems and activerecord gems. Not inside a rails app.

thanks for any tips

Upvotes: 1

Views: 472

Answers (2)

peter
peter

Reputation: 42182

The usual way is defining your connection and other data that could change is in a configuration file. In Ruby this means most of the times a YAML file. You can define multiple environments (develop, production, ..) in your config so you won't have to change a thing when moving your code or you can have multiple configs.

In your config.yaml file

connection:
    adapter:  mysql
    host:     xxx.xxx.xxx.xxx                                                                                                                                                                                                      
    database: test-db                                                                                                                                                                                                            
    username: userName                                                                                                                                                            
    password: passWord

In your main, helpers or model file

$config = YAML::load_file("./config.yaml")
ActiveRecord::Base.establish_connection($config.connection)

Upvotes: 1

shawn bright
shawn bright

Reputation: 93

found my best answer here to be to just make a hash in another file with connection parameters, then pass that hash into the establish_connection

so i have a file db_params.rb

def params 
  return {
    :adapter   => 'mysql',                                                                                                                                                                                                              
    :host      => 'ip.address',                                                                                                                                                                                                         
    :database  => 'test-db',                                                                                                                                                                                                            
    :username  => 'userName',                                                                                                                                                                                                           
    :password  => 'passWord'
  }
end

i require('db_params.rb') then in the file I need to connect, i use..

p = params()
ActiveRecord::Base::establish_connection(p)

Upvotes: 0

Related Questions