Strategies for Overriding Database.Yml

Strategies for overriding database.yml?

There's two tricks which might help here. One is what you've touched on, that being monkey-patching the routine that loads in the database configuration, and that's certainly something worth exploring. The nice thing about Ruby is you can pretty much patch out anything you don't like and replace it with something better. The liability here is that a newer version of Rails might use a different mechanism for configuration and your patch will cause everything to break. Maybe that's a price worth paying.

The other approach is to keep the database.yml file on the server instead of in your repository, and have some kind of deployment script that links it in to the appropriate location. The advantage to this approach is you don't have important passwords floating around in your version control system and you can update a server's configuration without having to patch the application.

Strategies for overriding database.yml

Use ActiveRecord::ConnectionHandling.establish_connection function call to connect to your database. You should be able to establish a connection with a different user even on every page if you would like, or look up the database credentials in a User model.

If you are doing this, you might consider subclassing ActiveRecord::Base and overriding the connection call, which would then be used by every model to connect to the database.

If you want to completely avoid a connection to the database before you explicitly set up the credentials through your application, then you probably want to initialize Rails without loading all of the ActiveRecord specific stuff. In that case, you will need to remove ActiveRecord from the initialization of Rails by putting this line in your config/environment.rb, within the Rails::Initializer.run do |config| block:

config.frameworks -= [ :active_record ]

Then you will need to load all of the ActiveRecord specific things once you have the credentials.

Alternatively, you can override Rails::Initialize::initialize_database which is where the actual database connection is being used by putting this in your environment.rb before the Rails::Initializer.run call:

module Rails
class Initializer
def initialize_database
nil
end
end
end

Prior to rails 4.x you would use ActiveRecord::Base.establish_connection, which is now deprecated.

Database.yml configuration options

I found a gist of database.yml examples using mysql, postgres, and sqlite3, and the Rails 3.2 source code for connection adapters provides good insight as well.

Looks to me that the following are the most widely used options:

  • adapter
  • encoding
  • database
  • pool
  • username
  • password
  • socket
  • host
  • port
  • timeout

The Rails 3.2 connection_specification.rb file looks like it simply merges any options you include, so I'd say what options you include are dependant on the database adapter you choose to use (lines 58-74):

def connection_url_to_hash(url) # :nodoc:
config = URI.parse url
adapter = config.scheme
adapter = "postgresql" if adapter == "postgres"
spec = { :adapter => adapter,
:username => config.user,
:password => config.password,
:port => config.port,
:database => config.path.sub(%r{^/},""),
:host => config.host }
spec.reject!{ |_,value| !value }
if config.query
options = Hash[config.query.split("&").map{ |pair| pair.split("=") }].symbolize_keys
spec.merge!(options)
end
spec
end

Overriding setting in YAML using a strategy from the default file?

Using the node-config-yml library actually did the trick, Despite it's sparse documentation on the matter it works perfectly.

It appears to make the configuration into one file in memory from both of them. You can use aliases, and the merge function from node-config and just override as needed.

How can I override the JPA properties for multiple datasources for Integration tests?

It seems you have a general application.yml file and a specific application-test.properties file for the test environment. I'm not sure if you can mix those file extensions, probably not. Either you choose to use .yml or .properties for both. Try to change the file name to application-test.yml. Also, to activate this specific test environment it's necessary to put this config in application.yml:

spring:
profiles:
active:test

How to correctly setup a database.yml file in Rails 4

The second database.yml you posted is actually equivalent to the first, it just copies values from the development block.

To answer your other questions:

1) Should I be setting usernames and passwords in ALL environments

you can if you wish, or you can leave it as you have above where it takes the credentials all from one block.

2)If I'm using clear DB with Heroku as my database then should I be uncommenting

heroku actually completely disregards this file (which shouldn't be checked into source control in the first place anyway). Heroku has its own mechanism to handle databases which you can read more about here: https://devcenter.heroku.com/articles/heroku-postgresql

Essentially, treat "heroku's databases" and local databases that you define in this file completely different.

Multiple database.yml but one application

As Mladen said, you can use RailsEnv. It's pretty much the ideal solution to your problem, it's intended to be used that way. Just don't forget to set different PassengerAppGroupName values for each env because Phusion Passenger normally uniquely identifies an application based on its path only. Also don't forget to make the config/initializers/[env name].rb file.

Where is defined path to database.yml in rails?

It is hardcoded to be <app-root>/config/database.yml. In production environment where you store it in a shared folder, you must create a symlink that points to that shared file.

 <app-root>/config/database.yml -> ~/sites/shared/config/database.yml

Or you could symlink the whole config directory, if you wanted.

Does Connecting to database specified by database.yml simply indicate that the server has started

As this question has been sat here for four months without an answer, I think the answer is:

Yes,

Connecting to database specified by database.yml

in a rails log simply indicates that the server has restarted.

Removing database.yml when using Mongoid in Rails 3.2

Maybe comment this two lines

config/environments/development.rb:  

# config.active_record.mass_assignment_sanitizer = :strict
# config.active_record.auto_explain_threshold_in_seconds = 0.5


Related Topics



Leave a reply



Submit