From 46711a43f0b7cc6821310d8910f27bfa16e285e5 Mon Sep 17 00:00:00 2001 From: Sampriti Panda Date: Thu, 31 May 2018 08:23:47 +0530 Subject: [PATCH] pgroonga: Upgrade to PGroonga 2 API. --- pgroonga/migrations/0003_v2_api_upgrade.py | 38 ++++++++++++++++++++++ zerver/tests/test_narrow.py | 4 +-- zerver/views/messages.py | 6 ++-- zproject/settings.py | 10 ------ zproject/test_settings.py | 9 ----- 5 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 pgroonga/migrations/0003_v2_api_upgrade.py diff --git a/pgroonga/migrations/0003_v2_api_upgrade.py b/pgroonga/migrations/0003_v2_api_upgrade.py new file mode 100644 index 0000000000..476e568eec --- /dev/null +++ b/pgroonga/migrations/0003_v2_api_upgrade.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +from django.db import models, migrations +from django.contrib.postgres import operations +from django.conf import settings + + +class Migration(migrations.Migration): + atomic = False + + dependencies = [ + ('pgroonga', '0002_html_escape_subject'), + ] + + database_setting = settings.DATABASES["default"] + operations = [ + migrations.RunSQL([""" +ALTER ROLE %(USER)s SET search_path TO %(SCHEMA)s,public; + +SET search_path = %(SCHEMA)s,public; + +DROP INDEX zerver_message_search_pgroonga; +""" % database_setting, """ + +CREATE INDEX CONCURRENTLY zerver_message_search_pgroonga ON zerver_message + USING pgroonga(search_pgroonga pgroonga_text_full_text_search_ops_v2); +"""], + [""" +ALTER ROLE %(USER)s SET search_path TO %(SCHEMA)s,public,pgroonga,pg_catalog; + +SET search_path = %(SCHEMA)s,public,pgroonga,pg_catalog; + +DROP INDEX zerver_message_search_pgroonga; +""" % database_setting, """ + +CREATE INDEX CONCURRENTLY zerver_message_search_pgroonga ON zerver_message + USING pgroonga(search_pgroonga pgroonga.text_full_text_search_ops); + """]) + ] diff --git a/zerver/tests/test_narrow.py b/zerver/tests/test_narrow.py index d8056343cf..89fdbc6d84 100644 --- a/zerver/tests/test_narrow.py +++ b/zerver/tests/test_narrow.py @@ -262,13 +262,13 @@ class NarrowBuilderTest(ZulipTestCase): @override_settings(USING_PGROONGA=True) def test_add_term_using_search_operator_pgroonga(self) -> None: term = dict(operator='search', operand='"french fries"') - self._do_add_term_test(term, 'WHERE search_pgroonga @@ escape_html(:escape_html_1)') + self._do_add_term_test(term, 'WHERE search_pgroonga &@~ escape_html(:escape_html_1)') @override_settings(USING_PGROONGA=True) def test_add_term_using_search_operator_and_negated_pgroonga( self) -> None: # NEGATED term = dict(operator='search', operand='"french fries"', negated=True) - self._do_add_term_test(term, 'WHERE NOT (search_pgroonga @@ escape_html(:escape_html_1))') + self._do_add_term_test(term, 'WHERE NOT (search_pgroonga &@~ escape_html(:escape_html_1))') def test_add_term_using_has_operator_and_attachment_operand(self) -> None: term = dict(operator='has', operand='attachment') diff --git a/zerver/views/messages.py b/zerver/views/messages.py index aae431219d..057ba56cd3 100644 --- a/zerver/views/messages.py +++ b/zerver/views/messages.py @@ -357,15 +357,15 @@ class NarrowBuilder: def _by_search_pgroonga(self, query: Query, operand: str, maybe_negate: ConditionTransform) -> Query: - match_positions_character = func.pgroonga.match_positions_character - query_extract_keywords = func.pgroonga.query_extract_keywords + match_positions_character = func.pgroonga_match_positions_character + query_extract_keywords = func.pgroonga_query_extract_keywords operand_escaped = func.escape_html(operand) keywords = query_extract_keywords(operand_escaped) query = query.column(match_positions_character(column("rendered_content"), keywords).label("content_matches")) query = query.column(match_positions_character(func.escape_html(column("subject")), keywords).label("subject_matches")) - condition = column("search_pgroonga").op("@@")(operand_escaped) + condition = column("search_pgroonga").op("&@~")(operand_escaped) return query.where(maybe_negate(condition)) def _by_search_tsearch(self, query: Query, operand: str, diff --git a/zproject/settings.py b/zproject/settings.py index 17587c17b2..0c89bec919 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -598,16 +598,6 @@ elif REMOTE_POSTGRES_HOST != '': else: DATABASES['default']['OPTIONS']['sslmode'] = 'verify-full' -if USING_PGROONGA: - # We need to have "pgroonga" schema before "pg_catalog" schema in - # the PostgreSQL search path, because "pgroonga" schema overrides - # the "@@" operator from "pg_catalog" schema, and "pg_catalog" - # schema is searched first if not specified in the search path. - # See also: http://www.postgresql.org/docs/current/static/runtime-config-client.html - pg_options = '-c search_path=%(SCHEMA)s,zulip,public,pgroonga,pg_catalog' % \ - DATABASES['default'] - DATABASES['default']['OPTIONS']['options'] = pg_options - ######################################################################## # RABBITMQ CONFIGURATION ######################################################################## diff --git a/zproject/test_settings.py b/zproject/test_settings.py index 207fb2454b..ab7167ce08 100644 --- a/zproject/test_settings.py +++ b/zproject/test_settings.py @@ -34,15 +34,6 @@ DATABASES["default"] = { "TEST_NAME": "django_zulip_tests", "OPTIONS": {"connection_factory": TimeTrackingConnection}, } -if USING_PGROONGA: - # We need to have "pgroonga" schema before "pg_catalog" schema in - # the PostgreSQL search path, because "pgroonga" schema overrides - # the "@@" operator from "pg_catalog" schema, and "pg_catalog" - # schema is searched first if not specified in the search path. - # See also: http://www.postgresql.org/docs/current/static/runtime-config-client.html - pg_options = '-c search_path=%(SCHEMA)s,zulip,public,pgroonga,pg_catalog' % \ - DATABASES['default'] - DATABASES['default']['OPTIONS']['options'] = pg_options if "TORNADO_SERVER" in os.environ: # This covers the Casper test suite case