Last active
April 28, 2017 07:50
-
-
Save lantip/ab8f61d4e12732c91ec3a42506d01212 to your computer and use it in GitHub Desktop.
script buat generate konversi table-table django ke utf8mb4. karena emoji itu penting. jalankan di environment django (python manage.py shell)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import connection | |
from django.conf import settings | |
cursor = connection.cursor() | |
cursor.execute("USE "+settings.DATABASES['default']['NAME']) | |
cursor.execute('SHOW TABLES') | |
tables = cursor | |
sql = '' | |
reverse_sql = '' | |
for (table_name,) in tables: | |
cursor.execute('SHOW fields in {}'.format(table_name)) | |
fields = cursor | |
for field in fields: | |
if 'varchar' in field[1] or 'text' in field[1]: | |
sql += "\n 'ALTER TABLE `{}` MODIFY `{}` {} CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci ',".format(table_name, field[0], field[1]) | |
reverse_sql += "\n 'ALTER TABLE `{}` MODIFY `{}` {} ',".format(table_name, field[0], field[1]) | |
print("migrations.RunSQL(\n" | |
" sql=[{}\n ],\n" | |
" reverse_sql=[{}\n ]\n" | |
"),".format(sql, reverse_sql)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment