This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Color Pry prompt according to environment | |
old_prompt = Pry.config.prompt | |
color = { | |
:red => "31m", | |
:green => "32m", | |
:yellow => "33m", | |
:blue => "34m", | |
:purple => "35m", | |
:cyan => "36m" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :db do | |
desc "Migrates the database to the target version, or to the lastest version if no target is given" | |
task :migrate, [:target, :current] => :environment do |t, args| | |
opts = {} | |
opts[:target] = args[:target].to_i if args[:target] | |
opts[:current] = args[:current].to_i if args[:current] | |
Sequel::Migrator.run(DB, "db/migrate", opts) | |
Rake::Task["db:dump"].invoke if Rails.env.development? | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create trigger trades_readonly_trigger before insert or update or delete or truncate on trades for each statement execute procedure readonly_trigger_function(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with | |
blocked as ( | |
select | |
pl.transactionid, | |
pl.pid blocked_pid, pl.mode blocked_mode, | |
psa.usename blocked_user, psa.application_name blocked_application, | |
now() - psa.query_start blocked_duration, psa.xact_start blocked_xact_start, psa.query_start blocked_query_start, | |
psa.current_query blocked_statement | |
from pg_stat_activity psa | |
inner join pg_locks pl on pl.pid = psa.procpid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostgresSessionStore < ActiveRecord::SessionStore::SqlBypass | |
@@session_id_column = 'id' | |
@@schema_name = 'system' | |
cattr_accessor :schema_name | |
class << self | |
def marshal(data) | |
::Base64.encode64(Marshal.dump(data)).chomp if data | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace function set_keyval(tablename varchar, keyname varchar, valuestring text, schemaname varchar default 'public') returns table(key varchar, value text, created_at timestamp, updated_at timestamp) as $$ | |
begin | |
return query execute 'update '||quote_ident(schemaname)||'.'||quote_ident(tablename)||' set value = $2, updated_at = now() where key = $1 returning *' using keyname, valuestring; | |
if not found then | |
return query execute 'insert into '||quote_ident(schemaname)||'.'||quote_ident(tablename)||' (key, value, created_at, updated_at) values ($1, $2, now(), now()) returning *' using keyname, valuestring; | |
end if; | |
end; | |
$$ language 'plpgsql'; | |
create or replace function get_keyval(tablename varchar, keyname varchar, schemaname varchar default 'public') returns table(key varchar, value text, created_at timestamp, updated_at timestamp) as $$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT c.id, c.name, c.jan, c.feb, c.mar | |
FROM crosstab( | |
'select buyers.id, buyers.name, transactions.month, sum(transactions.amount) from transactions inner join buyers on buyers.id = transactions.buyer_id group by buyers.id, buyers.name, transactions.month order by buyers.id, transactions.month', | |
'select generate_series(1, 3)' | |
) c (id integer, name character varying, jan numeric(12,2), feb numeric(12,2), mar numeric(12,2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Inventory | |
class Base < ActiveRecord::Base | |
self.abstract_class = true | |
self.table_name_prefix = 'inventory.' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(parser). | |
-export([parse/1]). | |
% Example 1: | |
% | |
% Tuple1 = {person, [ | |
% {name, [{first, "Robert"}, {last, "Sosinski"}]}, | |
% {home, [{city, "Hoboken"}, {state, "New Jersey"}]}, | |
% {info, [{age, 30}, {hobbies, [saxophone, beer, french]}]} | |
% ]}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'pg' | |
require 'eventmachine' | |
module Subscriber | |
def initialize(pg) | |
@pg = pg | |
end | |
def notify_readable |