From 1818915f0c9f90b2b780cd557ad93e610b10bcea Mon Sep 17 00:00:00 2001 From: Iurchenko Sergei Date: Fri, 6 Oct 2023 02:24:44 +0300 Subject: [PATCH] fix migration on mysql --- .../migrations/0002_migrate_from_old_table.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/constance/migrations/0002_migrate_from_old_table.py b/constance/migrations/0002_migrate_from_old_table.py index c3adabc2..da2fc69e 100644 --- a/constance/migrations/0002_migrate_from_old_table.py +++ b/constance/migrations/0002_migrate_from_old_table.py @@ -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])