Skip to content

Instantly share code, notes, and snippets.

@loisaidasam
Created February 19, 2014 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loisaidasam/9099383 to your computer and use it in GitHub Desktop.
Save loisaidasam/9099383 to your computer and use it in GitHub Desktop.
A template for adding indexes via a South migration
# -*- coding: utf-8 -*-
import copy
from south.db import db
from south.v2 import SchemaMigration
###
# CHANGE THESE VALUES TO FIT YOUR PROJECT
INDEXES = (
('table_name', 'column_name', 'index_name'),
('other_table_name', ('column_one', 'column_two'), 'other_index_name'),
)
PREVIOUS_MIGRATION = 'api.migrations.0001_initial'
#
###
previous_migration = __import__(PREVIOUS_MIGRATION, globals(), locals(), ['Migration'], -1)
class Migration(SchemaMigration):
"""This migration is for adding some indexes to some columns
References:
http://www.postgresql.org/docs/9.1/static/sql-createindex.html
http://www.postgresql.org/docs/9.2/static/sql-dropindex.html
"""
def forwards(self, orm):
for index in INDEXES:
table, columns, index_name = index
if not isinstance(columns, basestring):
columns = '(' + ', '.join(columns) + ')'
query = "CREATE INDEX %s ON %s (%s) ;" % (index_name,
table,
column)
db.execute(query)
def backwards(self, orm):
for index in INDEXES:
table, columns, index_name = index
query = "DROP INDEX %s ;" % index_name
db.execute(query)
models = copy.deepcopy(previous_migration.Migration.models)
complete_apps = copy.deepcopy(previous_migration.Migration.complete_apps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment