Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am dougcole on github.
  • I am dougcole (https://keybase.io/dougcole) on keybase.
  • I have a public key ASDTtsuupEhbdaE_b8iZcPro2VddC8iHN3MiGAiS0jqxUgo

To claim this, I am signing this object:

@dougcole
dougcole / nilify_blanks.rb
Created March 20, 2014 07:10
Handle empty strings with ActiveRecord
module NilifyBlanks
def nilify_blanks(*columns)
columns.each do |column|
value = read_attribute(column)
write_attribute(column, nil) if value.blank?
end
end
end
@dougcole
dougcole / gist:4210726
Created December 5, 2012 00:36
Simple class for running an ActiveRecord query in a separate thread.
class ConcurrentQuery < BasicObject
def initialize(&block)
@query = ::Thread.new do
result = begin
block.call
ensure
::ActiveRecord::Base.connection_pool.release_connection
end
result
@dougcole
dougcole / gist:2127622
Created March 19, 2012 22:17
Rspec should_not access_database
class AccessDatabase
def initialize
@query_count = 0
@query_sql = []
end
def matches?(target)
subscription = ActiveSupport::Notifications.subscribe(/^sql\./) do |*args|
@query_count += 1
@query_sql << args.last[:sql]
@dougcole
dougcole / run_tags.rb
Created June 29, 2011 18:40 — forked from tobias/run_tags.rb
A script for generating TAGS from a git hook.
#!/usr/bin/ruby
#-*-ruby-*-
# A script to run ctags on all .rb files in a project. Can be run on
# the current dir, called from a git callback, or install itself as a
# git post-merge and post-commit callback.
CTAGS = '/usr/local/bin/ctags'
HOOKS = %w{ post-merge post-commit post-checkout }
HOOKS_DIR = '.git/hooks'
@dougcole
dougcole / postgresql admin queries
Created October 20, 2009 00:23
postgresql admin queries
***find the biggest tables
select relname, pg_size_pretty(pg_relation_size(relname::text)) from pg_stat_user_tables where schemaname= 'public' order by pg_relation_size(relname::text) desc;
***find the biggest indexes
select tablename, indexname, pg_size_pretty(pg_relation_size(indexname::text)) from pg_indexes where schemaname= 'public' order by pg_relation_size(indexname::text) desc;
***find indexes that are almost never used sorted by size.
-if the application has changed significantly since the database was built, run pg_stat_reset() and wait for a few days to view only up-to-date statistics.
SELECT idstat.relname AS table_name,
indexrelname AS index_name,
select pg_class.relname,pg_locks.mode,pg_stat_activity.current_query from pg_class,pg_locks,pg_stat_activity where pg_class.relfilenode=pg_locks.relation and pg_stat_activity.procpid=pg_locks.pid;