Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ dev = [
"django-extensions==4.1",
"snoop==0.6.0",
"openapi-spec-validator==0.7.2",
"pytest-html==4.1.1",
"coverage==7.13.1",
"pytest-html==4.2.0",
"coverage==7.13.5",
"pytest-split==0.11.0",
"pytest-unordered==0.7.0",
"debugpy==1.8.20",
Expand All @@ -145,7 +145,7 @@ dev = [
"mypy==1.19.1",
"mypy-extensions==1.1.0",
"ipython",
"fakeredis[lua]==2.33.0",
"fakeredis[lua]==2.34.1",
"pytest-retry==1.7.0",
"pytest-testmon==2.2.0",
"ipdb",
Expand Down
17 changes: 16 additions & 1 deletion backend/src/baserow/contrib/database/data_sync/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,24 @@ def set_data_sync_synced_properties(

handler = FieldHandler()

unique_primary_field = next(
(ep.field for ep in enabled_properties if ep.unique_primary), None
)

for data_sync_property_instance in properties_to_be_removed:
field = data_sync_property_instance.field
# If we're about to delete the primary field, first move primary to the
# unique_primary field so the table is never left without one.
if (
field.primary
and unique_primary_field
and unique_primary_field.id != field.id
):
handler.change_primary_field(
user=user,
table=data_sync.table,
new_primary_field=unique_primary_field,
)
data_sync_property_instance.delete()
handler.delete_field(
user=user,
Expand Down Expand Up @@ -655,7 +671,6 @@ def set_data_sync_synced_properties(
data_sync.table,
[field_kwargs.pop("name")],
)
print(new_name)
field = handler.create_field(
user=user,
table=data_sync.table,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.db import migrations


def fix_data_sync_missing_primary_field(apps, schema_editor):
"""
Finds data sync tables that have no primary field and restores primary=True
on the field associated with their unique_primary synced property.

This fixes a bug where the primary field could be lost when a user changed
the primary to a non-unique_primary field, and that field was later removed
during a data sync.
"""

DataSyncSyncedProperty = apps.get_model(
"database", "DataSyncSyncedProperty"
)
Field = apps.get_model("database", "Field")

tables_with_primary = Field.objects.filter(primary=True).values("table_id")
should_be_primary_field_ids = (
DataSyncSyncedProperty.objects.filter(unique_primary=True)
.exclude(data_sync__table_id__in=tables_with_primary)
.values("field_id")
)
Field.objects.filter(id__in=should_be_primary_field_ids).update(primary=True)


class Migration(migrations.Migration):

dependencies = [
("database", "0207_add_view_default_values"),
]

operations = [
migrations.RunPython(
fix_data_sync_missing_primary_field,
migrations.RunPython.noop,
),
]
Loading
Loading