Skip to content

Commit

Permalink
Fix exception in 0002 migration (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogost authored Sep 5, 2024
1 parent f1d0dfc commit a68bf90
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions constance/migrations/0002_migrate_from_old_table.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from logging import getLogger

from django.core.management.color import no_style
from django.db import DatabaseError
from django.db import migrations

logger = getLogger(__name__)
Expand All @@ -14,15 +13,16 @@ def _migrate_from_old_table(apps, schema_editor) -> None:
"""
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(
f'INSERT INTO constance_constance ( {quoted_string} ) SELECT {quoted_string} FROM constance_config', # noqa: S608
[],
)
cursor.execute('DROP TABLE constance_config', [])
except DatabaseError:
logger.exception('copy data from old constance table to a new one')
old_table_name = 'constance_config'
with connection.cursor() as cursor:
if old_table_name not in connection.introspection.table_names():
logger.info('Old table does not exist, skipping')
return
cursor.execute(
f'INSERT INTO constance_constance ( {quoted_string} ) SELECT {quoted_string} FROM {old_table_name}', # noqa: S608
[],
)
cursor.execute(f'DROP TABLE {old_table_name}', [])

Constance = apps.get_model('constance', 'Constance')
sequence_sql = connection.ops.sequence_reset_sql(no_style(), [Constance])
Expand Down

0 comments on commit a68bf90

Please sign in to comment.