Skip to content

Instantly share code, notes, and snippets.

@sao
sao / database.yml
Created September 9, 2014 16:10
Dynamic database names on git checkout.
#
# requires 'gem install git'
#
<%
current_branch = Git.open('.').current_branch
unless current_branch =~ /(master|rebasing)/
branch = "_#{current_branch}"
end
%>
@sao
sao / db_helpers.rb
Last active August 29, 2015 14:06
Adds `rake db:import` to automatically download the newest backup from pg:backups and import the data into your current local database.
#
# This requires the Heroku toolbelt
#
# Add 'gem install httparty' to your Gemfile
# and make sure you have a default Heroku remote setup
# in your git config (make sure you can run 'heroku ps'
# without specifying an app name.
#
require 'httparty'
@sao
sao / database_commands.sh
Last active December 21, 2015 07:58
Add user and database to postgres from terminal
createuser --no-createdb --no-createrole --no-superuser [username]
createdb --owner [username] --encoding utf8 [database]
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U [username] -d [database] -p 5432 [data_file]
PGUSER=[username] heroku pg:pull HEROKU_POSTGRESQL_NAME [local_database] --app app-name
@sao
sao / count_all_rows.sql
Last active December 21, 2015 01:09
count rows for all tables
SELECT schemaname,relname,n_live_tup
FROM pg_stat_user_tables
ORDER BY n_live_tup DESC;
@sao
sao / masker.rb
Last active December 19, 2015 07:39
Masker: Custom image processor for Paperclip (requires masking image)
module Paperclip
class Masker < Processor
def initialize file, options = {}, attachment = nil
super
@format = File.extname(@file.path)
@basename = File.basename(@file.path, @format)
end
def make
source = @file