Skip to content

Instantly share code, notes, and snippets.

View dhermes's full-sized avatar

Danny Hermes dhermes

View GitHub Profile
@dhermes
dhermes / sizes.sql
Created September 30, 2022 14:43
[2022-09-30] Query to summarize PostgreSQL table size across all (specified) schemas
WITH raw_relation_sizes AS (
SELECT
n.nspname || '.' || c.relname as table_name,
pg_relation_size(n.nspname || '.' || c.relname) AS relation_size
FROM
pg_catalog.pg_class AS c
LEFT OUTER JOIN
pg_catalog.pg_namespace AS n
ON
n.oid = c.relnamespace
@dhermes
dhermes / main.go
Created September 9, 2022 15:42
[2022-09-09] Struct embedding a Go interface (e.g. for unit tests)
package main
import (
"fmt"
)
type Interface interface {
Foo() int
Bar() string
}
@dhermes
dhermes / main.go
Created September 9, 2022 15:42
[2022-09-09] Some examples of Go struct embedding
package main
import (
"fmt"
"math"
)
type EmbeddedType struct {
X int
Y int
@dhermes
dhermes / main.go
Created July 21, 2022 18:21
[2022-07-21] Quirks of JSON unmarshal onto `any` in Go
package main
import (
"encoding/json"
"fmt"
)
type T struct {
X float64 `json:"x"`
Y float64 `json:"y"`
@dhermes
dhermes / event_names.json
Created July 13, 2022 19:24
[2022-07-13] Stripe Event Names
{
"source": "https://stripe.com/docs/api/events/types",
"length": 194,
"names": [
"account.updated",
"account.application.authorized",
"account.application.deauthorized",
"account.external_account.created",
"account.external_account.deleted",
"account.external_account.updated",
@dhermes
dhermes / README.md
Last active July 8, 2022 01:45
[2022-07-07] FYI, nanoid is lossy

FYI, nanoid is lossy

I was scratching my head trying to understand where some of the bits went in a Nano ID. A Nano ID is 16 characters from the [Crockford base 32 alphabet][1]. Since 2**5 == 32, this means each character comes with 5 bits, for a total of 80 bits (5 * 16).

However a UUID has 128 bits, so 3 out of every 8 bits are missing.

@dhermes
dhermes / README.md
Last active July 6, 2022 18:26
[2022-07-06] Use filenames to generate a sequence

Use filenames to generate a sequence

Happy Path

$ mkdir files/
$ touch files/j.sql files/j-to-a.sql files/a-to-z.sql files/z-to-c.sql
$
$
$ go run ./main.go --target ./files/
@dhermes
dhermes / README.md
Created July 5, 2022 14:57
[2022-07-05] PostgreSQL Soft Deletes and Foreign Keys

PostgreSQL Soft Deletes and Foreign Keys

Showing a simple example for how to preserve referential integrity in the presence of soft deletes. Here we'll implement soft deletion by setting a nullable deleted_at column.

toolshop=> CREATE SCHEMA foo;
CREATE SCHEMA
toolshop=> CREATE TABLE foo.bar (
@dhermes
dhermes / README.md
Last active March 22, 2022 19:10
[2022-03-22] Concurrent but not really

Concurrent but not really

An experiment in serializing work items spawned concurrently in goroutines.

$ go run ./main.go
 Enter 00-03: 2022-03-22 18:55:16.7311542 +0000 UTC
Middle 00-03: 2022-03-22 18:55:16.7312627 +0000 UTC
 Enter 03-02: 2022-03-22 18:55:16.7312777 +0000 UTC
@dhermes
dhermes / .gitignore
Last active March 8, 2022 15:57
[2022-03-08] Messing Around with Go WASM
/main.wasm