mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 00:08:25 +00:00
fix: read view field options once when creating missing ones (#6952)
Each missing field option re-read all the view's field options, twice for grid views (Sentry BASEROW-SAAS-BACKEND-135).
This commit is contained in:
1 parent
aacb878968
commit
8aed5fdc85
3 files changed
+58
-9
No files matched your search
@@ -9,7 +9,7 @@ from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.postgres.fields import ArrayField
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.db.models.query import Prefetch
|
||||
from django.db.models.query import Prefetch, prefetch_related_objects
|
||||
from django.utils.functional import lazy
|
||||
|
||||
from baserow.contrib.database.fields.field_filters import (
|
||||
@@ -339,14 +339,29 @@ class View(
|
||||
# in-efficient because this rarely happens. The most important part
|
||||
# is that the check is fast.
|
||||
existing_field_ids = [options.field_id for options in existing_field_options]
|
||||
new_field_options = through_model.objects.bulk_create(
|
||||
[
|
||||
view_type.prepare_field_options(self, field.id)
|
||||
for field in fields
|
||||
if field.id not in existing_field_ids
|
||||
],
|
||||
ignore_conflicts=True,
|
||||
|
||||
# `prepare_field_options` reads all the field options of the view for every
|
||||
# missing field. Nothing is created until the end, so fetch them once.
|
||||
related_name = f"{through_model._meta.model_name}_set"
|
||||
already_prefetched = related_name in getattr(
|
||||
self, "_prefetched_objects_cache", {}
|
||||
)
|
||||
if not already_prefetched:
|
||||
prefetch_related_objects([self], related_name)
|
||||
|
||||
try:
|
||||
new_field_options = through_model.objects.bulk_create(
|
||||
[
|
||||
view_type.prepare_field_options(self, field.id)
|
||||
for field in fields
|
||||
if field.id not in existing_field_ids
|
||||
],
|
||||
ignore_conflicts=True,
|
||||
)
|
||||
finally:
|
||||
# The fetched options don't include the new ones anymore.
|
||||
if not already_prefetched:
|
||||
self._prefetched_objects_cache.pop(related_name, None)
|
||||
return new_field_options
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.db import IntegrityError
|
||||
from django.db import IntegrityError, connection
|
||||
from django.test.utils import CaptureQueriesContext
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -415,3 +416,27 @@ def test_migration_remove_stale_fieldoptions(
|
||||
|
||||
# Only the stale field option should be deleted.
|
||||
assert GridViewFieldOptions.objects.filter(grid_view=grid_view).count() == 1
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_creating_missing_field_options_does_not_query_per_field(data_fixture):
|
||||
def create_missing(num_fields):
|
||||
table = data_fixture.create_database_table()
|
||||
hidden_field = data_fixture.create_text_field(table=table)
|
||||
grid_view = data_fixture.create_grid_view(table=table)
|
||||
data_fixture.create_grid_view_field_option(grid_view, hidden_field, hidden=True)
|
||||
for _ in range(num_fields):
|
||||
data_fixture.create_text_field(table=table)
|
||||
|
||||
with CaptureQueriesContext(connection) as captured:
|
||||
field_options = grid_view.get_field_options(create_if_missing=True)
|
||||
return grid_view, field_options, len(captured)
|
||||
|
||||
_, _, few_fields_num_queries = create_missing(2)
|
||||
grid_view, field_options, many_fields_num_queries = create_missing(6)
|
||||
|
||||
assert many_fields_num_queries == few_fields_num_queries
|
||||
# New options are hidden because another field is hidden in the view.
|
||||
assert [o.hidden for o in field_options] == [True] * 7
|
||||
# The options fetched while creating them aren't kept on the view.
|
||||
assert len(grid_view.gridviewfieldoptions_set.all()) == 7
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"type": "bug",
|
||||
"message": "Speeds up opening a view for the first time after fields were added",
|
||||
"issue_origin": "github",
|
||||
"issue_number": null,
|
||||
"domain": "database",
|
||||
"bullet_points": [],
|
||||
"created_at": "2026-12-01"
|
||||
}
|
||||
Reference in new issue
Block a user