Skip to content

Commit

Permalink
fix: refine 0002 migration file
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Yu committed Nov 7, 2024
1 parent c45768e commit 99acc78
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions djangocms_version_locking/migrations/0002_migrate_locked_by.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# 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):
db_alias = schema_editor.connection.alias
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):
Expand Down

0 comments on commit 99acc78

Please sign in to comment.