Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am almightycouch on github.
  • I am mflach (https://keybase.io/mflach) on keybase.
  • I have a public key ASBABULfJCzh1RDXC4CDxU_X7vCduitjDW0Szu0ygpOr2Qo

To claim this, I am signing this object:

@almightycouch
almightycouch / tester.exs
Created July 19, 2016 20:41
RethinkDB.Connection one producer, three consumers
import RethinkDB.Query
require Logger
{:ok, pid} = RethinkDB.Connection.start_link
for i <- 1..3 do
spawn_link fn ->
Logger.debug "#{i} started"
{:ok, feed} = table("users")
@almightycouch
almightycouch / bucket.ex
Created January 14, 2016 21:41
Elixir Bucket module implementing the Access behaviour
defmodule Bucket do
@behaviour Access
defstruct pid: nil
def start_link(default \\ %{}) when is_map(default) do
case Agent.start_link(fn -> default end) do
{:ok, pid} ->%__MODULE__{pid: pid}
{:error, message} -> raise message
end
@almightycouch
almightycouch / update_or_create.py
Last active November 18, 2015 17:20
update_or_create() with support for foreign keys, m2m fields
from django.db import transaction
@transaction.atomic
def update_or_create(model, **kwargs):
"""
Looks up an object with the given kwargs, updating one if it exists,
otherwise creates a new one.
This implementation differs from Django `update_or_create()` function.
@almightycouch
almightycouch / psql_changefeed.sql
Last active August 16, 2019 07:56
PostgreSQL changefeed using NOTIFY
CREATE FUNCTION notify_changes() RETURNS trigger AS $$
DECLARE
BEGIN
IF TG_OP = 'INSERT' THEN
PERFORM pg_notify('insert', json_build_object('table', TG_TABLE_NAME, 'new_val', row_to_json(NEW))#>>'{}');
RETURN NEW;
ELSIF TG_OP = 'UPDATE' THEN
PERFORM pg_notify('update', json_build_object('table', TG_TABLE_NAME, 'new_val', row_to_json(NEW), 'old_val', row_to_json(OLD))#>>'{}');
RETURN NEW;
ELSIF TG_OP = 'DELETE' THEN
@almightycouch
almightycouch / uiki-install.sh
Last active October 24, 2015 10:16
UIkit LESS installer
#!/usr/bin/env bash
curl -L https://github.com/uikit/uikit/archive/master.tar.gz | tar xz
cp -r uikit-master/src/less/ uikit
cp -r uikit-master/themes/ uikit/themes
rm -rf uikit-master
sed -i '' -e 's/src\/less\///g' uikit/themes/*/uikit*.less