Skip to content

Commit

Permalink
Fix exception in migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogost committed Sep 3, 2024
1 parent c690d5e commit c72c72b
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(

Check warning on line 21 in constance/migrations/0002_migrate_from_old_table.py

View check run for this annotation

Codecov / codecov/patch

constance/migrations/0002_migrate_from_old_table.py#L21

Added line #L21 was not covered by tests
f'INSERT INTO constance_constance ( {quoted_string} ) SELECT {quoted_string} FROM {old_table_name}', # noqa: S608
[],
)
cursor.execute(f'DROP TABLE {old_table_name}', [])

Check warning on line 25 in constance/migrations/0002_migrate_from_old_table.py

View check run for this annotation

Codecov / codecov/patch

constance/migrations/0002_migrate_from_old_table.py#L25

Added line #L25 was not covered by tests

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

0 comments on commit c72c72b

Please sign in to comment.