Skip to content

Instantly share code, notes, and snippets.

View robertsosinski's full-sized avatar

Robert Sosinski robertsosinski

View GitHub Profile
@robertsosinski
robertsosinski / bottom-up.sql
Created June 29, 2012 18:07
Recursive querys in PostgresSQL
with recursive category_ancestors (id, parent_id, name) as (
select id, parent_id, name
from categories where id = 123
union
select parent.id, parent.parent_id, parent.name
from categories parent
inner join category_ancestors on parent.id = category_ancestors.parent_id
)
select * from category_ancestors;
@robertsosinski
robertsosinski / gist:3014067
Last active October 6, 2015 15:27
Preferences.sublime-settings - User
{
"bold_folder_labels": true,
"color_scheme": "Packages/Color Scheme - Default/Github.tmTheme",
"font_size": 13.0,
"highlight_line": true,
"highlight_modified_tabs": true,
"line_padding_bottom": 1,
"line_padding_top": 1,
"scroll_past_end": true,
"show_full_path": true,
@robertsosinski
robertsosinski / gist:2691813
Created May 14, 2012 04:37
Testing Postgres Listen/Notify using EventMachine
require 'rubygems'
require 'pg'
require 'eventmachine'
module Subscriber
def initialize(pg)
@pg = pg
end
def notify_readable
@robertsosinski
robertsosinski / application.rb
Created May 13, 2012 00:50
Setup Database URL for QueueClassic
db_config = config.database_configuration[Rails.env]
scheme = db_config['adapter']
userinfo = [db_config['username'], db_config['password']].compact.join(':')
host = db_config['host'] || 'localhost'
port = db_config['port'] || 5432
database = db_config['database']
ENV['QC_DATABASE_URL'] = ENV['DATABASE_URL'] = URI::Generic.new(scheme, userinfo, host, port, nil, "/#{database}", nil, nil, nil).to_s
@robertsosinski
robertsosinski / gist:2631823
Created May 8, 2012 01:21
See overall stats on your PostgreSQL database
select
psut.schemaname,
pc.relname,
pg_size_pretty(pg_relation_size(pc.relname::varchar)) relsize_pret,
pg_size_pretty(pg_total_relation_size(pc.relname::varchar)) total_relsize_pret,
pg_relation_size(pc.relname::varchar) relsize,
pg_total_relation_size(pc.relname::varchar) total_relsize,
pc.reltuples::integer,
pc.relpages,
coalesce(round((8000 / (nullif(pc.reltuples, 0) / nullif(pc.relpages, 0)))), 0) avg_tuplesize,
@robertsosinski
robertsosinski / gist:2577498
Created May 2, 2012 15:29
Sign in a user for capybara tests
module RSpecHelpers
module InstanceMethods
def sign_in(user=nil)
user ||= User.make
fill_in 'user[email]', :with => user.email
fill_in 'user[password]', :with => user.password
click_button 'Sign in'
end
@robertsosinski
robertsosinski / daily.sh
Created April 30, 2012 01:41
PostgreSQL Backup Scripts
#!/bin/bash
filepath="/data/postgresql/9.1/main-backups"
cd $filepath
for database in `ls $filepath/daily`; do
filename=`date +"$filepath/daily/$database/$database-daily-%w-%a.dmp" | tr '[:upper:]' '[:lower:]'`
if [ -e $filename ]; then
@robertsosinski
robertsosinski / gist:2241169
Created March 29, 2012 18:05
Typecast observable values with Knockout
ko.extenders['typecast'] = function(target, type) {
target.subscribe(function(newValue) {
newValue = type(newValue);
// Setting subscription to NaN or undefined will start a never-ending loop.
if (!_.isNaN(newValue) && !_.isUndefined(newValue)) {
target(type(newValue));
}
});
@robertsosinski
robertsosinski / gist:2050753
Created March 16, 2012 16:05
Regex to slice up an array of composite types from postgres
# regex
regex = /(\((\d+),(\d+),(\d+),(t|f)\))/
# multiple
tuples = "{\"(309686,248320,15912,t)\",\"(309688,248320,24928,f)\",\"(309691,248320,49041,f)\"}"
tuples.scan(regex)
[["(309686,248320,15912,t)", "309686", "248320", "15912", "t"],
["(309688,248320,24928,f)", "309688", "248320", "24928", "f"],
@robertsosinski
robertsosinski / authenticatable.rb
Created March 11, 2012 21:47
Simple token/secret based authentication
module Api
module Authenticatable
class Unauthorized < Exception; end
def self.included(base)
base.send :before_filter, :verify_timestamp
base.send :before_filter, :verify_token
base.send :before_filter, :verify_signature
base.send :helper_method, :current_credential
end