mirror of
https://github.com/django/django.git
synced 2025-10-09 06:49:12 +00:00
Refs #27489 -- Made RenamePermission() operation respect database.
Regression in f02b49d2f3bf84f5225de920ca510149f1f9f1da. Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
This commit is contained in:
parent
e7740780d0
commit
6e89271a85
@ -126,10 +126,10 @@ class RenamePermission(migrations.RunPython):
|
||||
from django.contrib.auth.models import Permission
|
||||
|
||||
db = schema_editor.connection.alias
|
||||
ctypes = ContentType.objects.filter(
|
||||
ctypes = ContentType.objects.using(db).filter(
|
||||
app_label=self.app_label, model__icontains=old_model.lower()
|
||||
)
|
||||
for permission in Permission.objects.filter(
|
||||
for permission in Permission.objects.using(db).filter(
|
||||
content_type_id__in=ctypes.values("id")
|
||||
):
|
||||
prefix = permission.codename.split("_")[0]
|
||||
|
@ -1546,6 +1546,7 @@ class PermissionRenameOperationsTests(TransactionTestCase):
|
||||
"django.contrib.auth",
|
||||
"auth_tests",
|
||||
]
|
||||
databases = {"default", "other"}
|
||||
|
||||
def setUp(self):
|
||||
app_config = apps.get_app_config("auth_tests")
|
||||
@ -1605,6 +1606,26 @@ class PermissionRenameOperationsTests(TransactionTestCase):
|
||||
Permission.objects.filter(codename=f"{action}_newmodel").exists()
|
||||
)
|
||||
|
||||
def test_permission_rename_other_db(self):
|
||||
ct = ContentType.objects.using("default").create(
|
||||
app_label="auth_tests", model="oldmodel"
|
||||
)
|
||||
permission = Permission.objects.using("default").create(
|
||||
codename="add_oldmodel",
|
||||
name="Can add old model",
|
||||
content_type=ct,
|
||||
)
|
||||
# RenamePermission respects the database.
|
||||
call_command("migrate", "auth_tests", verbosity=0, database="other")
|
||||
permission.refresh_from_db()
|
||||
self.assertEqual(permission.codename, "add_oldmodel")
|
||||
self.assertFalse(
|
||||
Permission.objects.using("other").filter(codename="add_oldmodel").exists()
|
||||
)
|
||||
self.assertTrue(
|
||||
Permission.objects.using("other").filter(codename="add_newmodel").exists()
|
||||
)
|
||||
|
||||
@mock.patch(
|
||||
"django.db.router.allow_migrate_model",
|
||||
return_value=False,
|
||||
|
Loading…
x
Reference in New Issue
Block a user