From cc104716e03d066cfdce18ef52927b7bbfa75866 Mon Sep 17 00:00:00 2001 From: catborise Date: Sun, 8 Nov 2026 12:20:17 +0300 Subject: [PATCH] refactor(instances): a migration merges owners with move_ownership record_migration() repeated computes.utils._move_ownership() line by line: owners move to the record the destination already has, a user's rights are merged and the template mark is kept. It now calls that function, renamed move_ownership as it is used from both apps; the owners left on the source record go with it, by cascade. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- computes/utils.py | 4 +-- instances/tests/test_migration_bookkeeping.py | 9 +++++++ instances/utils.py | 25 ++----------------- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/computes/utils.py b/computes/utils.py index c7605832..d2d29a1f 100644 --- a/computes/utils.py +++ b/computes/utils.py @@ -64,7 +64,7 @@ def _log(inst, what): write("-", inst.compute.name, inst.name, f"VM {inst.name} ({inst.uuid}): {what}", True, uuid=inst.uuid) -def _move_ownership(source, target): +def move_ownership(source, target): """Give target the owners and template flag of source; a user's rights are merged.""" for ui in UserInstance.objects.filter(instance=source): existing = UserInstance.objects.filter(instance=target, user=ui.user).first() @@ -88,7 +88,7 @@ def forget(inst): comes back.""" elsewhere = Instance.objects.filter(uuid=inst.uuid).exclude(compute_id=inst.compute_id).first() if elsewhere: - _move_ownership(inst, elsewhere) + move_ownership(inst, elsewhere) _log(inst, f"owners moved to {elsewhere.compute.name}") else: _bury(inst) diff --git a/instances/tests/test_migration_bookkeeping.py b/instances/tests/test_migration_bookkeeping.py index 5d24cc70..ef3b9517 100644 --- a/instances/tests/test_migration_bookkeeping.py +++ b/instances/tests/test_migration_bookkeeping.py @@ -68,3 +68,12 @@ class MigrationBookkeepingTests(TestCase): found.refresh_from_db() self.assertTrue(found.is_template) self.assertEqual(UserInstance.objects.get(user=owner).instance_id, found.id) + + def test_an_owner_of_both_records_keeps_the_rights_of_both(self): + owner = get_user_model().objects.create_user("mb-both") + UserInstance.objects.create(user=owner, instance=self.vm, is_change=True) + found = Instance.objects.create(compute=self.dest, name="vm", uuid="u-mb") + UserInstance.objects.create(user=owner, instance=found, is_vnc=True) + self.migrate() + ui = UserInstance.objects.get(user=owner) + self.assertEqual((ui.instance_id, ui.is_change, ui.is_delete, ui.is_vnc), (found.id, True, False, True)) diff --git a/instances/utils.py b/instances/utils.py index 23680ff7..281c684d 100644 --- a/instances/utils.py +++ b/instances/utils.py @@ -8,7 +8,7 @@ from collections import namedtuple from accounts.models import UserInstance, UserAttributes from appsettings.settings import app_settings from computes.models import Compute -from computes.utils import lock_ownership +from computes.utils import lock_ownership, move_ownership from django.conf import settings from django.db import transaction from django.utils.translation import gettext_lazy as _ @@ -351,28 +351,7 @@ def record_migration(instance, new_compute): lock_ownership() target_inst = Instance.objects.filter(compute=new_compute, uuid=instance.uuid).first() if target_inst and target_inst.id != instance.id: - for ui in UserInstance.objects.filter(instance=instance): - existing_ui = UserInstance.objects.filter(instance=target_inst, user=ui.user).first() - if not existing_ui: - ui.instance = target_inst - ui.save() - else: - updated = False - if ui.is_change and not existing_ui.is_change: - existing_ui.is_change = True - updated = True - if ui.is_delete and not existing_ui.is_delete: - existing_ui.is_delete = True - updated = True - if ui.is_vnc and not existing_ui.is_vnc: - existing_ui.is_vnc = True - updated = True - if updated: - existing_ui.save() - ui.delete() - if instance.is_template and not target_inst.is_template: - target_inst.is_template = True - target_inst.save(update_fields=["is_template"]) + move_ownership(instance, target_inst) instance.delete() instance.id = target_inst.id instance.compute = new_compute