Reinstate MSSQL implementation of _update_descendant_url_paths

We don't currently have testing infrastructure to confirm that the pure-ORM version works on MSSQL, so we'll make an exception for it in the interests of not blocking this fix for other DBs.
This commit is contained in:
Matt Westcott 2017-10-06 12:40:28 +01:00
parent cf25d07120
commit 0e38f0b401

View file

@ -14,7 +14,7 @@ from django.core.exceptions import ValidationError
from django.core.handlers.base import BaseHandler
from django.core.handlers.wsgi import WSGIRequest
from django.core.urlresolvers import reverse
from django.db import models, transaction
from django.db import connection, models, transaction
from django.db.models import Q, Value
from django.db.models.functions import Concat, Substr
from django.http import Http404
@ -576,12 +576,20 @@ class Page(six.with_metaclass(PageBase, AbstractPage, index.Indexed, Clusterable
return errors
def _update_descendant_url_paths(self, old_url_path, new_url_path):
(Page.objects
.filter(path__startswith=self.path)
.exclude(pk=self.pk)
.update(url_path=Concat(
Value(new_url_path),
Substr('url_path', len(old_url_path) + 1))))
if connection.vendor in ('mssql', 'microsoft'):
cursor = connection.cursor()
cursor.execute("""
UPDATE wagtailcore_page
SET url_path= CONCAT(%s, (SUBSTRING(url_path, 0, %s)))
WHERE path LIKE %s AND id <> %s
""", [new_url_path, len(old_url_path) + 1, self.path + '%', self.id])
else:
(Page.objects
.filter(path__startswith=self.path)
.exclude(pk=self.pk)
.update(url_path=Concat(
Value(new_url_path),
Substr('url_path', len(old_url_path) + 1))))
#: Return this page in its most specific subclassed form.
@cached_property