Skip to content

Instantly share code, notes, and snippets.

@mothra
mothra / gist:6195516
Last active December 20, 2015 21:09
Postgres/Rails "copy from stdin"
#!/usr/bin/env ruby
#https://bitbucket.org/ged/ruby-pg/src/315fa972883163de298acd695f9b7fe65e07e5d3/sample/copyfrom.rb?at=default
require 'pg'
require 'stringio'
$stderr.puts "Opening database connection ..."
conn = PG.connect( :dbname => 'test' )
add_index :attachments, :parent_id
add_index :attachments, :asset_id
add_index :domain_names, :user_id
add_index :domain_names, :event_id
add_index :event_memberships, :user_id
add_index :event_memberships, :event_id
attachments: parent_id, asset_id
domain_names: organisation_id
event_memberships: user_id, event_id
events: editor_id
group_actions: user_id, group_id
groups: user_id
icons: parent_id
invitations: sender_id
legacy_actions: item_upon_id
news_items: author_id
@mothra
mothra / pre-commit
Last active December 15, 2015 05:19 — forked from bestie/pre-commit
#!/usr/bin/env ruby
# vim: set syntax=ruby
# Ensures that changes to the Rails schema.rb file may only be committed if a
# migration file is also committed at the same time.
# put this in .git/hooks/pre-commit
def schema_modified?
%x[ git diff --cached |grep schema.rb ] == ''
end
class IndexUsersEmails < ActiveRecord::Migration
def self.up
execute "END"
add_pg_index :users, :email, :lock => false
execute "BEGIN"
end
end