diff --git a/CHANGELOG.md b/CHANGELOG.md index f1ace5e26..0fe40081f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Markdown editor: Show markdown snippet in markdown image preview dialogs * Bugfixes in note sharing * Fix race condition when creating comments for text selection +* Reset DB sequences on restore backup to prevent ID conflicts ## v2024.74 - 2024-09-24 diff --git a/api/src/reportcreator_api/api_utils/backup_utils.py b/api/src/reportcreator_api/api_utils/backup_utils.py index 0e2aa6d12..c2de2da32 100644 --- a/api/src/reportcreator_api/api_utils/backup_utils.py +++ b/api/src/reportcreator_api/api_utils/backup_utils.py @@ -14,6 +14,7 @@ from django.conf import settings from django.core import serializers from django.core.management import call_command +from django.core.management.color import no_style from django.core.serializers.json import DjangoJSONEncoder from django.db import connection, transaction from django.db.migrations.executor import MigrationExecutor @@ -235,6 +236,14 @@ def get_model(model_identifier): connection.check_constraints() +def reset_database_sequences(): + app_list = [app_config for app_config in apps.get_app_configs() if app_config.models_module is not None] + models = list(itertools.chain(*map(lambda a: a.get_models(include_auto_created=True), app_list))) + statements = connection.ops.sequence_reset_sql(style=no_style(), model_list=models) + with connection.cursor() as cursor: + cursor.execute('\n'.join(statements)) + + def walk_storage_dir(storage, base_dir=None): base_dir = base_dir or '' try: @@ -333,6 +342,11 @@ def restore_backup(z, keepfiles=True): restore_database_dump(f) logging.info('Finished restoring DB data') + # Reset sequences + logging.info('Begin resetting DB sequences') + reset_database_sequences() + logging.info('Finished resetting DB sequences') + # Restore files logging.info('Begin restoring files') if not keepfiles: diff --git a/api/src/reportcreator_api/tests/test_backup.py b/api/src/reportcreator_api/tests/test_backup.py index 736910b15..c525e51ad 100644 --- a/api/src/reportcreator_api/tests/test_backup.py +++ b/api/src/reportcreator_api/tests/test_backup.py @@ -240,6 +240,9 @@ def test_backup_restore(self): # BackupLog entry created assert list(BackupLog.objects.values_list('type', flat=True)) == [BackupLogType.RESTORE, BackupLogType.BACKUP, BackupLogType.SETUP] + # Can add ignored word without ID conflict + create_languagetool_ignore_word() + def test_backup_restore_damaged(self): backup = b''.join(self.backup_request(aes_key=self.backup_encryption_key).streaming_content) backup = backup[:len(backup) - 100]