Skip to content

Instantly share code, notes, and snippets.

In my application every time I send a newsletter to a User, I create a NewsletterDelivery record. I frequently want to be able to query, for each user, what is the most recent newsletter delivery record. This is called a "last n per group" query and a LATERAL JOIN is the best way to do it imo. But the query I've been using (and I've told people to use, and seen blogged about) is not very good, because the conditions don't get pushed down into the subselect which means that the query ends-up lateral-joining all the records before it applies the conditions for the association.

Instead of doing subselect_table.* the better query does association.id AS assocation_id, subselect_table.id, subselect_table.title, .... and enumerates over all of the columns. This allows the association query, which Active Record tacks on at the end as WHERE association_id = $1 or WHERE association_id IN ($1, $2, $3, ...) to be pushed down c

.ONESHELL:
test: .SHELLFLAGS := -i
test: SHELL := bb
test:
(println :wow)
(require '[clojure.string :as s])
(s/reverse (slurp "./Makefile"))
@cldwalker
cldwalker / spike-day-02-03-20.md
Last active June 19, 2024 04:24
GraalVM dive in Clojure at work

Spike

I looked into the state of GraalVM and Clojure and wrote some small work-related scripts.

GraalVM Build Tools

@stevenharman
stevenharman / 00_Heroku-Release-Phase-Review-Apps-Rails_README.md
Last active May 23, 2024 04:43
Heroku Release Phase script for managing Rails DB migrations, and playing nice with Review Apps and postdeploy scripts

Heroku Release Phase + Review Apps + Rails

This is a simplified, but fairly thorough, set of scripts and configuration to enable Heroku Release Phase for Rails apps. Further, this particular set up plays nicely with Heroku Review Apps in that the release phase script will:

  1. Fail, loudly, if the DB does not yet exist.
  2. Load the DB schema if the current schema version (as determined by bin/rails db:version) is 0.
  3. Run DB migrations otherwise.

For a "normal" app that usually means it will run the DB migrations.

@lauraturk
lauraturk / circleci-heroku-continuous-deployment2.0.md
Last active November 23, 2021 15:28
instructions for deploying from circleci2.0 to heroku
@yogthos
yogthos / gallery.cljs
Last active March 30, 2024 17:36
script to download walpapers from windowsonearth.org
@jdevoo
jdevoo / tg2p.st
Last active May 27, 2024 21:48
Not so Terse Guide to Pharo
"**************************************************************************
* Allowable characters: *
* - a-z *
* - A-Z *
* - 0-9 *
* - .+/\*~<>@%|&? *
* - blank, tab, cr, ff, lf *
* *
* Variables: *
* - variables must be declared before use *
@ghadishayban
ghadishayban / docker_tag_paginate.clj
Last active December 21, 2016 03:48
unfold example
(ns docker-tag-paginate
(:require [clj-http.lite.client :as client]
[cheshire.core :as json]
[clojure.pprint :refer [print-table]]))
(def base-url "https://registry.hub.docker.com/v2")
;; (progression pos? dec 15)
(defn progression
@favila
favila / restore_datoms.clj
Last active September 30, 2021 03:50
Restore a datomic database "manually", i.e. from raw datoms. Useful for memory databases. Context: https://groups.google.com/d/msg/datomic/BkTdKYB3WpE/AKfqKYqPONMJ
(ns favila.datomic-util.restore-datoms
"A \"manual\" datomic database restore.
Writes raw datoms (stored in a stream written by d/datoms) to an empty database.
Useful for memory databases: you can write out all the datoms in it, then read
them into another database. (Note mem dbs have no log or retractions)."
(:require [datomic.api :as d]
[clojure.edn :as edn]))
(defrecord datom [e a v tx added?])
@stuarthalloway
stuarthalloway / Nil Finder
Created January 15, 2015 20:51
Finding those nested nils
(ns user)
(def app
"Intenal Helper"
(fnil conj []))
(defprotocol PathSeq
(path-seq* [form path] "Helper for path-seq"))
(extend-protocol PathSeq