Skip to content

Instantly share code, notes, and snippets.

View cluesque's full-sized avatar

Bill Kirtley cluesque

View GitHub Profile
@cluesque
cluesque / tables_approaching_limit.sql
Created February 14, 2024 23:36
Postgres: Report how close each table primary key is to its limit
WITH column_info AS (
SELECT t.relname AS table_name, c.attname AS column_name, c.atttypid, c.attlen AS length, y.typname AS type
, pg_get_expr(adbin, adrelid) AS default
, (regexp_matches(pg_get_expr(adbin, adrelid), 'nextval..(.*).::regclass'))[1] AS sequencename
FROM pg_class t
LEFT OUTER JOIN pg_attribute c ON c.attrelid = t.oid
LEFT OUTER JOIN pg_type y ON y.oid = c.atttypid
LEFT OUTER JOIN pg_attrdef d ON t.oid = d.adrelid AND d.adnum = c.attnum
WHERE pg_get_expr(adbin, adrelid) LIKE '%nextval%'
),
@cluesque
cluesque / checker.rb
Created August 3, 2023 16:44
Liveness monitor for ruby programs
# Have your health checker execute this
require './liveness_monitor'
puts LivenessMonitor.new.status
@cluesque
cluesque / progress_regressor.rb
Created December 1, 2022 19:13
ProgressRegressor class
# Useful for tracking the progress of a large batch job
# Pass in 'counter', a lambda that returns an integer
# This will periodically call the counter,
# track the rate counted things are consumed (or produced)
# and make a prediction about when it completes
# options:
# - divisor: how many times per minute to sample, default 6 (every ten seconds)
# - rolling_window: how many rate samples to average when making predictions (default 10)
# - target: what value are we aiming for, default 0 (consuming a queue)
@cluesque
cluesque / Gemfile
Created January 14, 2021 21:00
For rails console pry is a much better REPL than IRB, so why not use it in production?
# This goes at global scope (remove it from your other groups)
gem 'pry-rails', require: false
gem 'pry-byebug', require: false

Keybase proof

I hereby claim:

  • I am cluesque on github.
  • I am cluesque (https://keybase.io/cluesque) on keybase.
  • I have a public key ASBvxQlEpGPq_kkRyixFNNvyPL7H1ag6fwoz4Dmb40Nsrgo

To claim this, I am signing this object:

@cluesque
cluesque / _Suppressible Notes
Last active March 5, 2018 17:26
Suppressible mixin
DHH's On Writing Software (well?) [episode 2](https://www.youtube.com/watch?v=m1jOWu7woKM) discussed callbacks, specifically the practice of hiding side effects of object creation using `after_save` and `after_commit` hooks.
It implicitly conceeded that `after_commit`, lacking information about what was just committed, was flawed, and uses the common workaround of using an `after_save` to decide to do something based on changes and set an @ivar, and then in the `after_commit` acting on the @ivar found. Which is fun, but.
It also [at the end](https://youtu.be/m1jOWu7woKM?t=1151) discussed the possibility that in some use cases a caller might want to explicitly prevent side effects from happening. Think about when audited temporarily suppresses auditing, or perhaps one might want to bulk update some data, without sending the email normally sent when said data is updated.
It teased the presence of a `Suppressible` mixin. For fun, I wrote it.
Update: turns out the real one is public. It uses `thread_mattr
@cluesque
cluesque / genesis_public_key
Created February 22, 2018 04:08
genesis_public_key
04ac10d9df868268f354c546fb71262375a7fa654841cebc1ce5e3269d2f69de9c614b2235c2311d23e26f55a5c38601e240f0168c8ecc7255b2e940f50fa783b8
@cluesque
cluesque / cryptor.rb
Created December 20, 2016 03:18
Toy class demonstrating encryption and decryption with aes-256-gcm
class Cryptor
# Usage:
# cipher = Cryptor.new(key, auth_data: '1234').encrypt(clear)
# clear = Cryptor.new(key, auth_data: '12344').decrypt(cipher)
attr_accessor :key, :auth_data
# The auth_data should be something different for each text, perhaps a database row id?
def initialize(key, auth_data: 'aad')
self.mode = mode
self.key = key
@cluesque
cluesque / shoulda_context_test.rb
Created May 28, 2015 17:43
Macros for adding setup and teardown to a context don't do what I think they should
require 'test_helper'
class ShouldaContextTest < ActiveSupport::TestCase
def self.setup_setup_helper
setup { puts "running the setup helper"; @wrapped = true }
teardown{ puts "running the teardown helper"; @wrapped = false }
end
context "outer nesting" do
context "without setup" do
setup do
@cluesque
cluesque / database.yml
Created October 28, 2014 15:09
The config/database.yml that heroku installs
<%
require 'cgi'
require 'uri'
begin
uri = URI.parse(ENV["DATABASE_URL"])
rescue URI::InvalidURIError
raise "Invalid DATABASE_URL"
end