Skip to content

Instantly share code, notes, and snippets.

@Jc2k
Jc2k / README.md
Last active July 22, 2024 21:26
Database rollback options in Django

Batching migrations

The idea here is that if you know which migrations were in version 2.0.3 of your project and which were in version 2.0.4 then setA - setB gives you the list of migrations you need to undo.

Django migrations give you a directed acyclic graph which describes how to get from the current database state to the target state. But there is no mechanism by which you can perform tasks like revert all the migrations that just ran in the last deployment.

Here is a quick recipe for batching Django migrations to allow you to do things like that.

Before you tag your project you do:

@Jc2k
Jc2k / ggrok.py
Created October 3, 2015 12:22
The start of an ngrok client for python. The goal was to share a django app natively, without even having local sockets to open.
from gevent.monkey import patch_all
patch_all()
import json
import socket
import struct
import ssl
import logging
import uuid
@Jc2k
Jc2k / process_query.py
Created August 26, 2015 11:15
A django orm-like PS
# https://raw.githubusercontent.com/pixelb/ps_mem/master/ps_mem.py
# https://people.gnome.org/~federico/misc/memstats.py
# http://brightbox.com/blog/2012/11/28/measuring-shared-ram-usage/
import itertools
import os
import operator
SUPPORTED_SMAPS_BUCKETS = [
'AnonHugePages', 'Anonymous', 'KernelPageSize', 'Locked',
@Jc2k
Jc2k / spawner.py
Created August 20, 2015 07:28
Looks like i started trying to make my own docker - Feb 2012
"""
This demo expects the following directory structure:
/bundle/ - empty directory
/library/
/app1/
RANDOM FILES
/app2/
RANDOM FILES
/empty/
@Jc2k
Jc2k / journald_stream.py
Created February 20, 2015 23:07
Streaming log lines from journald in python 3
import asyncio
from systemd import journal
class Journal(object):
def __init__(self, unit):
self.journal = journal.Reader()
# self.journal.log_level(journal.LOG_INFO)
self.journal.add_match(_SYSTEMD_UNIT=unit)
@Jc2k
Jc2k / debug.py
Created July 19, 2014 10:39
Call logging decorator (probably doesn't work, found it when having a spring clean)
import logger
log = logger.getLogger(__name__)
def log_calls(fn):
def inner(*args, **kwargs):
f = inspect.currentframe()
frame, filename, line, func_name, lines, index = inspect.getouterframes(f)[1]
args = inspect.getargvalues(f)
@Jc2k
Jc2k / gist:f5dce6d0e460e6295ef7
Created July 19, 2014 10:16
Crappy API invocation to turn a ticket into a pull request (in python)
import requests
import json
issue = 104
branch = "BRANCH_TO_MERGE"
username = "USERNAME"
repo = "REPO"
url = "https://api.github.com/repos/%(username)s/%(repo)s/pulls" % locals()
@Jc2k
Jc2k / keybase.md
Created July 8, 2014 13:32
I am me

Keybase proof

I hereby claim:

  • I am jc2k on github.
  • I am jc2k (https://keybase.io/jc2k) on keybase.
  • I have a public key whose fingerprint is A262 1F7E 9964 9468 FBE8 1555 EB27 AB01 4FB6 76F4

To claim this, I am signing this object:

@Jc2k
Jc2k / find_unpinned.py
Created May 21, 2014 16:09
Quick and dirty script to list unpinned dependencies in a requirements.txt
from pip.req import InstallRequirement, RequirementSet, parse_requirements
from pip.index import PackageFinder
import tempfile
import shutil
build_dir = tempfile.mkdtemp()
src_dir = tempfile.mkdtemp()
download_dir = tempfile.mkdtemp()
@Jc2k
Jc2k / inet_pton.py
Created March 11, 2014 16:49
inet_pton monkey patch for windows
import socket
if not hasattr(socket, "inet_pton"): # pragma: no cover
import ctypes
WSAStringToAddressA = ctypes.windll.ws2_32.WSAStringToAddressA
class sockaddr(ctypes.Structure):
_fields_ = [
("sa_family", ctypes.c_short),
("__pad1", ctypes.c_ushort),