Skip to content

Instantly share code, notes, and snippets.

View begriffs's full-sized avatar

Joe Nelson begriffs

View GitHub Profile
@begriffs
begriffs / already-in-progress.hs
Last active January 22, 2016 07:02
Illustrating the hasql error
import Control.Monad
import Data.Int
import Data.Monoid
import Data.Functor.Contravariant
import Hasql.Connection
import Hasql.Session
import Hasql.Query
import qualified Hasql.Decoders as HD
import qualified Hasql.Encoders as HE
@begriffs
begriffs / funcs.txt
Created January 13, 2016 16:49
Which functions use which other
createReadStatement
asCsvF
asJsonF
asJsonSingleF
limitF
locationF
createWriteStatement
asCsvF
asJsonF
@begriffs
begriffs / readqueries.sql
Last active January 13, 2016 00:03
Example read queries generated by postgrest
-- JSON FESTIVAL
WITH pg_source AS
(SELECT "public"."festival".* FROM "public"."festival")
SELECT
(SELECT pg_catalog.Count(1) FROM "public"."festival") AS total_result_set,
pg_catalog.Count(t) AS page_total,
NULL AS header,
Array_to_json(Array_agg(Row_to_json(t)))::character VARYING AS body
FROM
@begriffs
begriffs / testing_gt_with_scheme.rkt
Last active January 9, 2016 23:46
Using quickcheck in scheme to test a function
#lang racket
(require quickcheck)
(define my_gt
(lambda (n m)
(cond
((zero? n) #f)
((zero? m) #t)
(else (my_gt (sub1 n) (sub1 m))))))
@begriffs
begriffs / desks-unavailable.sh
Created January 8, 2016 22:13
Which standing desks at Workshop Cafe are currently unavailable?
curl -s https://app.workshopcafe.com/seats/taken_and_unreservable_seats | json | grep \\b7[4-6]

Answers to objections about putting logic in the db

  • The DB lacks version control, and a team cannot collaborate. There is no consistent way to evolve the schema.
    • Keep all db code in version control as test-driven migrations
    • Don't change the db except through migrations
    • Use a tool like Sqitch to apply the migrations and keep track of migration history using an auxiliary set of tables in the db
    • Write tests for views and functions with plpgunit
    • Deployment is easier because changing a backend means changing the database. Just point sqitch at the staging/production db and apply the migration(s) same as you would locally.
  • Central computation in a database can't scale
    • Start with RDS which allows you to scale pretty far
@begriffs
begriffs / crash.log
Created December 28, 2015 03:34
postgrest patch crash logs
2015-12-28T03:32:53.153150+00:00 heroku[router]: at=info method=OPTIONS path="/speakers?id=eq.140" host=postgrest.herokuapp.com request_id=dcdd0fb5-98f3-45ae-a769-4d6be6a4b6c8 fwd="73.233.161.119" dyno=web.1 connect=1ms service=1ms status=200 bytes=427
2015-12-28T03:32:53.144669+00:00 heroku[router]: at=info method=OPTIONS path="/speakers?id=eq.140" host=postgrest.herokuapp.com request_id=bfd29fc2-942e-456a-aba1-74346dbb1a23 fwd="73.233.161.119" dyno=web.1 connect=1ms service=16ms status=200 bytes=427
2015-12-28T03:32:53.132313+00:00 heroku[router]: at=info method=OPTIONS path="/speakers?id=eq.140" host=postgrest.herokuapp.com request_id=2121268b-397c-4fe9-90e9-b842c1e2c668 fwd="73.233.161.119" dyno=web.1 connect=1ms service=1ms status=200 bytes=427
2015-12-28T03:32:53.133157+00:00 app[web.1]: 10.180.57.199 - - [28/Dec/2015:03:32:53 +0000] "OPTIONS /speakers HTTP/1.1" 200 - "http://timwis.com/handsontable-postgrest/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrom
@begriffs
begriffs / primesTo.hs
Created December 23, 2015 08:12
Calculating list of primes less than n
primesTo :: Int -> [Int]
primesTo n = removeHeadMultiples [2..n]
where
removeHeadMultiples same@(p:xs)
| p*p > n = same -- short circuit factors greater than sqrt(n)
| otherwise = p : removeHeadMultiples [x | x <- xs, x `rem` p > 0]
@begriffs
begriffs / draft.raml
Created December 3, 2015 20:34
beginning attempt at describing api in raml
#%RAML 1.0
title: PostgREST Demo
version: v1
baseUri: https://postgrest.herokuapp.com/
types:
Sessions:
type: object
properties:
id: integer
speaker_id: integer
@begriffs
begriffs / postgrest.prof
Created November 22, 2015 23:31
Profiling info for posting big arrays
Sun Nov 22 15:25 2015 Time and Allocation Profiling Report (Final)
postgrest +RTS -p -L500 -RTS -- postgresql://postgrest_test:@localhost:5432/postgrest_test -a postgrest_test -s test
total time = 8.31 secs (8311 ticks @ 1000 us, 1 processor)
total alloc = 40,138,127,800 bytes (excludes profiling overheads)
COST CENTRE MODULE %time %alloc
MAIN MAIN 95.9 98.4