Skip to content

Instantly share code, notes, and snippets.

@jberkus
jberkus / gist:6b1bcaf7724dfc2a54f3
Last active January 7, 2024 21:26
Finding Unused Indexes
WITH table_scans as (
SELECT relid,
tables.idx_scan + tables.seq_scan as all_scans,
( tables.n_tup_ins + tables.n_tup_upd + tables.n_tup_del ) as writes,
pg_relation_size(relid) as table_size
FROM pg_stat_user_tables as tables
),
all_writes as (
SELECT sum(writes) as total_writes
FROM table_scans
@peterc
peterc / methods_returning.rb
Last active October 29, 2023 03:10
Object#methods_returning - to work out which method on an object returns what we want
require 'stringio'
require 'timeout'
class Object
def methods_returning(expected, *args, &blk)
old_stdout = $>
$> = StringIO.new
methods.select do |meth|
Timeout::timeout(1) { dup.public_send(meth, *args, &blk) == expected rescue false } rescue false
# Goal: Allow addition of instances to a collection in a factory-built object
# when those instances require references to the parent.
# Typically occurs in Rails when one model has_many instances of another
# See more at:
# http://stackoverflow.com/questions/2937326/populating-an-association-with-children-in-factory-girl
class Factory
def has_many(collection)
# after_build is where you add instances to the factory-built collection.
@nbibler
nbibler / gist:5307941
Last active October 7, 2021 09:38
A .powrc file which works with RVM's .rvmrc or .ruby-version (+ .ruby-gemset) configuration files.
if [ -f "$rvm_path/scripts/rvm" ]; then
source "$rvm_path/scripts/rvm"
if [ -f ".rvmrc" ]; then
source ".rvmrc"
fi
if [ -f ".ruby-version" ]; then
rvm use `cat .ruby-version`
fi
@albertstill
albertstill / enigma_machine.rb
Last active February 25, 2021 18:46
Understand how the Enigma machine works with 30 lines of Ruby
Plugboard = Hash[*('A'..'Z').to_a.sample(20)]
Plugboard.merge!(Plugboard.invert)
Plugboard.default_proc = proc { |_, key| key }
def build_a_rotor
Hash[('A'..'Z').zip(('A'..'Z').to_a.shuffle)]
end
ROTOR_1, ROTOR_2, ROTOR_3 = build_a_rotor, build_a_rotor, build_a_rotor
@Voronenko
Voronenko / downgrademysql.md
Last active June 25, 2020 11:45
Downgrade mysql to mysql 5.6 on xenial

Install MySQL 5.6 in Ubuntu 16.04

Ubuntu 16.04 only provides packages for MySQL 5.7 which has a range of backwards compatibility issues with code written against older MySQL versions.

Oracle maintains a list of official APT repositories for MySQL 5.6, but those repositories do not yet support Ubuntu 16.04. However, the 15.10 repos will work for 16.04.

Uninstall existing mysql 5.7 if any

sudo apt remove mysql-client mysql-server libmysqlclient-dev mysql-common
@jberkus
jberkus / gist:e4cadd6b8877c3bc59c8
Created September 19, 2014 00:36
Duplicate Index Query #1: Exact Duplicates
-- check for exact matches
WITH index_cols_ord as (
SELECT attrelid, attnum, attname
FROM pg_attribute
JOIN pg_index ON indexrelid = attrelid
WHERE indkey[0] > 0
ORDER BY attrelid, attnum
),
index_col_list AS (
SELECT attrelid,
@jberkus
jberkus / gist:6bbffae5ce10fb399d29
Last active January 13, 2018 06:55
Duplicate Index Query #2: Partial matches
-- check for containment
-- i.e. index A contains index B
-- and both share the same first column
-- but they are NOT identical
WITH index_cols_ord as (
SELECT attrelid, attnum, attname
FROM pg_attribute
JOIN pg_index ON indexrelid = attrelid
WHERE indkey[0] > 0
@flarik
flarik / dot.powrc.sh
Created June 12, 2013 10:25
Pow's .porwrc config file for use with RVM's config files .rvmrc or .ruby-version (+ optional .ruby-gemset)
if [ -f "${rvm_path}/scripts/rvm" ]; then
source "${rvm_path}/scripts/rvm"
if [ -f ".rvmrc" ]; then
source ".rvmrc"
elif [ -f ".ruby-version" ] && [ -f ".ruby-gemset" ]; then
rvm use `cat .ruby-version`@`cat .ruby-gemset`
elif [ -f ".ruby-version" ]; then
rvm use `cat .ruby-version`
@asalant
asalant / setup.sh
Created June 7, 2012 20:41
Good Eggs dev workstation setup with chef soloist
# bash <(curl -s https://raw.github.com/gist/2891426)
read -p "Install Xcode (from the App store) and gcc for Lion (http://github.com/kennethreitz/osx-gcc-installer) and press enter to continue."
# setup your ssh keys for github
if ! [ -e "$HOME/.ssh/id_rsa.pub" ]
then
echo "Generating ssh key..."
read -p "Please enter the email you want to associate with your ssh key: " email
ssh-keygen -t rsa -C "$email"