tl;dr Skip blog and get straight to the code.
Establishing ActiveRecord Databases
Let's cover the basics. How do we establish a basic database connection?require 'active_record' ActiveRecord::Base.establish_connection({ adapter: 'mysql', database: 'mydatabase', username: 'user', password: 'pass', host: 'localhost' })The code is fairly straightforward. This tells ActiveRecord to open a connection to a local MySQL database named mydatabase and use user and pass as the login credentials. Now, we can do cool things with our database like create tables and populate them with stuff using models:
ActiveRecord::Schema.define do create_table :dogs do |t| t.integer :id, :null => false t.string :name t.date :dob end end class Dog < ActiveRecord::Base end Dog.create({ id: 0, name: 'Scruffy', dob: '2012-01-01' })
Choose a Configuration using Environment Variables
Let's recreate the "magic" in Rails by reading in an environment variable to decide which database connection to open. We do this by using ENV:require 'active_record' conf = case ENV['DB'] when 'conf1' { adapter: 'sqlite3', database: 'db/mydb1.sqlite3' } when 'conf2' { adapter: 'sqlite3', database: 'db/mydb2.sqlite3' } else raise 'export DB=conf[n]' end ActiveRecord::Base.establish_connection confBefore we run this script, we need to set our environment variable:
export DB=conf1 # or conf2
Externalize Database Configurations
Never store the database configurations in your code. If you distribute the code, all of the information will be available for people to see! This may or may not be a security issue in your situation, but it's better to play it safe and store database configurations somewhere else. Let's call our database configuration file database.yml (looks familiar):conf1: adapter: sqlite3 database: db/mydb1.sqlite3 conf2: adapter: sqlite3 database: db/mydb2.sqlite3Let's tell ActiveRecord to establish a connection to the proper database by loading our YAML file:
require 'active_record' conf = YAML.load_file('database.yml') ActiveRecord::Base.establish_connection conf[ENV['DB']]Success! We can set our environment variable the same way as before, but our code will look in a YAML file for the proper database configuration rather than some hashes in code.
No comments:
Post a Comment