Skip to content

Instantly share code, notes, and snippets.

View colophonemes's full-sized avatar

Sam Deere colophonemes

View GitHub Profile
@colophonemes
colophonemes / README.md
Last active March 8, 2024 01:50
Postgres Distributed Key Generation

Postgres Distributed Key Generation

This SQL creates a Postgres function to generate sequential, numeric, unique IDs in a consistent format across services. Useful for database sharding or microservices.

Draws heavily on Instagram's ID generator, via Rob Conery, with minor modifications.

The main changes are that the unique number resolution is per-second rather than per-millisecond. This is to reduce key size below 2^53^-1 so that generated IDs that are under Javascripts Number.MAX_SAFE_INTEGER limit . This is important if you're using these on a Node.js server (e.g. our use case is an Express API using Hashids).

Max IDs are in the order of 51 bits, broken down as follows:

  • 31 bits for the timestamp difference
@colophonemes
colophonemes / README.md
Created January 5, 2017 04:58
Postgres auto-updating timestamps

Auto-updating timestamps in Postgres

A Postgres function to change the updated_at column of a record on INSERT/UPDATE

@colophonemes
colophonemes / create_triggers
Last active September 6, 2024 18:00
Postgres TRIGGER to call NOTIFY with a JSON payload
CREATE TRIGGER person_notify AFTER INSERT OR UPDATE OR DELETE ON income
FOR EACH ROW EXECUTE PROCEDURE notify_trigger(
'id',
'email',
'username'
);
CREATE TRIGGER income_notify AFTER INSERT OR UPDATE OR DELETE ON income
FOR EACH ROW EXECUTE PROCEDURE notify_trigger(
'id',
const {allResponses} = inputData
const responsesRaw = allResponses
.split('###')
.filter(r => r)
.map(r => r.trim())
const responses = {}
for (let responseRaw of responsesRaw) {
// split the question and answer at the first linebreak
#!/bin/bash
ASANA_ACCESS_TOKEN=<YOUR_ACCESS_TOKEN>
ASANA_PROJECT_GID=<YOUR_PROJECT_GID>
ASANA_CUSTOM_FIELD_GID=<YOUR_CUSTOM_FIELD_GID>
ASANA_WORKSPACE_GID=<YOUR_WORKSPACE_GID>
# relies on csv2json and jq
# csv2json: yarn global add csv2json
# jq: https://github.com/stedolan/jq
CSVDATA=$(csv2json -d "$1")
@colophonemes
colophonemes / example.sql
Created August 14, 2020 12:33
Find all tables referenced by a foreign key (including schema and referencing column)
/*
Function to merge two people into a single person
The primary person (referenced by primary_person_id) will be retained, the secondary person
will have all their records re-referenced to the primary person, and then the secondary person
will be deleted
Note that this function may be destructive! For most tables, the records will simply be merged,
but in cases where merging would violate a UNIQUE or EXCLUSION constraint, the secondary person's
respective records will be dropped. For example, people cannot have overlapping pledges (on the
@colophonemes
colophonemes / .zshrc
Created August 23, 2021 15:04
Dotfiles
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

Keybase proof

I hereby claim:

  • I am colophonemes on github.
  • I am colophonemes (https://keybase.io/colophonemes) on keybase.
  • I have a public key whose fingerprint is 14CF 549B 7C2D 5E39 7995 8795 5C57 56E6 C573 B55A

To claim this, I am signing this object: