Skip to content

Instantly share code, notes, and snippets.

@piotch
Last active August 11, 2017 09:12
Show Gist options
  • Save piotch/43114872b0e03e63e34b to your computer and use it in GitHub Desktop.
Save piotch/43114872b0e03e63e34b to your computer and use it in GitHub Desktop.
South migration to convert tables to utf-8
from django.db.models import get_app, get_models
from django.conf import settings
from south.db import db
from south.v2 import SchemaMigration
class Migration(SchemaMigration):
"""
This is a generic migration to convert tables and column from latin-1 to utf-8
To use it create an empty migration:
$ python manage.py schemamigration app_name migration_name --empty
Then replace the forward and backward methods with this class mehtods.
"""
# This application tables
tables = [model._meta.db_table for model in get_models(get_app(settings.RESOURCE_NAME))]
# To do the conversion on all the DB tables
# from django.db import connection
# cursor = connection.cursor()
# tables = connection.introspection.table_names()
def forwards(self, orm):
for table in self.tables:
db.execute("ALTER TABLE %s CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;" % table)
def backwards(self, orm):
for table in self.tables:
db.execute("ALTER TABLE %s CONVERT TO CHARACTER SET latin1 COLLATE latin1_swedish_ci;" % table)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment