Skip to content

Instantly share code, notes, and snippets.

@smcoll
smcoll / 0002_merge_tables.py
Created August 7, 2018 15:30
Django migration to merge one table's records into another similar table (both having UUID primary keys)
from django.db import migrations
def merge_foo_into_bar(apps, schema_editor):
""" For each FooCategory, create a corresponding BarCategory (with the new type)
and for each Foo, create a corresponding Bar.
The idea is that Foo and FooCategory would be dropped in a subsequent migration.
"""
Foo = apps.get_model('myapp', 'Foo')
@smcoll
smcoll / indexes.py
Created July 25, 2018 21:53
Django GIN index from pg_tgrm extension.
from django.contrib.postgres.indexes import GinIndex
class GinTrgmIndex(GinIndex):
""" GIN index from the pg_trgm extension.
NOTE: before adding this index, be sure to have the `pg_trgm` extension installed. This can be
done by including (before any GinTrgmIndex indexs in model meta) the migration operation
`django.contrib.postgres.operations.TrigramExtension`
https://vxlabs.com/2018/01/31/creating-a-django-migration-for-a-gist-gin-index-with-a-special-index-operator/
"""
@smcoll
smcoll / base.py
Created July 25, 2018 21:51
Django Postgres database wrapper with `pg_trgm` support.
from django.db.backends.postgresql_psycopg2 import base
class DatabaseWrapper(base.DatabaseWrapper):
""" Postgres database backend with support for operations using trigram indexes, inspired by
https://stackoverflow.com/a/44962928
Put this file in a new module, and point to that module in `settings.DATABASES[<name>]['ENGINE']
"""
@smcoll
smcoll / test.py
Created July 19, 2017 15:07
Django test runner which installs HSTORE extension
class TestRunner(runner.DiscoverRunner):
""" When migrations are disabled for the test runner, the `pre_migrate` signal
does not emit. So we need another hook for installing the extension. Prior to
Django 1.9, the `pre_syncdb` signal worked for that.
"""
def setup_databases(self, **kwargs):
"""
Always create PostgreSQL HSTORE extension if it doesn't already exist
on the database before syncing the database. Requires PostgreSQL >= 9.1
@smcoll
smcoll / 000x_convert_foo_to_polymorphic_child.py
Last active March 24, 2017 22:29
Django migration for converting model to django-polymorphic child model
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
from django.db.models import F
def seed_baseitem(apps, schema_editor):
""" For each Foo, create a BaseItem with the same primary key value and
the appropriate `polymorphic_ctype` value. This will be used by a new
@smcoll
smcoll / test_schema.py
Created February 10, 2017 20:39
Django test to detect uncreated/unmerged migration files
from StringIO import StringIO
from django.core.management import call_command
from django.test import TestCase, override_settings
class TestMigrationDiscrepancies(TestCase):
@override_settings(MIGRATION_MODULES={})
def test_for_missing_migrations(self):
@smcoll
smcoll / test_deploy_check.py
Last active August 4, 2016 18:38
test for output of `manage.py check --deploy`
import sys
from subprocess import check_call
from tempfile import TemporaryFile
from django.test import TestCase
class SecurityHealthCheck(TestCase):
""" Run the deploy check on each settings file for a live deployment. If there are any warnings, fail and report.
@smcoll
smcoll / django_migration_pk_to_uuid.py
Last active August 6, 2024 20:14
Django migration converting integer pk table to UUID pk table
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import uuid
from django.db import migrations, models
def fill_mymodel_uuid(apps, schema_editor):
db_alias = schema_editor.connection.alias
MyModel = apps.get_model('myapp', 'MyModel')
@smcoll
smcoll / fields.py
Created January 13, 2014 18:55
Django ModelField for timezone-naive datetimes
import datetime
from django.core import exceptions
from django.db.models import DateTimeField
from django.utils.dateparse import parse_date, parse_datetime
from django.utils.translation import ugettext_lazy as _
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^myproject\.myapp\.fields\.NaiveDateTimeField"])
@smcoll
smcoll / gist:1849267
Created February 17, 2012 00:45
more flexible DocumentingTemplateRenderer
class DocumentingTemplateRenderer:
...
escape_binary = False
def _escape_binary(self, content):
if self.escape_binary and not all(char in string.printable for char in content):
return '[%d bytes of binary content]' % len(content)
return content