[Patchew-devel] [PATCH] fix NonNullSearchVector for new django

Paolo Bonzini posted 1 patch 1 week, 5 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/patchew next tags/patchew/20201120134634.152020-1-pbonzini@redhat.com
api/search.py | 28 +++++++++++++++++++++-------
1 file changed, 21 insertions(+), 7 deletions(-)

[Patchew-devel] [PATCH] fix NonNullSearchVector for new django

Posted by Paolo Bonzini 1 week, 5 days ago
More recent Django has moved the COALESCEing of SearchVector
arguments from __init__ to as_sql.  Adjust NonNullSearchVector
to monkeypatch as_sql instead of __init__.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 api/search.py | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/api/search.py b/api/search.py
index 4f6650c..5d4529d 100644
--- a/api/search.py
+++ b/api/search.py
@@ -35,18 +35,32 @@ class InvalidSearchTerm(Exception):
 
 
 # Hack alert: Django wraps each argument to to_tsvector with a COALESCE function,
-# and that causes postgres not to use the index.  Monkeypatch the constructor
-# to skip that step, which we do not need since the subject field is not nullable.
+# and that causes postgres not to use the index.  Monkeypatch as_sql to skip
+# that step, which we do not need since the subject field is not nullable.
 class NonNullSearchVector(SearchVector):
     function = "to_tsvector"
     arg_joiner = " || ' ' || "
     _output_field = SearchVectorField()
-    config = None
 
-    def __init__(self, *expressions, **extra):
-        super(SearchVector, self).__init__(*expressions, **extra)
-        self.config = self.extra.get("config", self.config)
-        self.weight = None
+    def as_sql(self, compiler, connection, function=None, template=None):
+        config_sql = None
+        config_params = []
+        if template is None:
+            if self.config:
+                config_sql, config_params = compiler.compile(self.config)
+                template = '%(function)s(%(config)s, %(expressions)s)'
+            else:
+                template = self.template
+        sql, params = super(SearchVector, self).as_sql(
+            compiler, connection, function=function, template=template,
+            config=config_sql,
+        )
+        extra_params = []
+        if self.weight:
+            weight_sql, extra_params = compiler.compile(self.weight)
+            sql = 'setweight({}, {})'.format(sql, weight_sql)
+        return sql, config_params + params + extra_params
+
 
 
 class SearchEngine(object):
-- 
2.28.0

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel