:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(file-system): add a team-scoped trigram index on file names (#120318)
Shy Alter5841fcdb36ed
diff --git a/posthog/migrations/1408_file_system_team_name_trgm_index.py b/posthog/migrations/1408_file_system_team_name_trgm_index.pynew file mode 100644index 000000000000..cf8bc797b0e6--- /dev/null+++ b/posthog/migrations/1408_file_system_team_name_trgm_index.py@@ -0,0 +1,35 @@+import django.contrib.postgres.indexes+import django.db.models.functions.text+from django.db import migrations, models++from posthog.migration_helpers import SafeAddIndexConcurrently+++class Migration(migrations.Migration):+ atomic = False++ dependencies = [+ ("posthog", "1407_scimprovisioneduser_scimrequestlog"),+ ]++ operations = [+ SafeAddIndexConcurrently(+ model_name="filesystem",+ index=django.contrib.postgres.indexes.GinIndex(+ models.F("team_id"),+ django.contrib.postgres.indexes.OpClass(+ django.db.models.functions.text.Lower(+ models.Func(+ models.F("path"),+ models.Value("^((?:[^\\\\/]|\\\\.)*/)*"),+ models.Value(""),+ function="regexp_replace",+ output_field=models.TextField(),+ )+ ),+ name="gin_trgm_ops",+ ),+ name="posthog_fs_team_name_trgm",+ ),+ ),+ ]diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txtindex bf44342621e4..3c1cba8ffd2b 100644--- a/posthog/migrations/max_migration.txt+++ b/posthog/migrations/max_migration.txt@@ -1 +1 @@-1407_scimprovisioneduser_scimrequestlog+1408_file_system_team_name_trgm_indexdiff --git a/posthog/models/file_system/file_system.py b/posthog/models/file_system/file_system.pyindex 01e65fee29fc..7f501ce7071f 100644--- a/posthog/models/file_system/file_system.py+++ b/posthog/models/file_system/file_system.py@@ -3,7 +3,7 @@ from typing import Optionalfrom django.contrib.postgres.indexes import GinIndex, OpClassfrom django.db import models-from django.db.models import Q, TextField, Value+from django.db.models import Func, Q, TextField, Valuefrom django.db.models.expressions import Ffrom django.db.models.functions import Concat, Lowerfrom django.utils import timezone@@ -15,6 +15,9 @@ from posthog.models.user import Userfrom posthog.models.utils import uuid7SEARCH_TEXT = Lower(Concat(F("path"), Value(" "), F("type"), output_field=TextField()))+SEARCH_NAME = Lower(+ Func(F("path"), Value(r"^((?:[^\\/]|\\.)*/)*"), Value(""), function="regexp_replace", output_field=TextField())+)class FileSystem(models.Model):@@ -46,6 +49,7 @@ class FileSystem(models.Model):models.Index(F("team_id"), F("surface"), F("depth"), name="posthog_fs_team_s_depth"),models.Index(F("team_id"), F("surface"), F("type"), F("ref"), name="posthog_fs_team_s_typeref"),GinIndex(F("team_id"), OpClass(SEARCH_TEXT, name="gin_trgm_ops"), name="posthog_fs_team_text_trgm"),+ GinIndex(F("team_id"), OpClass(SEARCH_NAME, name="gin_trgm_ops"), name="posthog_fs_team_name_trgm"),]def __str__(self):diff --git a/posthog/models/file_system/test/test_file_system.py b/posthog/models/file_system/test/test_file_system.pyindex 022ef801d254..eaea382d93ec 100644--- a/posthog/models/file_system/test/test_file_system.py+++ b/posthog/models/file_system/test/test_file_system.py@@ -1,8 +1,11 @@from django.test import TestCase+from parameterized import parameterized+from posthog.models import Organization, Team, Userfrom posthog.models.file_system.file_system import (DEFAULT_SURFACE,+ SEARCH_NAME,FileSystem,create_or_update_file,delete_file,@@ -165,6 +168,23 @@ class TestFileSystemModel(TestCase):# Edge case: empty listself.assertEqual(join_path([]), "")+ @parameterized.expand(+ [+ (["Unfiled", "Insights", "Revenue"],),+ (["Folder", "Retention / Weekly"],),+ (["Team\\", "Revenue"],),+ (["a\\", "b\\", "c"],),+ (["a\\/", "b"],),+ (["x", "y\\"],),+ ]+ )+ def test_search_name_matches_the_last_path_segment(self, segments: list[str]) -> None:+ entry = FileSystem.objects.create(team=self.team, path=join_path(segments), type="insight")++ name = FileSystem.objects.annotate(name=SEARCH_NAME).values_list("name", flat=True).get(id=entry.id)++ self.assertEqual(name, escape_path(segments[-1]).lower())+# Example test for save_unfiled_files with a specific file_typedef test_save_unfiled_files_specific_type(self):"""