Skip to content

Instantly share code, notes, and snippets.

@gregplaysguitar
Created November 10, 2010 21:50
Show Gist options
  • Save gregplaysguitar/671593 to your computer and use it in GitHub Desktop.
Save gregplaysguitar/671593 to your computer and use it in GitHub Desktop.
The following is a simple django custom management command to handle model field additions and deletions on a development machine. It automates a common pattern in django development – that of renaming an existing table, recreating via syncdb, and inserti

Simple django management command to automate the process of renaming an existing table, recreating via syncdb, and inserting the old data into the new table. Currently does not handle field renames - will eat your data if you try.

To install, save this file into an installed django app's management/commands/ directory, eg

myapp/management/commands/simplemigration.py

Run the command from the command line as follows

python manage.py simplemigration APPNAME.MODELNAME
from django.core.management.base import BaseCommand, CommandError
from django.db import models
from django.core.management import call_command
from django.db import connection, transaction
class Command(BaseCommand):
def handle(self, *model_labels, **options):
for label in model_labels:
m = models.get_model(*label.split('.'))
temp_name = '_' + m._meta.db_table
try:
# rename table, then recreate
cursor = connection.cursor()
sql = 'ALTER TABLE %s RENAME TO %s' % (m._meta.db_table, temp_name)
cursor.execute(sql)
print sql
call_command('syncdb')
cursor = connection.cursor()
old_fields = [f[0] for f in connection.introspection.get_table_description(cursor, temp_name)]
new_fields = [f[0] for f in connection.introspection.get_table_description(cursor, m._meta.db_table)]
insert_fields = [f if f in old_fields else '\'\'' for f in new_fields]
# insert data from temp table into new
sql = 'INSERT INTO %s SELECT %s FROM %s' % (m._meta.db_table, ', '.join(insert_fields), temp_name)
cursor.execute(sql)
print sql
# drop temporary table
sql = 'DROP TABLE %s' % (temp_name)
cursor.execute(sql)
print sql
except Exception, e:
print 'Last SQL statement: ' + sql
transaction.rollback_unless_managed()
raise
else:
transaction.commit_unless_managed()
@gregplaysguitar
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment