Skip to content

Instantly share code, notes, and snippets.

View hgmnz's full-sized avatar

Harold Giménez hgmnz

View GitHub Profile
WITH weeks AS (
SELECT distinct date_trunc('week', dates)::date AS start,
daterange(date_trunc('week', dates)::date, (date_trunc('week', dates) + interval '7 days')::date) week
FROM generate_series(date '2013-01-01', now(), '1 day')
dates ORDER BY 1
)
SELECT weeks.start,
count(whatever.*) as count
FROM whatever
JOIN weeks
@hgmnz
hgmnz / dblink-demo
Created March 6, 2013 16:33
How to move data from one postgres database to another using dblink. Useful for cherry-picking data from a remote database
# drop this in an initializer
class ActiveRecordOverrideRailtie < Rails::Railtie
initializer "active_record.initialize_database.override" do |app|
ActiveRecord::Base.connection_pool.disconnect!
ActiveSupport.on_load(:active_record) do
if url = ENV['DATABASE_URL']
parsed_url = URI.parse(url)
establish_connection( {
require 'rubygems'
require 'tweetstream'
require 'sequel'
DB = Sequel.connect('postgres:///bostonio')
TweetStream.configure do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.oauth_token = ENV['TWITTER_OAUTH_TOKEN']
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAw79BbQmsMbYTq1kc+7aOjnQEPkx5O/0rcSyn1ymh4AhhJIlmSvHmXDi2L/spJf7KlCRDETdgoKr8Gd10PZsg4tYNzZn1UCyiWPNrGW4fdo3F1r1r5qgH3knucM8oPTLqaYUIsoAcnlmpf+QsF8pxNmjYiTVjLfc9sXVGNp5hClnLokwdgtA6clbwRjo3mGNvre+m2MkgQFLh9/ovBsP1sJ81PDCpTfm0zgO621E4HdeNgUcrAl6h3HtSvfhpZ5EBFn2/riiaqAR15w7hpvWsrmmEanfgM3efjx1HW8M0DFsYBPKyeymYK+leYetGJN0Avtp0+QV+sompoXkKP+MvhQ== hgimenez@Harold-Gimenezs-MacBook-Pro.local
notify_deploy_environments = %w(staging production integration)
notify_deploy_roles = %w(solo app_master)
if notify_deploy_environments.include?(@configuration[:environment]) && notify_deploy_roles.include?(node['instance_role'])
run "cd #{release_path} && bundle exec rake hoptoad:deploy TO=#{@configuration[:environment].strip} REVISION=#{@configuration[:revision].strip} USER=`whoami` REPO=#{@configuration[:repo].strip}"
end
@hgmnz
hgmnz / query_planner.markdown
Created March 23, 2011 14:14
PostgreSQL query plan and SQL performance notes

Types of index scans

Indexes

Sequential Scan:

  • Read every row in the table
  • No reading of index. Reading from indexes is also expensive.
@hgmnz
hgmnz / gist:877001
Created March 18, 2011 22:57
rvm gemset export hoptoad_notifier
# hoptoad_notifier generated gem export file. Note that any env variable settings will be missing. Append these after using a ';' field separator
abstract -v1.0.0
actionmailer -v3.0.3
actionmailer -v3.0.2
actionmailer -v3.0.1
actionmailer -v3.0.0
actionmailer -v2.3.8
actionpack -v3.0.3
actionpack -v3.0.2
actionpack -v3.0.1
@hgmnz
hgmnz / gist:719171
Created November 28, 2010 18:34
Object#method
ree-1.8.7-2010.02 > some_string = "hi there"
=> "hi there"
ree-1.8.7-2010.02 > some_string.capitalize
=> "Hi there"
ree-1.8.7-2010.02 > some_method = some_string.method :capitalize
=> #<Method: String#capitalize>
ree-1.8.7-2010.02 > some_method.call
=> "Hi there"
ree-1.8.7-2010.02 > String.class_eval { remove_method(:capitalize) }
=> String
# authorization matchers
module AuthorizationMatcher
class RequireAuthentication
def initialize(method, action, params, context)
@method = method
@action = action
@params = params
@context = context
end