Skip to content

Commit

Permalink
fix migration on mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
Iurchenko Sergei committed Oct 5, 2023
1 parent bc9707d commit 1818915
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions constance/migrations/0002_migrate_from_old_table.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
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:
"""
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])
Expand Down

0 comments on commit 1818915

Please sign in to comment.