Skip to content

Commit

Permalink
pgroonga: Upgrade to PGroonga 2 API.
Browse files Browse the repository at this point in the history
  • Loading branch information
sampritipanda authored and timabbott committed May 31, 2018
1 parent aaad7fe commit 46711a4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
38 changes: 38 additions & 0 deletions pgroonga/migrations/0003_v2_api_upgrade.py
Original file line number Diff line number Diff line change
@@ -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);
"""])
]
4 changes: 2 additions & 2 deletions zerver/tests/test_narrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,13 @@ def test_add_term_using_search_operator_and_negated(
@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')
Expand Down
6 changes: 3 additions & 3 deletions zerver/views/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,15 @@ def by_search(self, query: Query, operand: str, maybe_negate: ConditionTransform

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,
Expand Down
10 changes: 0 additions & 10 deletions zproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,16 +598,6 @@ def get_dirs(self):
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
########################################################################
Expand Down
9 changes: 0 additions & 9 deletions zproject/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@
"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
Expand Down

0 comments on commit 46711a4

Please sign in to comment.