Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created November 8, 2012 10:55
Show Gist options
  • Select an option

  • Save revolunet/4038155 to your computer and use it in GitHub Desktop.

Select an option

Save revolunet/4038155 to your computer and use it in GitHub Desktop.
Django DB migration
#
# Migration from some DB to Another (Sqlite->Mysql)
# The DB_NAMES are from the django settings.py
#
# if something goes wrong, change the log level to DEBUG for django.db in your settings.py logging conf
#
# first, remove useless tables from the existing DB
from django.db import connection
current_tables = connection.introspection.table_names()
needed_tables = connection.introspection.django_table_names()
useless_tables = [table for table in current_tables if table not in needed_tables]
cursor = connection.cursor()
for table in useless_tables:
cursor.execute('DROP TABLE %s;' % table)
# create the fixture
python manage.py dumpdata --all --natural --database OLD_DB_NAME --exclude=contenttypes --exclude=auth.permission --exclude=admin.logentry > dump_sqlite.json
# create the new DB structure
python manage.py syncdb --all
# update your structures if needed (eg: username maxlength) -> should be in south
# from django.db import models
# from south.db import db
# db.alter_column('auth_user', 'username', models.CharField(max_length=75))
# load data into the new DB
python manage.py loaddata --database NEW_DB_NAME dump_sqlite.json
# you should have a brand new DB :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment