Skip to content

Instantly share code, notes, and snippets.

@mswieboda
Last active October 14, 2016 19:52
Show Gist options
  • Save mswieboda/92b205153f79e7d62cad6bf205c34002 to your computer and use it in GitHub Desktop.
Save mswieboda/92b205153f79e7d62cad6bf205c34002 to your computer and use it in GitHub Desktop.
Swap Rails development DBs with DATABASE_URL

Alt DBs ⚡️ Talk

Uses

Want to swap development databases? Sick of dropping, migrating and setting up databases when swtiching branches? This ⚡️ talk's for you!

Database Config Options

The sections Configuring a Database and Connection Preference in the Rails Guides explains that there are two ways to configure your DB.

Database YML

This is how we config our database in Centro Platform. config/database.yml looks like this:

development:
  adapter: postgresql
  host: localhost
  database: cmm_development

Environment Variable

The other way is via an Environment variable DATABASE_URL would would use the same info as above, formatted as a URL such as:

postgres://localhost/cmm_development

The other thing of note about DATABASE_URL is it overrides config/database.yml, so we can specify a different database name, like cmm_dump for example:

$ export DATABASE_URL=postgres://localhost/cmm_dump

And bam 💥 our development environment will now use cmm_dump instead of cmm_development. We'll need to make sure the db is created in postgres though:

$ createdb cmm_dump

If you're using powder, you'll have to do an extra step, make sure that ENV var is set in powder too.

powder env DATABASE_URL postgres://localhost/cmm_dump

(You'll need to restart powder too, but I find powder down && powder up most effective just in case instead of powder restart.)

You can output all powder's set environment vars with powder env.

Now we can take a prod/staging dump, and populate our new db as usual:

$ psql cmm_dump < cmm_dump.sql

Making sure we bundle exec rake db:migrate etc.

Switching Branches/Contexts

But say we're working on a bunch of git branches, and need to switch contexts for taking care of an open PR's comments. But we don't want the DB dump, in fact, we need to start from stratch since our branch has new DB migrations. Well simple, let's switch back to our original cmm_development database.

Simply unset the DATABASE_URL environment variable:

$ unset DATABASE_URL

(and powder's if you're using it, with a down/up restart)

$ powder env DATABASE_URL

And bam 💥 our development environment is now using use cmm_development again. Rinse wash and repeat to go back to your cmm_dump DB, or any other one.

Bash/Zsh Profile using aliases & functions

alias ldb_dev='unset DATABASE_URL && ldb_powder_dev'
alias ldb_powder_dev='powder env DATABASE_URL && powder down && powder up'
alias ldb_echo='echo $DATABASE_URL && powder env'

function ldb_dump() {
  db="$1"

  # Default is cmm_dump if no args passed
  if [[ -z "$db" ]]; then
    db="cmm_dump"
  fi

  # TODO: would be nice to only create if DB doesn't exist
  # This requires some grepping and non-simple commands, so not inluding for simplicity.
  # If the DB exists, an error is thrown, but this will continue
  createdb "$db"

  export DATABASE_URL="postgres://localhost/$db"
  echo "DATABASE_URL set to: $DATABASE_URL"
  powder env DATABASE_URL
  powder env DATABASE_URL "postgres://localhost/$db"
  echo "powder env:"
  powder env
  powder down
  powder up
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment