From 99acc78c2501794d9844b940771a781bdf042d2f Mon Sep 17 00:00:00 2001 From: Josh Yu Date: Thu, 7 Nov 2024 22:07:52 +0800 Subject: [PATCH] fix: refine 0002 migration file --- .../migrations/0002_migrate_locked_by.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/djangocms_version_locking/migrations/0002_migrate_locked_by.py b/djangocms_version_locking/migrations/0002_migrate_locked_by.py index 7c6c42f..9036c2f 100644 --- a/djangocms_version_locking/migrations/0002_migrate_locked_by.py +++ b/djangocms_version_locking/migrations/0002_migrate_locked_by.py @@ -1,6 +1,6 @@ # Generated by Django 4.2.8 on 2024-11-07 07:21 -from django.db import migrations +from django.db import migrations, transaction def forwards(apps, schema_editor): @@ -8,13 +8,14 @@ def forwards(apps, schema_editor): Version = apps.get_model("djangocms_versioning", "Version") VersionLock = apps.get_model("djangocms_version_locking", "VersionLock") - version_qs = Version.objects.using(db_alias).all() version_lock_qs = VersionLock.objects.using(db_alias).all() - for lock_item in version_lock_qs: - locked_version = version_qs.filter(pk=lock_item.version.pk) - if locked_version: - locked_version.locked_by = lock_item.created_by - locked_version.save() + versions = set(Version.objects.using(db_alias).filter(pk__in=version_lock_qs.values('version'))) + + with transaction.atomic(): + for version in versions: + version.locked_by = version.versionlock.created_by + + Version.objects.using(db_alias).bulk_update(versions, ['locked_by']) class Migration(migrations.Migration):