fix migration on mysql (#531)

Co-authored-by: Iurchenko Sergei <empty>
This commit is contained in:
Sergei Iurchenko 2023-10-16 17:14:40 +03:00 committed by GitHub
parent bc9707d618
commit d03bea804f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,10 @@
from logging import getLogger
from django.core.management.color import no_style
from django.db import migrations, connection, DatabaseError
from django.db import migrations, DatabaseError
logger = getLogger(__name__)
def _migrate_from_old_table(apps, schema_editor) -> None:
@ -7,13 +12,14 @@ def _migrate_from_old_table(apps, schema_editor) -> None:
Copies values from old table.
On new installations just ignore error that table does not exist.
"""
connection = schema_editor.connection
quoted_string = ', '.join([connection.ops.quote_name(item) for item in ['id', 'key', 'value']])
try:
with connection.cursor() as cursor:
cursor.execute('INSERT INTO constance_constance ( id, key, value ) SELECT id, key, value FROM constance_config', [])
cursor.execute(f'INSERT INTO constance_constance ( {quoted_string} ) SELECT {quoted_string} FROM constance_config', [])
cursor.execute('DROP TABLE constance_config', [])
except DatabaseError:
pass
except DatabaseError as exc:
logger.exception('copy data from old constance table to a new one')
Constance = apps.get_model('constance', 'Constance')
sequence_sql = connection.ops.sequence_reset_sql(no_style(), [Constance])