nbudin / heroku_external_db

Makes it easy to connect to external databases from a Heroku app
MIT License
40 stars 5 forks source link

= PLEASE DON'T USE THIS GEM ANYMORE

Heroku officially supports using externally-hosted databases, so this gem is no longer necessary. For example, here's their documentation on how to use Amazon RDS databases: https://devcenter.heroku.com/articles/amazon-rds

= THANKS PLEASE STOP USING THIS

== heroku_external_db

Allows you to easily set up one or more external databases in Rails applications from Heroku, including the ability to do MySQL connections over SSL. This is useful in the following situations, for example:

== Installation

Add the following to your Gemfile:

gem 'heroku_external_db'

Or with the version number (easily found at https://rubygems.org/gems/heroku_external_db)

gem 'heroku_external_db', '~> x.y.z'

Then run:

bundle install rails generate heroku_external_db:generator

This will set up a default initializer in your app's +config/initializers/heroku_external_db.rb+.

NOTE: If the generator does not work on your setup, you can copy the initializer by hand.

== Tutorial

The generator makes the assumption that you're happy with using +EXTERNAL_DATABASE_URL+ and (optionally) +EXTERNAL_DATABASE_CA+ as your environment variables and that you want to use the external database as your main production DB. If that's not true, please edit the initializer as needed.

=== Example: MySQL with a CA Certificate

In this example, we are setting up a connection to an external MySQL server using the default initializer.

What we have:

First, we configure Heroku with the appropriate environment variables:

You may have to specify the app name or remote name here via --app or --remote, respectively

heroku config:add EXTERNAL_DATABASE_CA='ca-cert.pem' heroku config:add EXTERNAL_DATABASE_URL='mysql://username:password@server/dbname'

By default, +heroku_external_db+ looks for the CA cert in +config/ca+, so we need to commit it:

mkdir -p config/ca cp path/to/ca-cert.pem config/ca git add config/ca git commit -v # Using -v since we want to make sure the contents are what we expect (e.g. not a private key)

Additionally, we need the +mysql+ gem in our Gemfile since we are setting up a MySQL server:

echo "gem 'mysql', '~> 2.8.1'" >> Gemfile bundle install # Need Gemfile.lock too

Keep in mind that Heroku installs its own +database.yml+ for Rails apps and we have to install +pg+ as well. Unfortunately, shared databases are mandatory (but are free).

$ heroku addons:remove shared-database:5mb -----> Removing shared-database:5mb from our-app... failed ! Shared databases cannot be removed

NOTE: This seems to be changing in Cedar. See http://devcenter.heroku.com/articles/cedar#adding_a_database

PostgreSQL may still be useful to you if, for example, you want to have feature toggles in a local database, but the main data kept externally. However in our case, it also means all developers will need MySQL and PostgreSQL running locally, which is unfortunate.

One workaround is only installing +pg+ in production:

File: Gemfile

Only needed on production.

group :production do gem 'pg', '~> 0.11.0' # Regardless of whether you plan to use the database or not, Heroku requires you have 'pg' installed. end

Then +bundle+ like so:

$ bundle install --without production

With our gems updated, commit:

git commit -av # Commit Gemfile and Gemfile.lock

Another option may be overriding +database.yml+ somehow, although it would add complexity. For more info, please see http://stackoverflow.com/questions/4204724/strategies-for-overriding-database-yml.

With our dependencies out of the way, we can move on to testing the connection.

If you are making a new application, you may wish to have a simple MVC for testing that the connection works. E.g., for a blog style application with posts do:

rails generate scaffold post

NOTE you probably want to change the default "Post.all" to "Post.limit(5)" or something similar

git add . git commit # ...

Don't forget to set a default route, etc.

With all these changes committed, we can deploy to our Heroku app:

git push heroku master # Your remote may be different

Now, since we are connecting to an existing database, we don't need to run any migrations. (Keep in mind that when sharing a database, it is best to have one authoritative source for migrations to live.) If in your situation you're creating a new database, you may need to do that, run migrations, seed the database, etc at this point.

Open http://our-app.heroku.com and we should see our data. If you happen to run into a problem, please check the logs first:

heroku logs --tail # Again, you may need to specify an app

If you are having a problem, a good starting point is double checking your passwords, usernames, security settings, etc.

=== Example: MySQL with X.509

The process is very much the same as the above example, except two extra environment variables and files are required. Below are the extra steps.

What we have:

First, we configure Heroku with the appropriate environment variables:

heroku config:add EXTERNAL_DATABASE_CA='ca-cert.pem' heroku config:add EXTERNAL_DATABASE_CERT='client-cert.pem' heroku config:add EXTERNAL_DATABASE_KEY='client-key.pem'

By default, +heroku_external_db+ looks for the files in +config/ca+, so we need to commit them:

mkdir -p config/ca cp path/to/ca-cert.pem path/to/client-cert.pem path/to/client-key.pem config/ca git add config/ca git commit

The rest of the process is the same as in "MySQL with a CA Certificate".

== Contributing to heroku_external_db

=== Setting up Environment and Running Specs

./configure rake

== Copyright

Copyright (c) 2011 Nat Budin. See LICENSE.txt for further details.