Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception in 0002 migration #579

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
"""
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')
sergei-iurchenko marked this conversation as resolved.
Show resolved Hide resolved
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