Tuesday, June 19, 2012

Sync database from production db on Heroku

One could use the awesome taps gem like so:

heroku db:pull

However that gets real slow real quick so I had to use PG Backups addon instead

# Install heroku addon
heroku addons:add pgbackups

# Capture latest db snapshot on heroku
heroku pgbackups:capture

# Get download url and enter it in browser with a heroku login session
heroku pgbackups:url {latest_backup_name}

# Restore pg dump into local PostgreSQL database
pg_restore -d {db_name} ~/Downloads/{latest_backup_name}.dump

Starting PostgreSQL server on OSX


pg_ctl -D /usr/local/var/postgres start

Thursday, June 14, 2012

Force remove files in OSX Trash


rm ~/.Trash/xxx

Monday, June 11, 2012

Rails number_to_percentage always 0

This is a silly mistake that I often make.

# This always return a big fat zero
number_to_percentage(User.count/Signup.count)

# Remember to have divide by floats!
number_to_percentage(User.count.to_f/Signup.count.to_f)

Check if string is a JSON in Rails


# config/initializers/string_methods.rb

require 'json'

class String
  def is_json?
    begin
      !!JSON.parse(self)
    rescue
      false
    end
  end
end

Friday, June 8, 2012

Switch database from MySQL (ClearDB addon) back to PostgresSQL in Heroku

Had permission problems using ClearDB and figure it wasn't worth all the hassle of adding another layer of abstraction/configuration when Heroku already works with PostgreSQL out of the box. Here's how to switch back to PG.


# Remove addon
heroku addons:remove cleardb

# Set DATABASE_URL back to PG
heroku config | grep SHARED_DATABASE_URL # => postgres://XXX
heroku config:add DATABASE_URL='postgres://XXX'

Wednesday, June 6, 2012

ClearDB MySQL on Heroku gotcha

Didn't know that a rails project deployed on heroku was using ClearDB MySQL as production database - I just assumed that it's PostgreSQL on Heroku till I saw that ClearDB addon was used. Removing the addon and mysql gems and configuring config/database.yml did not saw the application switched back to PG. Read the ClearDB addon and realized that DATABASE_URL environment was set.

https://devcenter.heroku.com/articles/cleardb