Skip to content

Instantly share code, notes, and snippets.

@MaxGabriel
MaxGabriel / file.sql
Created June 2, 2023 19:56
Postgres database wiping in tests, only deleting from tables with rows, using triggers to track inserts
CREATE TABLE tables_with_insertions (
name TEXT PRIMARY KEY
);
CREATE OR REPLACE FUNCTION track_insert_for_tests()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO tables_with_insertions VALUES (TG_TABLE_NAME) ON CONFLICT ON CONSTRAINT tables_with_insertions_pkey DO NOTHING;
RETURN NEW;
@MaxGabriel
MaxGabriel / file.sql
Created August 18, 2022 19:34
Tables without primary keys
SELECT relname FROM pg_class pgc
JOIN pg_namespace pgns ON pgns.oid = pgc.relnamespace
WHERE pgns.nspname = 'public'
AND pgc.relkind = 'r'
AND pgc.oid NOT IN
(SELECT pgc.oid
FROM pg_class pgc
JOIN pg_index pgi ON pgi.indrelid = pgc.oid
JOIN pg_namespace pgns ON pgns.oid = pgc.relnamespace
@MaxGabriel
MaxGabriel / gist:eacdae1d5948d29f32fd4ed2263d5dd8
Created May 17, 2021 02:21
Table of Contents for Alexandrian Remix Fancy Props
Kalain's letter to Dalkhar
- page 118
- Four days after Dalakhar’s death, a letter arrives at the Inn of the Dripping Dagger for him.
- The late arrival of the letter is designed to push a clue to PCs after their initial visit: Those who leave their names with the owners of the Dripping Dagger, particularly those who specifically ask the owners to contact them if any new information crops up, will be rewarded with a proactive follow-up. (Alternatively, but probably less likely, it can reward PCs who follow-up on old leads.) Since the clue is non-essential for the current investigation, the slightly heightened risk of them missing the clue is offset by the benefit of adding depth to the game world: Little details like this make the players feel as if the game world is a fully functional, living environment that persists beyond their immediate line of sight. (Largely because that is, in fact, what you’re doing.)
LORD DAGULT’S PAPERS
- page 118
- Found in Kalain's Tower
- DOCUMENT LOCKBOX: A document lock
@MaxGabriel
MaxGabriel / DeriveLoadAllModels.hs
Created November 22, 2020 20:12
Template Haskell to load all Persistent models, stream them from the database, and validate they deserialize correctly
{-# LANGUAGE AllowAmbiguousTypes #-}
module Mercury.Database.Persist.DeriveLoadAllModels (mkLoadAllModels) where
import ClassyPrelude
import Control.Monad.Logger (MonadLogger, logInfoN)
import Data.Acquire (with)
import Data.Conduit (fuse, runConduit)
import qualified Data.Conduit.Combinators as Conduit
import qualified Data.Kind as K
@MaxGabriel
MaxGabriel / gist:167eadb8548286177ffbea95ca73757d
Created June 21, 2020 01:05
ghcid bug user error (could not create file system event stream)
This file has been truncated, but you can view the full file.
(130) ❄️ ~/D/M/h/mercury-web-backend> ghcid -o ghcid.txt --command 'cabal new-repl lib:mercury-web-backend' --allow-eval --warnings --verbose 20:58:35
%OS: darwin
%ARCH: x86_64
%VERSION: 0.7.6
Loading cabal new-repl lib:mercury-web-backend ...
%STDOUT: Build profile: -w ghc-8.6.5 -O0
%STDOUT2: Build profile: -w ghc-8.6.5 -O0
Build profile: -w ghc-8.6.5 -O0
%STDOUT: In order, the following will be built (use -v for more details):
%STDOUT2: In order, the following will be built (use -v for more details):
@MaxGabriel
MaxGabriel / gps-parser.hs
Created May 9, 2020 15:41
Degrees minutes seconds parser in haskell, for parsing e.g. 37 deg 8' 21.26" N, 80 deg 34' 41.84" W
latToDecimal :: DMSLat -> Double
latToDecimal (DMSLat dms direction) =
let decimal = dmsToDecimal
in case direction of
North -> decimal
South -> decimal * -1
lonToDecimal :: DMSLat -> Double
lonToDecimal (DMSLon dms direction) =
let decimal = dmsToDecimal

Motivation

Long compilation times are a challenge for large Haskell projects. After tricks like giving GHC more memory are exhausted, users have a few levers within their code to speed up compilation:

  1. Reducing the time it takes to compile a module (e.g. removing unnecessary deriving statements, deleting unused code, etc.)
  2. Improving parallelism, which currently is done at the module level.
  3. Improving caching via incremental builds (mostly unrelated to this feature request)

However, users don't have great insight into what modules are slow to compile, and where compilation is bottlenecked on fewer than the available CPU cores, leading to fruitless StackOverflow questions like this one: https://stackoverflow.com/questions/47444097/is-there-a-way-to-profile-compilation-time-per-module-with-ghc

master
Running 1 benchmarks...
Benchmark persistent-th-bench: RUNNING...
benchmarking mkPersist/From File
time 606.2 μs (578.8 μs .. 631.0 μs)
0.963 R² (0.939 R² .. 0.978 R²)
mean 942.0 μs (820.5 μs .. 1.092 ms)
std dev 529.6 μs (428.2 μs .. 641.2 μs)
variance introduced by outliers: 99% (severely inflated)
test/main.hs:(41,124)-(56,2): Splicing expression
template-haskell-2.14.0.0:Language.Haskell.TH.Quote.quoteExp
persistUpperCase
"\n\
\Person json\n\
\ name Text\n\
\ age Int Maybe\n\
\ foo Foo\n\
\ address Address\n\
\ deriving Show Eq\n\
@MaxGabriel
MaxGabriel / PULL_REQUEST_TEMPLATE.md
Created December 18, 2019 13:34
Mercury PR template

Testing Plan / How I Tested

  • Wrote a test
  • Tested locally (running SHA)
  • Tested on staging (running SHA)
  • Will test in production

Database