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 migration on mysql #531

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
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
Loading