Skip to content

Commit

Permalink
fix(community): trigger to update search_vector
Browse files Browse the repository at this point in the history
No-Issue
  • Loading branch information
rochacbruno committed Nov 15, 2023
1 parent ac50a54 commit 7e7f151
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
4 changes: 3 additions & 1 deletion galaxy_ng/app/api/ui/views/search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db import connection
from django.contrib.postgres.aggregates import JSONBAgg
from django.contrib.postgres.search import SearchQuery, SearchVector
from django.db.models import (
Expand Down Expand Up @@ -273,7 +274,8 @@ def get_role_queryset(self, query=None):
search_vector=vector,
relevance=relevance,
).values(*QUERYSET_VALUES)
print(qs._query)
print(qs.all())
print(connection.queries)
return qs

def filter_and_sort(self, collections, roles, filter_params, sort, type="", query=None):
Expand Down
15 changes: 15 additions & 0 deletions galaxy_ng/app/api/v1/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.db import models
from django.contrib.postgres.search import SearchVectorField
from django.contrib.postgres.indexes import GinIndex

from galaxy_ng.app.models import Namespace
from galaxy_ng.app.models.auth import User
Expand Down Expand Up @@ -185,6 +187,19 @@ class LegacyRoleDownloadCount(models.Model):
count = models.IntegerField(default=0)


class LegacyRoleSearchVector(models.Model):
role = models.OneToOneField(
LegacyRole,
on_delete=models.CASCADE,
primary_key=True,
)
search_vector = SearchVectorField(default="")
modified = models.DateTimeField(auto_now=True)

class Meta:
indexes = (GinIndex(fields=["search_vector"]),)


class LegacyRoleImport(models.Model):
role = models.ForeignKey(
'LegacyRole',
Expand Down
38 changes: 38 additions & 0 deletions galaxy_ng/app/migrations/0046_legacyrolesearchvector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2.7 on 2023-11-15 15:52

import django.contrib.postgres.indexes
import django.contrib.postgres.search
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
("galaxy", "0045_setting"),
]

operations = [
migrations.CreateModel(
name="LegacyRoleSearchVector",
fields=[
(
"role",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
primary_key=True,
serialize=False,
to="galaxy.legacyrole",
),
),
("search_vector", django.contrib.postgres.search.SearchVectorField(default="")),
("modified", models.DateTimeField(auto_now=True)),
],
options={
"indexes": [
django.contrib.postgres.indexes.GinIndex(
fields=["search_vector"], name="galaxy_lega_search__13e661_gin"
)
],
},
),
]
53 changes: 53 additions & 0 deletions galaxy_ng/app/migrations/0047_update_role_search_vector_trigger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by Django 4.2.7 on 2023-11-15 15:55

from django.db import migrations

REBUILD_ROLES_TS_VECTOR = """
UPDATE galaxy_legacyrole SET name = name;
"""

CREATE_ROLE_TS_VECTOR_TRIGGER = """
CREATE OR REPLACE FUNCTION update_role_ts_vector()
RETURNS TRIGGER AS
$$
DECLARE
_search_vector tsvector;
BEGIN
SELECT ((((
setweight(to_tsvector(COALESCE(ln."name", '')), 'A')
|| setweight(to_tsvector(COALESCE(lr."name", '')), 'A'))
|| setweight(to_tsvector(COALESCE(((lr."full_metadata"->'tags'))::text, '')), 'B'))
|| setweight(to_tsvector(COALESCE(((lr."full_metadata"->'platforms'))::text, '')), 'C'))
|| setweight(to_tsvector(COALESCE((lr."full_metadata"->>'description'), '')), 'D'))
into _search_vector
FROM galaxy_legacyrole lr
INNER JOIN galaxy_legacynamespace ln ON (lr.namespace_id = ln.id)
WHERE lr.id = NEW.id;
INSERT INTO galaxy_legacyrolesearchvector(role_id,search_vector,modified)
VALUES(new.id,_search_vector,current_timestamp)
ON CONFLICT (role_id)
DO
UPDATE SET search_vector = _search_vector, modified = current_timestamp;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS update_ts_vector ON galaxy_legacyrole;
CREATE TRIGGER update_ts_vector
AFTER INSERT OR UPDATE
ON galaxy_legacyrole
FOR EACH ROW
EXECUTE PROCEDURE update_role_ts_vector();
"""

DROP_ROLE_TS_VECTOR_TRIGGER = """
DROP TRIGGER IF EXISTS update_ts_vector ON galaxy_legacyrole;
DROP FUNCTION IF EXISTS update_role_ts_vector();
"""


class Migration(migrations.Migration):
dependencies = [
("galaxy", "0046_legacyrolesearchvector"),
]

operations = []

0 comments on commit 7e7f151

Please sign in to comment.