Fix #706: Added a 'fix_federation_ids' management command to deal with protocol/domain issues in federation

IDs after deployments
This commit is contained in:
Eliot Berriot 2019-02-11 11:11:08 +01:00
parent f780fa24c1
commit 00846ca3e9
No known key found for this signature in database
GPG key ID: DD6965E2476E5C27
7 changed files with 238 additions and 0 deletions

View file

@ -147,3 +147,28 @@ def order_for_search(qs, field):
this function will order the given qs based on the length of the given field
"""
return qs.annotate(__size=models.functions.Length(field)).order_by("__size")
def replace_prefix(queryset, field, old, new):
"""
Given a queryset of objects and a field name, will find objects
for which the field have the given value, and replace the old prefix by
the new one.
This is especially useful to find/update bad federation ids, to replace:
http://wrongprotocolanddomain/path
by
https://goodprotocalanddomain/path
on a whole table with a single query.
"""
qs = queryset.filter(**{"{}__startswith".format(field): old})
# we extract the part after the old prefix, and Concat it with our new prefix
update = models.functions.Concat(
models.Value(new),
models.functions.Substr(field, len(old) + 1, output_field=models.CharField()),
)
return qs.update(**{field: update})