Skip to content

Instantly share code, notes, and snippets.

View mattlong's full-sized avatar
🌊

Matt Long mattlong

🌊
  • Medplum
  • San Francisco, CA
View GitHub Profile
@mattlong
mattlong / pg.sql
Last active May 7, 2025 21:56
Useful Postgres Queries
-- all connections
select pid, datname, usename, left(application_name, 15) as app_name, state, backend_start, xact_start, query_start, state_change, wait_event_type, wait_event, left(regexp_replace(query,E'[\\n\\r]+',' ','g'), 40) from pg_stat_activity WHERE pid<>pg_backend_pid() ORDER BY datname, usename;
-- active connections
SELECT application_name, state, age(now(), xact_start) age, query FROM pg_stat_activity WHERE state <> 'idle';
SELECT application_name, pid, state, age(now(), xact_start) age,
left(regexp_replace(query,E'[\\n\\r]+',' ','g'), 100)
FROM pg_stat_activity WHERE state <> 'idle' ORDER BY age;
@mattlong
mattlong / models.py
Last active June 27, 2017 23:27
Altering UUID column types
from __future__ import print_function
from sqlalchemy import (
Column,
create_engine,
MetaData,
)
from sqlalchemy.types import String, Integer
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
@mattlong
mattlong / gist:d5c97c3829a24a19dcef1e65e00c37c1
Created September 28, 2016 00:20
Purge git history of old files
# From http://stackoverflow.com/questions/17901588/new-repo-with-copied-history-of-only-currently-tracked-files
Delete everything and restore what you want
Rather than delete this-list-of-files one at a time, do the almost-opposite, delete everything and just restore the files you want to keep:
$ git checkout master
$ git ls-files > keep-these.txt
$ git filter-branch --force --index-filter \
"git rm --ignore-unmatch --cached -qr . ; \
@mattlong
mattlong / db.py
Created February 27, 2016 03:27 — forked from carljm/db.py
SQLAlchemy and Postgres autocommit
"""
SQLAlchemy, PostgreSQL (psycopg2), and autocommit
See blog post: http://oddbird.net/2014/06/14/sqlalchemy-postgres-autocommit/
"""
from contextlib import contextmanager
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker, Session as BaseSession
Remove osxfuse if installed via homebrew:
> brew uninstall osxfuse
Install osxfuse binary and choose to install the MacFUSE compatibility layer:
http://sourceforge.net/projects/osxfuse/files/latest/download?source=files
Reboot (optional but recommended by osxfuse)
Install ntfs-3g via homebrew:
> brew update && brew install ntfs-3g
@mattlong
mattlong / wrapper
Last active August 29, 2015 14:13
Production MySQL client wrapper
#!/bin/bash
cat << End-of-message
..........,...,.......,. .......,....,....................
..MMMMMM..MM....M..,MNMMMM...MM...MM.......MM..MMMMMMMM...
..MM......MM....M..MM....MI..MM..MM. ......MM.....MM..,..
..MMMMZ...MM....M..MM.. ...MMMMM.........MM.....MN...
..MM......MM. ..M..MM.. .....MM..MM........MM.....MN...
..MM:.....OM....Z..MM....MI..MM...MM.......MM.....MN...
..MM... ..,MM8......MMMMM...MM....MM......MM.....MN.

Keybase proof

I hereby claim:

  • I am mattlong on github.
  • I am mateolargo (https://keybase.io/mateolargo) on keybase.
  • I have a public key whose fingerprint is 2630 B827 4976 C128 FD90 8C08 0E97 4319 70DF A0F3

To claim this, I am signing this object:

@mattlong
mattlong / gist:9416571ace0db598577b
Last active August 29, 2015 14:07
foldLeft in terms of foldRight expansion
def foldRight[A,B](l: List[A], z: B)(f: (A, B) => B): B =
l match {
case Nil => z
case Cons(a, as) => f(a, foldRight(as, z)(f))
}
def foldLeft[A,B](l: List[A], z: B)(f: (B, A) => B): B =
foldRight(l, (b:B) => b)((a,g) => b => g(f(b,a)))(z)
@mattlong
mattlong / admin.py
Created September 17, 2014 18:26
Add a custom admin page for a model and link to it from the detail page
from functools import update_wrapper
from django.contrib import admin
from django.contrib.admin import ModelAdmin
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
from django.core.exceptions import PermissionDenied
from django.shortcuts import render
from myapp.models import Widget
from myapp.forms import ManageWidgetForm
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# Usage:
# Remove the .sh file extension when you put the script in your hooks folder!
#