Skip to content

Instantly share code, notes, and snippets.

@DmitryTsepelev
DmitryTsepelev / change_column_type.rb
Created August 27, 2020 08:46
How to change column type (e.g., int -> bigint) without downtime
ActiveRecord::Migration.remove_foreign_key(:current_table, :foreign_table) # no lock
ActiveRecord::Migration.add_column(:current_table, :column_bigint, :bigint) # no lock
copy_data = lambda do
CurrentTable.where(column_bigint: nil).where.not(column: nil).in_batches do |batch|
batch.update_all("column_bigint = column")
end
end
@zwolf
zwolf / pre-commit
Last active December 15, 2016 10:27
A pre-commit hook for git to alert if ":focus" (or similar) is found in a + line of a diff
#!/usr/bin/env ruby
if `git diff HEAD spec` =~ /^\+.*,\s?(:focus|focus:\s?true|:focus\s?=>\s?true)/
puts "\e[31mPlease focus and remove your :focus tags before committing :)"
exit 1
end
@parrish
parrish / pg_locks.sql
Created June 30, 2016 14:33
Show info on postgres locks
SELECT bl.pid AS blocked_pid,
a.usename AS blocked_user,
ka.query AS current_statement_in_blocking_process,
now() - ka.query_start AS blocking_duration,
kl.pid AS blocking_pid,
ka.usename AS blocking_user,
a.query AS blocked_statement,
now() - a.query_start AS blocked_duration
FROM pg_catalog.pg_locks bl
JOIN pg_catalog.pg_stat_activity a ON a.pid = bl.pid
@parrish
parrish / pre-commit
Last active August 29, 2015 14:03
Git whitespace pre-commit hook
#!/bin/sh
# http://stackoverflow.com/a/6262715
# .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
# A git hook script to find and fix trailing whitespace
# in your commits. Bypass it with the --no-verify option
# to git-commit
#